1

I wanted to break the loop when the user doesn't want to add anymore:

#include<iostream> using namespace std; int main() { int i = 0, a = 0, h = 0; cout << "Enter numbers to be added:\n "; for(i=0; ??; i++) { cout << "\n" << h << " + "; cin >> a; h = h+a; } return 0; } 
4
  • What is the user supposed to do when they don't want to add anymore? Commented Aug 12, 2015 at 21:56
  • 1
    en.wikipedia.org/wiki/Sentinel_value Commented Aug 12, 2015 at 21:57
  • @DJ McMayhem : Thats the question Commented Aug 12, 2015 at 22:00
  • @PrabhatSingh You can't do that with that code. Suppose you could, then pressing enter would put some integer A0 in a, which means that manually entering this integer would stop the loop, I can't see how it is desirable (besides, cin >> a; doesn't even return when you only press enter on my machine... how do you want to test a for a line end if the function blocks until you've entered something relevant?). I suggest you use Kay's answer (or RyanP's answer which is fine too) which has the intended behaviour (and even checks for invalid values). I don't get why they were downvoted. Commented Aug 12, 2015 at 23:05

3 Answers 3

2

Use std::getline to read an input line and exit the loop when the line is empty.

#include<iostream> #include <sstream> int main() { int a = 0, h = 0; std::cout << "Enter numbers to be added:\n "; std::string line; std::cout << "\n" << h << " + "; while (std::getline(std::cin, line) && // input is good line.length() > 0) // line not empty { std::stringstream linestr(line); while (linestr >> a)// recommend better checking here. Look up std::strtol { h = h+a; std::cout << "\n" << h << " + "; } } return 0; } 

And output:

Enter numbers to be added: 0 + 1 2 3 4 5 6 7 8 9 1 + 3 + 6 + 10 + 15 + 21 + 28 + 36 + 45 + 

Note that this allows multiple entries per line and looks pretty ugly, so OP is probably more interested in:

#include<iostream> int main() { long a = 0, h = 0; std::cout << "Enter numbers to be added:\n "; std::string line; std::cout << "\n" << h << " + "; while (std::getline(std::cin, line) && // input is good line.length() > 0) // line not empty { char * endp; // will be updated with the character in line that wasn't a digit a = std::strtol(line.c_str(), &endp, 10); if (*endp == '\0') // if last character inspected was the end of the string // warning: Does not catch ridiculously large numbers { h = h+a; } else { std::cout << "Very funny, wise guy. Try again." << std::endl; } std::cout << "\n" << h << " + "; } return 0; } 

Output

Enter numbers to be added: 0 + 1 1 + 1 2 3 4 Very funny, wise guy. Try again. 1 + 2 3 + 44444 44447 + jsdf;jasdklfjasdklf Very funny, wise guy. Try again. 44447 + 9999999999999999999999 -2147439202 + 
Sign up to request clarification or add additional context in comments.

Comments

0

It is easier to use a sentinel value that you can check against, something like this:

#include<iostream> #include<string> using namespace std; int main() { int sum = 0; string userInput; while(true) { cout<<"Enter number to be added ('q' to quit): "; cin >> userInput; if( (userInput == "q") || (userInput == "Q") ) { break; } try { sum += stoi( userInput ); } catch( const std::invalid_argument& e ) { cerr << "Invalid input \"" << userInput << "\" received!" << endl; return EXIT_FAILURE; } } cout << "Sum: " << sum << endl; return EXIT_SUCCESS; } 

Comments

0

std::getline(std::istream&, std::string&) happily gives all lines including empty lines:

#include <iostream> #include <string> int main() { long long accumulator = 0; while (true) { // read a (possibly empty) line: std::string buf; if (!std::getline(std::cin, buf)) { std::cerr << "The input stream is broken." << std::endl; break; } // was the entered line empty? if (buf.empty()) { std::cerr << "You entered a blank line" << std::endl; break; } // convert string to integer std::size_t pos; long long summand; try { summand = std::stoll(buf, &pos, 10); } catch (std::invalid_argument &) { std::cerr << "Not an integer: " << buf << std::endl; continue; } catch (std::out_of_range &) { std::cerr << "Out of range: " << buf << std::endl; continue; } if (pos != buf.size()) { std::cerr << "Not an integer on its own: " << buf << std::endl; continue; } // do something with the data: accumulator += summand; } std::cout << "accumulator = " << accumulator << std::endl; return 0; } 

4 Comments

@PrabhatSingh (might be the downvoter) I didn't see this answer (deleted my duplicate answer), why is this downvoted? It's perfectly fine. Can we have an explanation from the downvoter please?
@Caninonos Not the downvoter, but the OP wishes to exit on an empty line, ergo not a solution. The use of exceptions is a good improvement. I couldn't get std::invalid_argument to catch all I wanted, but I didn't see out of range getting thrown when I tried it.
@user4581301 Unless I'm mistaken, he does exit the loop on an empty line here: if (buf.empty()) { std::cerr << "You entered a blank line" << std::endl; break; } hence my incomprehension. (btw checking whether multiple numbers are entered in your answer is nice, I haven't thought of that case)
@Caninonos He does indeed. My apologies for missing that. I am at a loss, then. I'll have to poke a bit deeper when I've the time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.