I want to use getch() from conio.h but g++ does not include it. Is there any substitution for getch() in g++? Thank you in advance
This is a discussion on getch() for g++? within the C++ Programming forums, part of the General Programming Boards category; I want to use getch() from conio.h but g++ does not include it. Is there any substitution for getch() in ...
I want to use getch() from conio.h but g++ does not include it. Is there any substitution for getch() in g++? Thank you in advance
Have you already tried searching this forum or the FAQ?
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
I found similar post from the forum, but I have another problem.
The code below I replaced ans=getch() with cin >> ans, but it fell into the next else(assuming user enter 'y') and printed. It was working fine and looped back to the start of the for loop if I keep ans=getch().
"Incorrect format
Would you like to try again? (Y/N)"
Shouldn't the program goes back to the start of the for loop instead of get into the else?
Code:string fx;// store the user input of a term char ans;// get user input for continue of exit bool flagf=false;// keep track if polyf and polyg are zero bool flagg=false; bool has_entered; for(;;) { system("CLS"); // Clear the console cout << "\nAddition:\n\n" << endl; cout << "Please enter each polynomial term in the format of: cx^k\n" << endl; cout << "f(x)= "; if (flagf)// Check if polyf is zero {polyf.disp_poly();}// Display polyf if not zero getline(cin, fx);// get user input of each term if (check_string(fx)==true)// if user input is corrected { has_entered=addterm(fx,polyf);// check if the term exceeded MAX if (has_entered) {} else// Exceeded MAX { cout<<"\n\nExceeded maximum degree of 19"<<endl<<endl; system("PAUSE"); continue; // loop back to the beginging of for loop } flagf=true; //Check if user want to add more terms cout << "\n\n\n\n\n\n\n\n\nWould you like to add more term(s) to f(x)? (Y/N)"; cin >> ans;// wait for user input if (ans=='N'||ans=='n')// if user entered 'N' or 'n' then break the for loop { break; } else if (ans=='Y'||ans=='y')// If user entered 'Y' or 'y' then do nothing {;} } else// User input is not corrected { //check if user want to try again cout << "\n\n\n\nIncorrect format" << endl; cout << "\n\n\n\nWould you like to try again? (Y/N)"; ans=getch();// wait for user input if (ans=='N'||ans=='n') { break; //If user entered 'N' or'n' the break the for loop } else if (ans=='Y'||ans=='y') {} } }
Last edited by jk1998; 04-25-2007 at 10:16 PM.
OK, I know why it goes to that else. Actually, it went into that else because user has to press Enter. The Enter triggered the getline(cin, fx), which then went to that else, not because of 'y'.
I still want to use getline(cin, fx). Is there a better way to fix this? Thanks
I tried to substitute cin >> ans with the following function with ans=YorN(), but it is still doing the same thing. getline(cin, fx) still picks up the Enter key.
Please help.
Code:char YorN() { char ans; cin >> ans; return ans; }
Last edited by jk1998; 04-25-2007 at 11:25 PM.
The reason it happens is because cin is line buffered on more than one level. The program doesn't even SEE the keypress until the user strikes the enter key. Even if you disabled the buffering at the iostreams level, the terminal itself is line buffered as well, at least on most platforms. To disable that, you have to make some system-specific calls.
A reasonable solution would be to use ncurses, if it is available on your system. That wraps up most of the portability issues so you don't have to deal with it.
Thanks. I found a way around it.