Home > Net >  While loop doesnt function when calculatin all positive numbers c#
While loop doesnt function when calculatin all positive numbers c#

Time:09-29

I am making a while loop. The purpose of the loop is to take all the positive numbers and calculating the average. It works most of the time but not always. Can someone help? here is the code:

double count = 0;
int sum = 0;
int input = -1;

while (input != 0)
{
    Console.Write("Enter a number: ");
    input = int.Parse(Console.ReadLine());

    if (input > 0)
    {
        sum  = input;
        count  ;
    }

    if (input < 0)
    {
        Console.Write("Enter a number: ");
        input = int.Parse(Console.ReadLine());
    }
}

if (input == 0)
{
    Console.Write($"Average of all positive numbers is: { (sum  = input) / count:0.00} ");
}

CodePudding user response:

The second if is redundant it should work just like this

while (input != 0) {
    Console.Write("Enter a number: ");
    input = int.Parse(Console.ReadLine());
    if (input > 0)
    {
        sum  = input;
        count  ;
    }
}

Your problem was when you enter a negative number it gives you the option to enter another, but then you enter a new iteration of the while loop and it asks you to enter another number. Removing the second if should fix your problem. Hope i helped :)

CodePudding user response:

This code does not throw exceptions when non numeric data is entered

    int count = 0;
    long sum = 0;
    int input;
    
    Console.Write("Enter a number: ");
    while (int.TryParse(Console.ReadLine(), out input))
    {
        Console.Write("Enter a number: ");
        if (input > 0)
        {
            sum  = input;
            count  ;
        }
    }   
    
    Console.Write($"Average of all positive numbers is: {sum} / {Math.Max(count,1.0)} = {Math.Round( sum / Math.Max(count,1.0), 2)}.");

CodePudding user response:

Please, note that application will crash on an inpup which is not an integer (say, bla-bla-bla). To solve it we can extract a method (the other purpose is to separate UI - integers inputting - from business logic - average computing):

private static int NextInteger() {
  Console.Write("Enter a number: ");

  while (!int.TryParse(Console.ReadLine(), out int result)) 
    Console.Write("Not a valid integer, please, try again: ");

  return result;
}

Then you can solve the problem with a help of for loop (input is pure loop variable - the current value we should add to sum let keep it within the loop):

// Note, that we have double for sum and count:
//   - we are protected from integer overflow: 
//     2000000000   2000000000 != 4000000000
//   - in case count = 0 (when the very first item is not positive)
//     we'll get NaN (not a number) instead of crash
double sum = 0.0;
double count = 0.0;

// we have much to write within for; let's format in another way
for (int input = NextInteger(); 
         input != 0; 
         sum  = Math.Max(input, 0.0),   count, input = NextInteger())
  ; // nothing to do, we've done all within for  

Console.Write($"Average of all positive numbers is: {sum / count:0.00} ");
  •  Tags:  
  • c#
  • Related