C
C#2y ago
daniel1

unable to use while proparly [Answered]

.
17 Replies
daniel1
daniel12y ago
using System; namespace school_work_28 { class Program { static void Main(string[] args) { int num, sum = 0, count = 0; double avrage = 0; Console.WriteLine("enter num to end enter -1"); num = int.Parse(Console.ReadLine()); while (num != -1) { count++; sum = sum + num; } Console.WriteLine("enter num"); num = int.Parse(Console.ReadLine()); avrage = count % sum; Console.WriteLine("the avrage is=" + avrage); ; } } }
TheRanger
TheRanger2y ago
what? yeah ur while loop will run forever if the user input any number other than -1
daniel1
daniel12y ago
yeah but once i enter one number it wont let me type anything else
TheRanger
TheRanger2y ago
well ofcourse, since ur program is stuck in the while loop
daniel1
daniel12y ago
so how do i fix it
TheRanger
TheRanger2y ago
uh what do u need the while loop for
daniel1
daniel12y ago
{ count++; sum = sum + num; } this is inside the while loop so how can the whole program be stuck i got a school asigment and it goes something like this
TheRanger
TheRanger2y ago
because you never break out of the while loop ur while loop will run as long as num isnt -1
daniel1
daniel12y ago
"write a program that takes in numbers once number -1 is taken in the program shuts down the prgoram needs to caculate the avrage out of all the numbers and display it on screen"
TheRanger
TheRanger2y ago
and since you never modified num in the while loop, it will run forever
daniel1
daniel12y ago
yeah but i can only enter 1 number so how should i modify it ?
TheRanger
TheRanger2y ago
try to ask for user input in the while loop
daniel1
daniel12y ago
yeah well it works now besides one small problem the avrage is not proparly caculated
TheRanger
TheRanger2y ago
average's formula is usually like this right? sum / count
daniel1
daniel12y ago
you just fixed everything in less then 10 minutes 👍
TheRanger
TheRanger2y ago
if ur done type/close to mark this thread as answered
Accord
Accord2y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server
More Posts