1

I'm doing exercise 1.16 (p. 17) in C++ primer. The exercise is to get a set of numbers from user input (std::cin) and add them and output (std::cout). My loop won't terminate with std::cin >> input as the condition. Once it runs out of numbers to add it reads from the keyboard again.

I'm a little familiar with C and in that language I believe we can do something like while (input != \n), but I don't know what the character at the end of the buffer would be for std::cin. What is the terminating/last value in std::cin and why is it not working in my condition?

#include<iostream>

int main()
{
    int x = 0;
    int sum = 0;
    std::cout << "Enter a set of integers: " << std::endl;
    while (std::cin >> x)
        sum += x;
    
    std::cout << "The sum of those integers is " << sum << std::endl;

    std::cin.get();
    std::cin.get();
    return 0;
}
Spellbinder2050
  • 644
  • 3
  • 13
  • 3
    Try Ctrl+Z on Windows or Ctrl+D on Linux. – MikeCAT Mar 27 '21 at 23:14
  • 3
    Entering something that is _not_ an integer will probably also work. – Ted Lyngmo Mar 27 '21 at 23:23
  • How do you actually want the program to decide that there are no more numbers? Are you looking for a signal from the user that no more will be typed, ever? Or do you want to take a *line* of input and extract the integers from that? Or something else? – Karl Knechtel Mar 27 '21 at 23:24
  • The exercise is to read inputs from the user using std::cin (only input learned by this point in the book). I assumed that the new character line returns -1 like in c, and would terminate the loop, but clearly that's wrong here. I would like it to terminate on reading the newline from the input buffer. – Spellbinder2050 Mar 27 '21 at 23:27
  • 1
    this may help. https://stackoverflow.com/questions/38978266/how-can-stdcin-return-a-bool-and-itself-at-the-same-time – cnux9 Mar 27 '21 at 23:30

2 Answers2

0

If you still want to do as many numbers as the user inputs, you could input as a string and check if the input is a number like so (may need some tweaks):

int main()
{
    int x = 0;
    int sum = 0;
    std::string input;
    std::cout << "Enter a set of integers: " << std::endl;
    while (std::cin >> input){
        if( isdigit(input) ){ //may need to be "if( std::isdigit(input) ){"
            x = std::stoi(input);
            sum += x;
        }
        else{
            break;
        }
    }
    std::cout << "The sum of those integers is " << sum << std::endl;

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

I would like it to terminate on reading the newline from the input buffer

In that case, I'd use std::getline to read the line as a std::string and then convert that string to an integer using one of the conversion functions, like std::stoi:

#include <string>

// ...

    //            exit the for loop if an empty line is entered
    for(std::string line; std::getline(std::cin, line) && not line.empty();) {
        try {
            x = std::stoi(line);
            sum += x;
        }
        catch(const std::exception& ex) {
            // stoi can throw std::invalid_argument and std::out_of_range
            std::cerr << line << ": " << ex.what() << '\n';
        }
    }

If you would like to be able to enter multiple numbers on each line, you could put the line you've read with std::getline in a std::istringstream and extract the numbers from that instead:

#include <sstream>
#include <string>

// ...

    for(std::string line; std::getline(std::cin, line) && not line.empty();) {
        std::istringstream is(line);
        while(is >> x) {
            sum += x;
        }
    }
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • So in other words, there's no way of doing this exercise I'm referring to without newer knowledge or without not accounting for specific cases? Either way, your answer is the best. Thank you. – Spellbinder2050 Mar 28 '21 at 00:43
  • 1
    @Spellbinder2050 You're welcome! I don't have the book myself so I'm not sure exactly what it says, but reading lines as strings and then using some function or `istringstream` to extract values is a pretty common technique to solve these kinds of problems. – Ted Lyngmo Mar 28 '21 at 00:50