"Skipping" Cin?

Hello, my code here seems to be skipping my cin statement.

Currently I compile with g++ then use ./a.out < file.txt to execute then it should take input from the cin statement I have under the file processing loop. Any chance someone can help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<iostream>
#include<vector>
#include<fstream>
#include<sstream>
#include<string>
#include<utility>
#include<algorithm>
#include<deque>

using namespace std;


 int main() {

      //These two vectors represent the graph
    vector<int> vertices;
    vector< pair<int, int> > edges;

    string line;
    int val;
    int start;  //Starting node for BFS
    int g_size=1; //Graph size

    //Intermediate values for processing
    int begin;
    int count = 0;
    deque<int> interm; //Intermediate deque

    //Process file
    while( getline(cin, line) ){
    
      if (count == 0){ 
        stringstream in(line);
        in >> g_size;
        count++;
      }   

      else if(count <= g_size){
        line[1] = ' ';

        stringstream in(line);

        while (in >> val)
          interm.push_back(val);

        begin = interm[0];
        interm.pop_front();
    
        vertices.push_back(begin);
    
        for (int elem : interm)
          edges.push_back(make_pair(begin, elem));
    
        interm.clear();
    
      }   
    }   
    
    cout << "Vertices: ";
    
    for (auto elem : vertices)
        cout << elem << " ";
    
    cout << endl;
    
    cout << "Edges: "; 
    for ( auto elem : edges){
        cout << '(' << elem.first << ',' << elem.second << ')';
    }   
    cout << endl;

    cout << "Enter a node to start the search: ";
    cin >> start;

    cout << "\ng_size: " << g_size << endl;
    cout << "start: " << start << endl;
}



I've tried doing a multitude of cin.ignore, cin.clear statements and nothing is working.

My output:
1
2
3
4
5
6
./a.out < test.txt
Vertices: 1 2 3 4 
Edges: (1,2)(1,3)(1,4)(2,4)(3,4)
Enter a node to start the search: 
g_size: 4
start: 6317940


Here's my input file:
4
1:2 3 4
2:4
3:4
4:

The goal the code is trying to accomplish isn't the important part, I just have no idea whats happening.
Last edited on
The input stream is permanent. If it is reading from some source, it will always be reading from that source. Once it reaches the end of the file, that's it. The input stream is dead and there's no bringing it back. You will never at any point in the program before during or after reading the file be able to interact with the user through it.

Instead, ask the user for the filename and then use std::ifstream.
If it is reading from some source, it will always be reading from that source.
Technically speaking, you can replace source on arbitrary stream, but because of less-than-ideal stream library, it is not easy to get it right.

Asking user for filename or passing it as program argument would be better.
@MiiNiPaa: I don't even think hacky unix code could read from the console if stdin is redirected to a file.
You could access terminal directly AFAIR.
That would be different for each terminal/shell though, and what about remote connections?

I know I oversimplified, but it's better to just consider stdin permanent.
Topic archived. No new replies allowed.