getch() for g++?

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 ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    42

    getch() for g++?

    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

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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.*

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    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.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    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

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    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.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Quote Originally Posted by jk1998 View Post
    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.
    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.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Thanks. I found a way around it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 04:15 AM
  3. Pause a C program without getch()
    By swgh in forum C Programming
    Replies: 4
    Last Post: 02-20-2006, 10:24 AM
  4. Clearing input buffer after using getch()
    By milkydoo in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2003, 11:04 PM
  5. Problems with getch()
    By GrNxxDaY in forum C++ Programming
    Replies: 14
    Last Post: 08-12-2002, 02:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21