C++ getchar 不能正常工作
C++ getchar dont work as it should
我有这段代码,它向用户建议 select 我菜单中的选项之一:
char c;
do {
switch(c=getchar()){
case '1':
cout << "Print" << endl;
break;
case '2':
cout << "Search" << endl;
break;
case '3':
cout << "Goodbye!" << endl;
break;
default:
cout << "I have this symbol now: " << c << endl;
break;
}
} while (c != '3');
因此,它假设读取字符并将我们置于三个选项之一。确实如此。但只有在我按下 enter 之后,好吧,我可以接受它,但它也接受这些字符串作为有效选项:
- dfds2kflds、fdsf3、fds1lkfd
什么鬼?
我希望它只接受这样的字符:
- 1, 2, 3
我该如何解决?我是 C++ 的菜鸟。
使用 getche()
或 getch()
。如果你做这样的事情
c=getch();
switch(c=getch()){
case '1':
cout<<c;
cout << "Print" << endl;
break;
case '2':
cout<<c;
cout << "Search" << endl;
break;
case '3':
cout<<c;
cout << "Goodbye!" << endl;
break;
default:
break;
}
除了 1,2 和 3 之外,您不会在屏幕上看到任何其他字符
** 编辑 **
如果 conio.h 不可用,你可以试试这个:(丢弃行中的其余字符)
char c;
do {
switch(c=getchar()){
case '1':
cout << "Print" << endl;
break;
case '2':
cout << "Search" << endl;
break;
case '3':
cout << "Goodbye!" << endl;
break;
default:
cout << "I have this symbol now: " << c << endl;
break;
}
while((c=getchar())!='\n'); //ignore rest of the line
} while (c != '3');
或丢弃超过 1 个字符的输入
char c;
do {
c=getchar();
if(getchar()!='\n'){ //check if next character is newline
while(getchar()!='\n'); //if not discard rest of the line
cout<<"error"<<endl;
c=0; // for case in which the first character in input is 3 like 3dfdf the loop will end unless you change c to something else like 0
continue;
}
switch(c){
case '1':
cout << "Print" << endl;
break;
case '2':
cout << "Search" << endl;
break;
case '3':
cout << "Goodbye!" << endl;
break;
default:
cout << "I have this symbol now: " << c << endl;
break;
}
} while (c != '3');
我有这段代码,它向用户建议 select 我菜单中的选项之一:
char c;
do {
switch(c=getchar()){
case '1':
cout << "Print" << endl;
break;
case '2':
cout << "Search" << endl;
break;
case '3':
cout << "Goodbye!" << endl;
break;
default:
cout << "I have this symbol now: " << c << endl;
break;
}
} while (c != '3');
因此,它假设读取字符并将我们置于三个选项之一。确实如此。但只有在我按下 enter 之后,好吧,我可以接受它,但它也接受这些字符串作为有效选项:
- dfds2kflds、fdsf3、fds1lkfd
什么鬼? 我希望它只接受这样的字符:
- 1, 2, 3 我该如何解决?我是 C++ 的菜鸟。
使用 getche()
或 getch()
。如果你做这样的事情
c=getch();
switch(c=getch()){
case '1':
cout<<c;
cout << "Print" << endl;
break;
case '2':
cout<<c;
cout << "Search" << endl;
break;
case '3':
cout<<c;
cout << "Goodbye!" << endl;
break;
default:
break;
}
除了 1,2 和 3 之外,您不会在屏幕上看到任何其他字符
** 编辑 **
如果 conio.h 不可用,你可以试试这个:(丢弃行中的其余字符)
char c;
do {
switch(c=getchar()){
case '1':
cout << "Print" << endl;
break;
case '2':
cout << "Search" << endl;
break;
case '3':
cout << "Goodbye!" << endl;
break;
default:
cout << "I have this symbol now: " << c << endl;
break;
}
while((c=getchar())!='\n'); //ignore rest of the line
} while (c != '3');
或丢弃超过 1 个字符的输入
char c;
do {
c=getchar();
if(getchar()!='\n'){ //check if next character is newline
while(getchar()!='\n'); //if not discard rest of the line
cout<<"error"<<endl;
c=0; // for case in which the first character in input is 3 like 3dfdf the loop will end unless you change c to something else like 0
continue;
}
switch(c){
case '1':
cout << "Print" << endl;
break;
case '2':
cout << "Search" << endl;
break;
case '3':
cout << "Goodbye!" << endl;
break;
default:
cout << "I have this symbol now: " << c << endl;
break;
}
} while (c != '3');