Firstly I think your choice of using a
while
loop is wrong. I would use a
for
loop because you know up front how many iterations you need. I would use a
while
loop if the number of iterations depended on the user input - for example if you prompted "Type X to stop the input"
Most importantly you are only capturing the input of the first number ... you have a
Console.ReadLine();
but you are not assigning the number to anything. number is only ever set on the first input
number = Convert.ToInt32(Console.ReadLine());
it needs to be captured within the loop on each iteration.
I really hate
Convert.ToInt32
, especially if it is used with user-input.
int.TryParse
(and similar functions) is far better (less chance to have an exception thrown)
For example:
static void Main(string[] args)
{
int highestnumber = 0;
const int NoOfSalesToCompare = 10;
for (int counter = 1; counter <= NoOfSalesToCompare; counter++)
{
int number;
Console.WriteLine("Enter number of sales for salesperson {0}:", counter);
if(int.TryParse(Console.ReadLine(), out number))
if(number > highestnumber) highestnumber = number;
}
Console.WriteLine("The highest number of sales is: {0}", highestnumber);
Console.ReadLine();
}
Lastly, if you learn to use the debugger you would have probably spotted this for yourself. Stepping through lines of code that don't work is an excellent way of improving your knowledge. Actually, stepping through lines of code that *do* work that you've found somewhere is also a good way of improving your knowledge.
I strongly recommend that you step through the code that I've given you and thoroughly understand why it works, but yours didn't. Don't forget ... your teacher probably uses this site too.