Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9201179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:56:37+00:00 2026-06-17T22:56:37+00:00

I am doing an assignment for my C++ class. In this assignment I need

  • 0

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.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:56:38+00:00Added an answer on June 17, 2026 at 10:56 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a homework assignment for my summer OO class and we need
I am doing a homework assignment for my summer OO class and we need
for an assignment in my Java class I'm supposed to write a program for
Hi im doing a school assignment, and I need to convert this JAVA code
I'm doing an assignment for a class and it's fairly routine. I've done this
I'm doing another Java project for my class. In this assignment, we have to
I am doing this Java assignment for hours and stuck with this tester class
I am doing an assignment for an information systems class and the professor decided
I'm doing an assignment for my Data Structures class. we were asked to to
so we're doing this assignment at Uni and i have a serious craving to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.