Reading lines of input

Jan 30, 2019 at 10:28pm
I got a problem. I need to allow a user to make one input and it assigns the numbers to variables. Afterwards, it computes the sum. So if I input "1 2 3", it will output 6. How do i go about assigning the variables the values if cin only can affect one?
Jan 30, 2019 at 10:39pm
One possible option is to obtain the input as one string, and then split it into numbers delimited by the whitespace between them.

If you don't wish to use stringstreams, you can parse the input string yourself (character by character).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    std::string input;
    std::getline(std::cin, input); // get whole line entered as input
    
    std::istringstream iss(input); // convert input to a stringstream
    int num;
    while (iss >> num) // get each number out of the input string's stream
    {
        std::cout << "You entered: " << num << '\n';
        // TODO: Keep track of a "sum" variable.
    }
}
Last edited on Jan 30, 2019 at 10:41pm
Jan 30, 2019 at 10:41pm
You can use stringstreams to achieve this operation.

1
2
3
4
5
6
7
std::string line;                        
int a, b, c, sum;

std::getline(std::cin, line);
std::stringstream ss(line);                     
ss >> a >> b >> c;
sum = a + b + c;

Jan 30, 2019 at 10:41pm
Hekko Bulbasheen,

For the future:

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


A good place to start is http://www.cplusplus.com/doc/tutorial/basic_io/

With out any code of yours there is nothing to help you with. There is nothing to say if you did things right or wrong.

Work something up and post it so that there is something to work with.

Andy
Jan 31, 2019 at 4:32am
Here: cin >> var1 >> var2 >> var3;

It's that simple. I donno why everybody above me wants to use stringstream.

Your program:
1
2
3
4
int var2, var2, var3;
cin >> var1 >> var2 >> var3;

cout << "Sum:" << var1+var2+var3;

Note that the program fails when you type non digits. 🙂
Do read handy andys post. Though I think it was a little harsh to ask you to work this out yourself.
Last edited on Jan 31, 2019 at 4:38am
Jan 31, 2019 at 7:01am
closed account (E0p9LyTq)
Grime wrote:
I donno why everybody above me wants to use stringstream.

You answered your own head-scratching question.

Note that the program fails when you type non digits.

Using stringstream the program can recover from entering non-numeric data.

A bit more work required for that bit of error checking, but IMO worth every extra bit of effort.
Jan 31, 2019 at 7:28am
Since its certain to be three inputs there's no need to use stringstream

You could either take input with cin and three string and convert the string or you could take input with three integers and check for fail state, which is what I would have done.

But more importantly cin is an easier and shorter approach. Consider that OP is probably a newbie.

Then it's more important to know about cin than stringstream.
Jan 31, 2019 at 7:32am
Why is it certain to be three inputs?
Jan 31, 2019 at 7:37am
Because it is an assignment
Jan 31, 2019 at 7:59am
Lol I found that really funny XD. I love you for that reply bulba!

But you're right last chance. Bulba sheen i don't want you to go wrong because of me. So use cin only if it is guaranteed that there are only 3 inputs.

Otherwise If you know only that the number input is terminated with newline then you must use get line (and hence stream) Because cin doesn't read whitespace so you've got no choice. 👍
Topic archived. No new replies allowed.