Click here to Skip to main content
15,906,335 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a stuck about calculate average does not work as I think so. It says marks entered must be 0 to 100 (including 0 and 100), or the user is prompted to re-enter until a valid mark is received. The user enters a mark of -1 to end the input process. The program should then display: 

1. How many students have been processed
2. The average mark of all students

Hints: there should be two nested loops to solve this problem - one inside anther - the loop for validating user input should be in the inner one.

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Practical_1_Q16_Calculate_Average
{
    class Program
    {
        static void Main(string[] args)
        {
            double total = 0;
            int count = 0;
            double score = 5;

            Console.WriteLine("Enter a score (0-100): ");
            score = double.Parse(Console.ReadLine());
            while (score < 0 || score > 100)
            {
                Console.WriteLine("Invalid, must be 0-100");
                score = double.Parse(Console.ReadLine());
                total += score;
                count++;
            }
            if (count == 0)
            {
                Console.WriteLine("No numbers were entered.");
            }
            else
            {
                Console.WriteLine("Average:" + total / count);
            }
            Console.WriteLine("");
            Console.WriteLine("Program finished");
            Console.ReadLine();
        }
    }
}
Posted
Updated 7-Sep-16 0:04am
v2
Comments
[no name] 7-Sep-16 5:45am    
The program runs but results wrong. Next step get out the debugger and fix it.
jimboo1232 7-Sep-16 15:32pm    
Did you get this to run? If you did, would you mind posting the code? I would like to see this in action.

Thank you.

Start by reading the instructions again, and paying attention to the "Hint"

Then, I'd suggest that double.TryParse would be a lot better to use than double.Parse: if you entered 56 values and then mistyped one and the app crashed, you wouldn't be a happy bunny! TryParse tells you it was a bad number instead of throwing an exception and crashing the program, so you can tell the user and move on.

Then, when you get the value "-1" from the user, you should exit the loop and show the results.

When you read the first value from the user outside your loop, how many samples have you got? is it really zero?

And finally, the while loop executes the lines it contains as long as the condition is true. If the user starts with a valid value, it will never enter your loop...as I said, read the "hint"...
 
Share this answer
 
v2
Comments
Maciej Los 7-Sep-16 16:04pm    
5ed!
You didn't follow the hints: your program doesn't feature two nested loops, pseudo-code:

C#
do 
{
  score = get_from_user();
  if ( score >= 0 && score <=100)
  {
    update_and_possibly_print_values();
  }
} while (score != -1);
 
Share this answer
 
Comments
Maciej Los 7-Sep-16 16:04pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900