what's wrong with my code? Lookslike my code does not receive the pressed key properly.
The problem in the program is that the variable 'pilihan' is declared as an 'int' but as you wish to receive a character it should be declared as a 'char' and then later typecasted to an 'int' in the to 'toupper()' function. changed lines should be as follows: char pilihan; switch ( toupper(int(pilihan)) ){ The program should now work as you expect. Regards Kirk Garvey P.S The reason the program kept looping is because a key had been pressed but the as 'pilihan' was declared as an integer the 'cin >> pilihan;' line was expecting a number, not an alpha character, so the character is left in the input buffer and reread in the loop's next iteration... On Tuesday 28 September 2004 14:43, Prabu Subroto wrote:
Dear my friends...
I am trying to learn and practice GNU C++.
I start with this very simple program: ===== #include <iostream.h> #include <ctype.h> #include <stdlib.h>
class menu { public: int pilihan; void tampil(); };
int main(){ menu * m = new menu(); m->tampil(); return 0; }
void menu::tampil(){ for (;;){ cout << "Menu : (S)amba (P)ostfix (B)ind (Q)uit\n"; cin >> pilihan; switch ( toupper(pilihan) ){ case 'S': cout << "You are administrating Samba Server" << endl; break; case 'P': cout << "You are administrating Postfix Server" << endl; break; case 'B': cout << "You are administrating BIND Server" << endl; break; cout << "You selected Quit. Good bye!" << endl; case 'Q': exit(0); default: cout << "***No such command!***" << endl; break; } } } ===== I compiled it successfully, as below : ===== patrixlinux@patrix:~/arsip/proyek/gcc/1> ls liss liss.cpp liss.cpp~ patrixlinux@patrix:~/arsip/proyek/gcc/1> g++ -o liss liss.cpp In file included from /usr/include/g++/backward/iostream.h:31, from liss.cpp:1: /usr/include/g++/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated. patrixlinux@patrix:~/arsip/proyek/gcc/1> ./liss Menu : (S)amba (P)ostfix (B)ind (Q)uit s ==== But the problem is my program does not run as expected. It does like this below without ending: ==== Menu : (S)amba (P)ostfix (B)ind (Q)uit ***No such command!*** Menu : (S)amba (P)ostfix (B)ind (Q)uit ***No such command!*** Menu : (S)amba (P)ostfix (B)ind (Q)uit ***No such command!*** Menu : (S)amba (P)ostfix (B)ind (Q)uit ***No such command!*** Menu : (S)amba (P)ostfix (B)ind (Q)uit ***No such command!*** Menu : (S)amba (P)ostfix (B)ind (Q)uit ***No such command!*** ==== I aborted with <control>-<C>.
what's wrong with my code? Lookslike my code does not receive the pressed key properly.
Please help.
Thank you very much in advance.
__________________________________ Do you Yahoo!? Yahoo! Mail - 50x more storage than other providers! http://promotions.yahoo.com/new_mail