2

As you know in C++ one is able to have variables assigned based on a user input via std::cin.

When using std::cin ,if I recall correctly, when there are too many inputs that what is required, then the extra inputs are ignored.

int main(){
    int a, b;
    std::cin >> a >> b;
    std::cout << a << " " << b << endl;
    return 0;
}
Input : 2 4 9 16 32

Output : 2 4

I am looking to still use the excess information from the user.How does one store the remaining inputs if one doesn't know the amount of excess inputs that the user will type?

Goroz
  • 67
  • 5
  • *There are other ways of doing it in C so this question can also apply to the C programming language.* -- And how about Java, C#, etc.? This is not `C` code, so the relevance of the `C` tag isn't there. – PaulMcKenzie Oct 22 '22 at 04:33
  • The language-agnostic answer would be to keep reading inputs until it fails. In C++, use something like `int value; std::vector extra_inputs; while (std::cin >> value) { extra_inputs.push_back(value); }`. – kotatsuyaki Oct 22 '22 at 04:37
  • 2
    @PaulMcKenzie point taken, post has been revised. – Goroz Oct 22 '22 at 04:37
  • 1
    Does this answer your question? [Reading a full line of input](https://stackoverflow.com/questions/5882872/reading-a-full-line-of-input) – Karl Knechtel Oct 22 '22 at 05:13
  • "How does one store the remaining inputs if one doesn't know the amount of excess inputs that the user will type?" **What result do you want to get**? (Or is that the question?) – Karl Knechtel Oct 22 '22 at 05:14
  • @KarlKnechtel This is more of a question of how to make these inputs available for use later on in the program, as for the application of these inputs is left to the imagination; use of inputs as elements for arrays/vectors, for doing calculations, etc. – Goroz Oct 22 '22 at 05:24
  • @KarlKnechtel also your link refers to using getline and save the excess as a string. While a bit tedious, it also works. – Goroz Oct 22 '22 at 05:27
  • Well, yes; if you put it into a string, it's available for use later, by looking at the string. It's otherwise not clear *what you actually want to happen*. – Karl Knechtel Oct 22 '22 at 05:30

1 Answers1

1

answer:

The data stream is not lost, cin still holds the unused data stream.

explain

You can understand cin like this way, it has a stream of date, and each time cin was called, it would check the target variable type and pour (or consume) the date to fill the variable until success or read delimiters or use out the data.

For you above example, cin has "1 2 3 4", and tried to fill the integers a, and b. When it done, cin still holds the data "3 4". The information is not lost.

demo of full code

#include<iostream>
int main(){
    int a, b, c, d;
    std::cin >> a >> b;
    std::cout << a << " " << b << endl;
    std::cin >> c >> d;
    std::cout << c << " " << d << endl;
    return 0;
}
  • Compile and run:

    g++ demo.cpp -Wall -o demo.out
    ./demo.out
    
  • Input and Ouput

    1 2 3 4
    1 2
    3 4 
    

P.S. There are different ways of reading input for various purpos. i.e. while loop to into variables into a container, pour the input into a stream data structure, etc. For example, uses stringstreams family data structures.

details could be find in refences like: c++ basic io

Xin Cheng
  • 1,432
  • 10
  • 17