By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,341 Members | 1,395 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,341 IT Pros & Developers. It's quick & easy.

c++: getchar() doesn't work on conditional control statement

P: 2
Hello Everybody!

I am a beginner that try to learn C++ programming. I work in Linux Ubuntu Karmic Koala (9.10) environment.

I got a problem with getchar(). The function works well in a simple program. But when I use it in while (cond.) and switch (var.) , the function doesn't work, it doesn't even ask me to input character (including "\n"). Or maybe there are other reasons why getchar() doesn't work.

I am sorry if I repost the same question. But I already searched on Internet but I still can't find the answer. So I decided to ask this problem to this forum.

Here the code:
Expand|Select|Wrap|Line Numbers
  1. #include <ncurses.h>
  2. #include <iostream>
  3.     using namespace std;
  4. #include <stdlib.h>
  5.  
  6. void menu();
  7.  
  8. int main() {
  9.     int stop(0);
  10.     int choice;
  11.  
  12.     while (stop == 0) {
  13.         menu();
  14.         cout << "Input: ";
  15.         cin >> choice;
  16.         switch (choice) {
  17.             case 1  : cout << "press any key ...";
  18.                       getchar();
  19.                       cout << endl << endl;
  20.                       break;
  21.             case 2  : stop = 1;
  22.                       break;
  23.             default : cout << "Prompt Error..." << endl << endl;
  24.                       break;
  25.         }
  26.     }
  27.     return 0;
  28. }
  29.  
  30. void menu () {
  31.     cout << "Menu: \n";
  32.     cout << "1. Keep looping" << endl;
  33.     cout << "2. Stop and quit" << endl;
  34. }
  35.  
Can somebody help me and explain why getchar() doesn't work?

Thank you,
Wind Scar
Feb 22 '10 #1
Share this Question
Share on Google+
1 Reply


P: 2
Hello everybody,

Finally with some experiments, I can make the getchar() works as my expectation. By putting getchar() and cin.get() together. So, my code becomes like this:
Expand|Select|Wrap|Line Numbers
  1. #include <ncurses.h>
  2. #include <iostream>
  3.     using namespace std;
  4. #include <stdlib.h>
  5.  
  6. void menu();
  7.  
  8. int main() {
  9.     int stop(0);
  10.     int choice;
  11.  
  12.     while (stop == 0) {
  13.         menu();
  14.         cout << "Input: ";
  15.         cin >> choice;
  16.         switch (choice) {
  17.             case 1  : cout << "press ENTER ...";
  18.                   getchar();
  19.                   cin.get();
  20.                   cout << endl << endl;
  21.                   break;
  22.             case 2  : stop = 1;
  23.                   break;
  24.             default : cout << "Prompt Error..." << endl << endl;
  25.                   break;
  26.         }
  27.     }
  28.     return 0;
  29. }
  30.  
  31. void menu () {
  32.     cout << "Menu: \n";
  33.     cout << "1. Keep looping" << endl;
  34.     cout << "2. Stop and quit" << endl;
  35. }
  36.  
It does work! Anyway, if you other better solutions, I'll appreciate it if you share it :)
Feb 22 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.