I have only really been coding for a few days, though I've reading the textbook for my Intro to C++ class for two weeks. I'm having an issue with an assignment, and I feel like I'm missing something super simple, but I can't understand what I've done wrong.
The exercise calls for you to 'write a program that reads numbers from cin, and then sums them, stopping when 0 has been entered.'
The professor told us we could write it with a for loop, a while loop, or a do-while loop. I am trying to write it with a for loop.
The program compiles successfully, it allows me to enter multiple values, and it sums it correctly. It also stops on no successfully. The only thing that's wrong with it is when I enter 0, it does not stop the program. I have tried using different commands inside the for loop, such as goto, and trying to direct it to go to break; when the value entered is zero, but my knowledge is shoddy, to say the least. I've read the textbook but I don't have enough experience yet, and I don't remember everything, and I can't figure out what I'm doing wrong.
This is what the program looks like:
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
char indicator{ 'n' };
double value{};
double sum{};
for (;;)
{
cout << endl
<< "Enter a value here: ";
cin >> value;
sum += value;
cout << endl
<< "Do you want to enter another value (enter y or n)? ";
cin >> indicator;
if (('n' == indicator) || ('N' == indicator))
break;
}
cout << endl
<< "The sum of the values you entered is " << sum << "."
<< endl;
return 0;
}
Please point out my stupid mistake, I'd be grateful to learn. Thank you!