Using cin to enter keyboard after handling redirected file input using getline

I know this question has been handled many times .. but I can’t get it to work anyway .. here I insert the code:

#include <sstream>
#include "header.h"
using namespace std;
using namespace header;

int main(){
    string parent, child, line, node;
    int choose = 0;
    tree_ptr tree_inst = new tree;
    cout << "*** Start ***" << endl;    
    while (getline(cin, line, '\n')){
        istringstream line_stream(line);
        line_stream >> parent;
        if (parent.compare("0") == 0) break;
        while (line_stream >> child) {
            if (!tree_inst->insert(parent, child)) {
                cout << "*** ERROR! ***" << endl;
                tree_inst->visit(tree_inst->root);
                cout << "*** End ***" << endl;
                return -1;
            }
        }
    }
    while (true) {
        cin >> choose;  //<== doesn't wait for the input, just loop forever
                        //    on the default statement of the switch...
        switch (choose) {
            ...
        }
    }
}

I already tried to insert cin.sync () cin.clear () cin.ignore () .. etc .. but nothing has changed!

+4
source share
1 answer

The first cycle getline()will loop forever unless you break it anyway. If this is done in a clean way, this should not be a problem at all. In fact, I could not reproduce your error.

How to analyze an error

, cin, Numberic non space input, . , :

cout << "Enter free lines of text or stop to return to the menu:";
while (getline(cin, line, '\n')) {   // This would loop foreved
    if (line == "stop")              // I added this one to exit
        break;                       // but how do you do ?
}
while (true) {                       // I get no problem here
    cout << "Cin status: failed="    // <===Add this simple diagnostic to 
         << cin.fail() << " bad="    //     check the state of the stream 
         << cin.bad() << " eof=" << cin.eof() << endl;  
    cout << "Next char in decimal is:" // <=== Add this to show 
         << cin.peek() << endl;        //      where in the stream you're hanging
    cout << "Choose:";
    cin >> choose;  
    cout << "selected: " << choose << endl;
}

, cin.clean(), , , (Ctrl+D Ctrl+Z ).

, peeked char (.. 48 57), , .

:

, (fail = 1, eof = 1), , . , , . pastebin , - "0", .

, (, yourprogramme <yourinput.txt) , , . , , . , cin .

, <fstream> ifstream cin .

, , (, yourprogramme yourinput.txt):

...
int main(int argc, char**argv)
{
    if (argc != 2) {     // if called from command line wrong arguments
        cerr << "You must provide the name of the data file as command line argument\n";
        return EXIT_FAILURE;
    }
    ifstream file(argv[1]);     // open the file for reading 
    if (!file) {                // if open failed, end the programme
        cerr << "Could not open "<<argv[1]<<"\n";
        return EXIT_FAILURE;
    }
    string line;                // here your code from before
    int choose = 0;
    cout << "*** Loading "<<argv[1]<< " ***" << endl;
                              // but in the first loop replace cin with file
    while (getline(file, line, '\n')){   
        cout << "Read: " << line<<endl;
        if (line == "0")                // Abridged version;-)
            break;
    }
    file.close();               // File is no longer needed here.  
    while (true) {              // Second loop, unchanged, using cin  
        cout << "Choose (9 to exit):";
        if (!(cin >> choose))   // avoid looping forever if problem on cin
            break;
        cout << "selected: " << choose << endl;
        if (choose == 9)
            break;  
    }
return EXIT_SUCCESS;
}
+1

All Articles