Getchar() Doesn't Work on Conditional Control Statement

Feb 22, 2010 at 5:47am
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:
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
#include <ncurses.h>
#include <iostream>
	using namespace std;
#include <stdlib.h>

void menu();

int main() {
	int stop(0);
	int choice;
	
	while (stop == 0) {
		menu();
		cout << "Input: ";
		cin >> choice;
		switch (choice) {
			case 1  : cout << "press any key ...";
				  getchar();
				  cout << endl << endl;
				  break;
			case 2  : stop = 1;
				  break;
			default : cout << "Prompt Error..." << endl << endl;
				  break;
		}
	}
	return 0;
}

void menu () {
	cout << "Menu: \n";
	cout << "1. Keep looping" << endl;
	cout << "2. Stop and quit" << endl;
}


Can somebody help me and explain why getchar() doesn't work?

Thank you,
Wind Scar
Last edited on Feb 22, 2010 at 6:53pm
Feb 22, 2010 at 7:17am
use _getch() not getchar().
Feb 22, 2010 at 7:18am
use _getch() for C++ and getch() for C, not getchar().
Feb 22, 2010 at 12:42pm
getch() is non-standard and _getch() is even less so. Avoid using conio.h as far as possible.

Let me explain why your getchar() isn't working. That is because you use cin at line 15. When you input a number a newline along with the number will also be read. So, the getchar() reads the newline and hence your program never pauses.

Precede your getchar() call by a fflush() call to flush out unwanted newlines or any other char. Like this: fflush(stdin);

That is all, and your getchar() will then work as you expect it to. However, you may want to change the "Pressy any key" to "Press ENTER".
Feb 22, 2010 at 6:31pm
@ Dear olredixsis,
I work on Linux Ubuntu Karmic Koala (9.10), so _getch() won't be recognized by its compiler (g++) because Linux doesn't have conio.h. I already mentioned it, didn't I? Anyway, thank you for trying to help me :)

@ Dear unoriginal,
Thank you for your explanation. But it still doesn't work as I expect it. I add fflush(stdin) like you said, and my code becomes like this:
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
#include <ncurses.h>
#include <iostream>
	using namespace std;
#include <stdlib.h>

void menu();

int main() {
	int stop(0);
	int choice;
	
	while (stop == 0) {
		menu();
		cout << "Input: ";
		cin >> choice;
		switch (choice) {
			case 1  : cout << "press ENTER ...";
				  fflush(stdin);
				  getchar();
				  cout << endl << endl;
				  break;
			case 2  : stop = 1;
				  break;
			default : cout << "Prompt Error..." << endl << endl;
				  break;
		}
	}
	return 0;
}

void menu () {
	cout << "Menu: \n";
	cout << "1. Keep looping" << endl;
	cout << "2. Stop and quit" << endl;
}


Are my codes written incorrectly?
Last edited on Feb 22, 2010 at 6:32pm
Feb 22, 2010 at 6:53pm
Move the fflush(stdin) to line 16 and try that, right now fflush() is only occuring when case 1 = true.
Feb 22, 2010 at 7:05pm
@ Dear Computergeek01,
Thank you for trying to help me. I already tried your advice. But it still doesn't work. I put fflush(stdin) on line 16 (below cin >> choice). My code becomes like this:
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
#include <ncurses.h>
#include <iostream>
	using namespace std;
#include <stdlib.h>

void menu();

int main() {
	int stop(0);
	int choice;
	
	while (stop == 0) {
		menu();
		cout << "Input: ";
		cin >> choice;
		fflush(stdin);
		switch (choice) {
			case 1  : cout << "press ENTER ...";
				  getchar();
				  cout << endl << endl;
				  break;
			case 2  : stop = 1;
				  break;
			default : cout << "Prompt Error..." << endl << endl;
				  break;
		}
	}
	return 0;
}

void menu () {
	cout << "Menu: \n";
	cout << "1. Keep looping" << endl;
	cout << "2. Stop and quit" << endl;
}


Did I make a mistake?
Last edited on Feb 22, 2010 at 7:05pm
Feb 22, 2010 at 10:56pm
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:
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
#include <ncurses.h>
#include <iostream>
	using namespace std;
#include <stdlib.h>

void menu();

int main() {
	int stop(0);
	int choice;
	
	while (stop == 0) {
		menu();
		cout << "Input: ";
		cin >> choice;
		switch (choice) {
			case 1  : cout << "press ENTER ...";
				  getchar();
				  cin.get();
				  cout << endl << endl;
				  break;
			case 2  : stop = 1;
				  break;
			default : cout << "Prompt Error..." << endl << endl;
				  break;
		}
	}
	return 0;
}

void menu () {
	cout << "Menu: \n";
	cout << "1. Keep looping" << endl;
	cout << "2. Stop and quit" << endl;
}


It does work! Anyway, if you have other better solutions, I'll appreciate it if you share it :)
(notice on line 18 and 19...)
Last edited on Feb 22, 2010 at 11:08pm
Feb 23, 2010 at 12:16am
Feb 23, 2010 at 12:30am
@ Dear Zhuge,

Thank you for your nice information :)
Topic archived. No new replies allowed.