CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Need some Help

  1. #1
    Join Date
    Sep 2016
    Posts
    4

    Need some Help

    I'm extremely lost on how to do this...

    If anyone could help, it would be greatly appreciated.

    I need to write an application in c# that continuously prompts a user for test scores until the user enters a sentinel value. A valid score ranges from 0 through 100. When the user enters a valid score, add it to a total; when the user enters an invalid score, display an error message and before the program ends, display the number of scores entered and their average.

    The code needs to contain a while loop.

    Thanks!

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Need some Help

    And the difference between this thread and your other one http://forums.codeguru.com/showthrea...31#post2203431 is??

    Are you writing the same program in c++ and c#?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Sep 2016
    Posts
    4

    Re: Need some Help

    Im writing in c#

    this is what i have so far in a console application

    bool IsValidScore=true;
    List<int> score=new List<int>();
    while(IsValidScore)
    {
    int tempVal=-1;
    if(Int32.TryParse(Console.ReadLine(),out tempVal) && (tempVal>=0 && tempVal<=100))
    {
    score.Add(tempVal);
    }
    else
    {
    score.ForEach(i=>Console.WriteLine(i));
    Console.WriteLine($"Average: {score.Average()}");
    Console.WriteLine("Error.Try again.");
    IsValidScore=false;
    }
    }

  4. #4
    Join Date
    Jun 2016
    Location
    Columbus, Ohio, USA
    Posts
    12

    Re: Need some Help

    I will point you in the correct direction, but not do your homework. Read here for a basic console app to write "Hello World!":
    https://msdn.microsoft.com/en-us/library/k1sx6ed2.aspx

    Within the body of main(), place your existing code and compile. Resolve the errors one by one until it compiles cleanly. The one thing I see missing in your code is a prompt to the user telling them what to enter. It could go right after your int declaration.

    EDIT: I am still in C# <6, so my debugger complains on the new interpolated string. I cannot help you directly due to that, but can give more general help.
    Last edited by Jim Snyder; September 8th, 2016 at 08:43 AM. Reason: Added caveat
    .NET Framework Version 4.5.50938; Visual Studio Professional 2013 Version 12.0.21005.1 REL
    "Having power is nice, having a lot of power is very nice, having too much power is just about right!"

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured