Getting specific amount of values from the line

As title says, I need to get the specific amount of values from the input file’s line. The amount will be input, so program will know it. I have the following line to get values of 2 elements on line. It checks if both a and b gets values successfully or not. How can I implement it for i element instead of 2? As I said, i value will be user input.

if (!(iss >> a >> b)) { break; }
Last edited on
1
2
3
4
5
6
7
8
std::vector<int> v;

for( int idx = 0; idx < i; idx++ )
{
    int tmp;
    if(!(std::cin >> tmp)) break;
    v.push_back( tmp );
}
1
2
3
4
5
6
7
8
std::vector<int> v;

for( int idx = 0; idx < i; idx++ )
{
    int tmp;
    if(!(std::cin >> tmp)) break;
    v.push_back( tmp );
}


It didn't work. I replaced this loop with my line, but it never gets the values. Program acted like it's infinite loop.
Please show more context.
Please show more context.

Imagine my first input file is this:
1 2 3 4 5


And my second input file is this:
1 2 3


If I use the line if (!(iss >> a >> b >> c >> d >> e)) { break; } I can get 1, 2, 3, 4 and 5 from my first input file. And if I use if (!(iss >> a >> b >> c)) { break; } I can get 1, 2 and 3 from my second input file. I want this line to be generated according to input. (Don't worry about the input, I already handled that.). For example if I read my first input file, input will be 5 and I want to run if (!(iss >> a >> b >> c >> d >> e)) { break; }. And if I read my second input file, input will be 3 and I want to run if (!(iss >> a >> b >> c)) { break; }. So I need a loop that generates a, b, c, d, e, ... , z according to input size and implement them. How can I do this?
Last edited on
I meant, the code that you posted doesn't show the issue that you describe. The code that you posted is not an infinite loop, although it's possible it's a really long loop if the value if i is very high. Therefore, you should show more code to let us reproduce the issue.
The code that you posted is not an infinite loop, although it's possible it's a really long loop if the value if i is very high.

Yes, that for loop is not an infinite loop, but when I run the code, it never ends so I need to terminate it. The i values that I work is 5-6 so I don't think it should take this much time. It instantly gives the expected result when I don't use the loop. But if I want this to work with any i value I know I have to use a loop.

Therefore, you should show more code to let us reproduce the issue.

What I basically do is; I get values from each line according to input value and store them in a vector.
For example, I get values from
1 2 3 4 5
6 7 8 9 10

and put each into a vector using push_back(). My vector will have 1,2,3,4,5,6,7,8,9,10 at the end. It is really simple operation, I don't know why that for loop doesn't work.
Topic archived. No new replies allowed.