The assignment says "Write a program that asks the user to type in numbers. After each entry, the program should report the cumulative sum of the entries to date. The program should terminate when the user enters 0."

I have partly been succesfull I guess. But I dont know how to make the program so it doesnt quit when I press the first number. And for it to actually quit when I press 0.. I hope a friendly soul can help me out.

#include <iostream>

using namespace std;

int main()
{


  int numbers = 0;
  
  cout << "Enter numbers: ";
  cin >> numbers;                                  

for (int i=0; i<100;i=i+numbers){
              
            
cout << i;              
              

}



    cin.get();
    cin.get();
    return 0;
}

Recommended Answers

All 11 Replies

for your program you need to ask user for number in a loop(preferably do while loop)
with the condition that number!=0.
Vinayak

you can use a while loop to make it terminates when the user enters 0 also you need to read the numbers from the user in the loop

int i=0;
while(numbers!=0 && i<100)
{
  cout << "Enter numbers: ";
  cin >> numbers;
  i=i+numbers;
                                  
 

}
commented: Do NOT post working answers. It is THEIR homework to figure out, not yours -3

you can use a while loop to make it terminates when the user enters 0 also you need to read the numbers from the user in the loop

int i=0;
while(numbers!=0 && i<100)
{
  cout << "Enter numbers: ";
  cin >> numbers;
  i=i+numbers;
                                  
 

}

It is forbidden to use the && operator in this chapter, plus the program must not terminate, it must keep asking for a "cin" after it calcalutes the previous cumulative numbers... Then I shall be able to use 0 to terminate it when I want. I hope someone can help me out with this problem..

Where did the number 100 come from in the first place? Your best bet is a do/while loop.

If the number is not zero, the while condition will be true, and the program will loop again. Putting it in the do/while form guarantees that the program will prompt for the number at least once.

Where did the number 100 come from in the first place? Your best bet is a do/while loop.

If the number is not zero, the while condition will be true, and the program will loop again. Putting it in the do/while form guarantees that the program will prompt for the number at least once.

Thats is another thing.. You are right about the 100, I didnt know how to do it else... Here is the assignment description.

"Write a program that asks the user to type in numbers. After each entry, the program should report the cumulative sum of the entries to date. The program should terminate when the user enters 0."

Yes, what we have described to you will fulfill the requirements of the assignment. Which part are you still stuck on?

Member Avatar for danb737

The assignment says "Write a program that asks the user to type in numbers. After each entry, the program should report the cumulative sum of the entries to date. The program should terminate when the user enters 0."

I have partly been succesfull I guess. But I dont know how to make the program so it doesnt quit when I press the first number. And for it to actually quit when I press 0.. I hope a friendly soul can help me out.

You might want to decompose the problem in small sub-problems. So reading this assignment, you find out that you are required to do a few things :

  • get a number from the user
  • compute a cumulative sum
  • terminate the program when the user enters 0

You already achieved the first point with the usage of std::cin >> numbers. (and I'll make a note for myself to make sure the user entered a number. What would happen if the user entered a character ? But that's something you'll worry about afterwards).

For the second point, it seems you will need another variable to hold your running sum. Probably an int.

The third item is the one you're struggling with. So let's try to decompose it into even smaller problems. How do you terminate a program ? There are a few ways to do it, but it should normally terminate when you return from your main function. And how do you return from your main function, well ... by using return 0; or some other int you fancy (but the standard is to return 0 from main when the program executed succesfully).

So now we know how to termintate the program. But we have to do this under one condition : IF the user entered 0.

So leaving the runing sum on the side, you might be tempted to write something like this :

#include <iostream>

int main()
{
    int number = 0;

    std::cout << "Enter a number : ";
    std::cin >> number;

    if(number == 0) // If that's the case, we terminate the program.
    {
        return 0;
    }
                   // But if not we are now terminating the program also. Oops...
    return 0;
}

But you realize that you're missing something : some kind of a loop which would ask again the user to enter a number in case he entered something else than 0.
You tried with a for loop, but you probably saw the limitation. You made a loop of 100 iterations, but what if the user wanted to enter 105 numbers ? Well he couldn't. So what other loop are out there ? You have do-while loops or just while loops.

Let's try with do-while. Do what, while what ? Well do asking for numbers, while that number is different than 0. So now it seems you could have a loop that could infinitely loop until a certain condition is met. And what happens when that condition is met ? You terminate your program.

Hope this helps.

commented: Nice explanation. Thanks for not giving the OP the answer. :) +6

M8 the next chapter is about if statments... It have to be done with a do-while, while or for loop.. Without logical expression as &&.. !0=, ==, => things is fine, cause its in the chapter with the assignment... Thanks for the help though...

Yes, what we have described to you will fulfill the requirements of the assignment. Which part are you still stuck on?

Im stuck with making the program terminate when entered 0. And im stuck at the part so the program will ask for another cin after the program gives the first cumulative numbers. The program asking for another cin after it calculated the first have to be infinite.. Only when I press 0 then the program have to terminate.. Logical operators as &&, || are not allowed. It has to be done with Do-While loop, While-loop or for loop. Expression like ==, =>, !=numbers etc are allowed since its in the chapter for the assignment...


The assignment is from C++ Primer Plus. I find the book easy and understandble but with that said some of the assignents are tricky..

Member Avatar for danb737

M8 the next chapter is about if statments... It have to be done with a do-while, while or for loop.. Without logical expression as &&.. !0=, ==, => things is fine, cause its in the chapter with the assignment... Thanks for the help though...

And do...while is what you should use, as I wrote below my code. The code was just given as an example which shows something that will not work.

So basically you will use a

do
{
    // Insert the code you want to repeat until 0 is entered
}while(numbers != 0)

So this do...while will be wrapped around the largest part of your code, repeating it as long as the user doesn't enter 0.

If you place your cin statement at the top of the loop, you'll get it to run once entering the loop, and then it will run first and each subsequent time through the loop. To convince yourself, try putting a cout statement in the same place and see how often it gets printed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.