Jack
Jack

Reputation: 105

stuck in a while loop c

I am doing an assignment for my C++ class. In this assignment I need to write a program that reads numbers from cin and then sums them, stopping when 0 has been entered using while loops.

I have written the code and gotten the results I need. However I am stuck in a while loop that continues to reprint the results. Can anyone give me some advice to end this loop after printing the results once?

Here is my code:

int main ()
{
int i(1), sum(0);
int sum2(0);
const int max(10);

cout << "Enter numbers, one per line. Enter 0 to end:" << endl;

while (i!=0)
{
    cin >> i; 
    sum += i; //add current value of i to sum
    sum2 += 1;
}
while (i==0)
{   
    if (sum < 100) // If total is less than 100
        cout << "Thank you. The total was " << sum << endl 
                 << "The total number of inputs reads: " << sum2 << endl
                 << "The total is less than 100." << endl ;
    else // Else total is greater than 100
            cout << "Thank you. The total was " << sum << endl 
                 << "The total number of inputs reads: " << sum2 << endl 
                 << "The total is greater than 100." << endl ;
}
} //End of Int Main

Hopefully that came out alright. Sorry I am not sure how to post with the numbers on each line. I also tried changing the while (i==0) to an if statement if (i==0) but this closes the program when 0 is entered. Anyone have and helpful advice? I would appreciate it. ^_^

Update: Sorry I forgot to mention that I also have to include a loop counter that keeps track of the number of inputs, a comment that determines if the total value is less than or greater than 100, and a statement to determine the total number of inputs. That's the reason for the if else statement at the end. I have tried taking out the last while statement because I understand it is the reason for the infinite loop, but then it doesn't display the results when I do. I changed the code to this:

int main ()
{
    int i(1), sum(0);
    int sum2(0);
    const int max(10);

cout << "Enter numbers, one per line. Enter 0 to end:" << endl;

    while (i!=0)
    {
        cin >> i; 
        sum += i; //add current value of i to sum
        sum2 += 1;
    }   
if (sum < 100) // If total is less than 100
            cout << "Thank you. The total was " << sum << endl 
                     << "The total number of inputs reads: " << sum2 << endl
                     << "The total is less than 100." << endl ;
        else // Else total is greater than 100
                cout << "Thank you. The total was " << sum << endl 
                     << "The total number of inputs reads: " << sum2 << endl 
                     << "The total is greater than 100." << endl ;

} //End of Int Main

my output is supposed to be

Enter numbers, one per line. Enter 0 to end:
7
8
6
5
5
9
8
0
Thank you. The total was 48.
The total number of inputs read: 8
The total is less than 100.

Upvotes: 1

Views: 4259

Answers (3)

Chris Seymour
Chris Seymour

Reputation: 85875

Your problem is your second while loop you are looping while (i==0) but i is never changed inside the loop.

while (i==0)  // Infinite loop, i is never change inside the block. 
{   
    if (sum < 100)    // If total is less than 100
        cout << "Thank you. The total was " << sum << endl 
                 << "The total number of inputs reads: " << sum2 << endl
                 << "The total is less than 100." << endl ;
    else            // Else total is greater than 100
            cout << "Thank you. The total was " << sum << endl 
                 << "The total number of inputs reads: " << sum2 << endl 
                 << "The total is greater than 100." << endl;
}

The looping construct isn't needed at all here.

Edit: What you want is:

#include <iostream>
using namespace std;

int main(int argc, const char* argv[]) {
  int i, sum, count;

  cout << "Enter numbers, one per line. Enter 0 to end:" << endl;

  while (i!=0) {
    cin >> i; 
    sum += i;
    count++;
  }   

  cout << "Thank you. The total was " << sum << endl 
       << "The total number of inputs reads: " << count << endl;

  if (sum < 100)
    cout << "The total is less than 100." << endl;
  else
    cout << "The total is greater than 100." << endl;

  return 0;
}

Demo:

$ ./a.out 
Enter numbers, one per line. Enter 0 to end:
1000
100
10
0
Thank you. The total was 1110
The total number of inputs reads: 4
The total is greater than 100.

$ ./a.out 
Enter numbers, one per line. Enter 0 to end:
1
2
3
0
Thank you. The total was 6
The total number of inputs reads: 4
The total is less than 100.

Notice that the count includes the terminating 0 so you might want to report count-1 instead.

Upvotes: 1

Sam Axe
Sam Axe

Reputation: 33738

Remove the while (i==0). That will always be true.

Upvotes: 1

James
James

Reputation: 427

i stays stuck at 0 and never changes after you have exited your first loop. Instead of using another while loop, just print the results afterwards.

Upvotes: 2

Related Questions