473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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

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
1 2668
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

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

Similar topics

8
by: neblackcat | last post by:
Would anyone like to comment on the following idea? I was just going to offer it as a new PEP until it was suggested that I post it here for comment & consideration against PEP 308. I'm far...
2
by: Megan | last post by:
Can you write conditional VBA code that affects only one or two records on a continuous subform? I have a form with a subform on it. The parent/ child field that links the forms is CaseID. The...
3
by: suzy | last post by:
hi, how do i show/hide a user control depending on a condition. i can't write an IF statement because im referencing the user control from html. i am thinking of putting a panel control on my...
5
by: Jonathan | last post by:
Hi-- I have the following code: #include <stdio.h> char a,b; int main()
11
by: shekhardeodhar | last post by:
The program compiles properly (most of it is from k&r) but the second function (here character_count) gives wrong answer. Can someone please explain why ? #include<stdio.h> #define IN 1...
2
by: Nobody | last post by:
I have an aspx page where I have a repeater control that repeats a asp:LinkButton. When I click on one of the links, I get a post back, and the page flashes. A friend told me to look into ATLAS/AJAX,...
43
by: dev_cool | last post by:
Hello friends, I'm a beginner in C programming. One of my friends asked me to write a program in C.The purpose of the program is print 1 to n without any conditional statement, loop or jump. ...
14
by: arnuld | last post by:
i have slightly modified the programme from section 1.5.1 which takes the input frm keyboard and then prints that to the terminal. it just does not run and i am unable to understand the error...
8
by: lovecreatesbea... | last post by:
Thank you for your time. #include <stdio.h> int main(void) { int c; while ((c = getchar()) != EOF){
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.