Ugh...stuck on the stupidest simplest coding problem :( Help?

I need to enter random integers into a Console program, and then hit enter to total them up!!! Its a simple addition calculation for crying out loud!

Can someone please tell me how to do this using Console ReadLine and a damn loop? Using Enter to break the loop? (Maybe with IsNotNullorWhiteSpace to break the loop)?

…need the option of unlimited iterations until the user breaks the loop manually.

(this question is apparently so dumb the stack overflow wouldn’t even let me post it haha)

You can get infinite iterations without having to deal with arrays at all if you just add the numbers as you get them instead of adding them at the end.

And to end the loop, you just need to find a specific word or phrase or number that will trigger the break. It can be whatever you want it to be.

int total = 0;
int newNumber;

while(!isEnd){
   newNumber = AskForNumber();
   isEnd = checkIsEnd()

   if(!isEnd){
      total += newNumber
   }

}
1 Like

Woohoo. Got it. I totally didn’t understand += as an assignment operator. I also kept trying to put the newNumber variable outside the loop lol. That’s what keeps changing the total. What you just wrote really clarified the structure of a while loop!

		int total = 0;
		while (true)
		{
			Console.Write("Enter a number (or hit enter to exit): ");
			var newNumber = Console.ReadLine();

			if (!String.IsNullOrWhiteSpace(newNumber))

			{
				total += Convert.ToInt32(newNumber);
				continue;
			}
				break;
		}
		Console.WriteLine("The sum is: " + total);
1 Like