I cannot understand this exercise:
"Write a program that computes a running sum of inputs from the user, terminating when the user gives an input value of 0"
I do not understand what I'm supposed to do here.
This is a discussion on C++ - loops within the C++ Programming forums, part of the General Programming Boards category; I cannot understand this exercise: "Write a program that computes a running sum of inputs from the user, terminating when ...
I cannot understand this exercise:
"Write a program that computes a running sum of inputs from the user, terminating when the user gives an input value of 0"
I do not understand what I'm supposed to do here.
It sounds like the general algorithm is supposed to be like this:
1. Read number from the user
2. If that number is zero, exit. Otherwise, add it to a sum variable and go back to #1.
Ok, I will try it.
This should get you started in the right direction
Code:#include <iostream> using namespace std; int main() { int num = 0; int num2 = 0; int total = 0; do { cout << "Enter a number: " << endl; cin >> num; cout << "Enter a number: " << endl; cin >> num2; total += num + num2; } while(num || num2 != 0); cout << total << endl; return 0; }
What are you expecting this line to do?Code:while(num || num2 != 0);
And this could have been done with a single input variable.
Note I said "This should get you started in the right direction" which implies I put it there knowingly, we could argue for hours how this program could be done better and more efficiently but that would be counter productive. But I accept your input yes, it could be done much better.![]()
Just a little tip adding system("pause") will increase your file size substantially, you can replace it with something like this
Side noteCode:cout << "Press enter to exit: " char x; cin x;nce yes I know I could use better code than this or read for a specific key, but this will suffice.
The problem with system is not that it increases size, but that it is Evil™.
Here's why.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
You didn't answer my question, though.
And how is this the "right direction"? This specific program might get lucky, but if we change the sentinel value to, say, -1, you have at least two major issues.
Your sentinel value gets factored into the sum, and the syntax of your "while()" loop is wrong.
IMHO, examples with incorrect syntax is not the right direction. But that's just my opinion - you're free to disagree.