使用 cin.get() 似乎无法读取我期望的字符。怎么了?
Using cin.get() doesn't seem to read the character I'm expecting. What's wrong?
所以我正在开发这个程序来执行基本任务的菜单,其中一项是判断用户输入的字符是大写字母、小写字母还是非字母。
#include <iostream>
using namespace std;
int main () {
int mi;
cout << "1) Area of Circle" << endl;
cout << "2) Character Detection" << endl;
cout << "3) Capitalization 1-3-5" << endl;
cout << "4) Binomial Roots" << endl;
cout << "0) Quit" << endl;
cin >> mi;
switch (mi) {
case 2:
{
char c;
cout << "input a character: ";
cin.get(c);
cin.ignore(); /////// unsure if using this properly
if ('a' <= c && c <= 'z') {cout << "c is lower case" << endl;}
else if ('A' <= c && c <= 'Z') {cout << "C IS UPPER CASE" << endl;}
else { cout << "C is not a letter" << endl;}
}
break;
}
return 0;
}
选择 2 并输入 字母 (或任何其他字符)后,输出始终为 "C is not a letter."
令我困惑的是,如果我将案例 2 中的内容放在一个单独的程序中,即
using namespace std;
int main () {
char c;
cout << "input a character: ";
cin.get(c);
if ('a' <= c && 'z' >= c) {cout << "c is lower case" << endl;}
else if ('A' <= c && c <= 'z') {cout << "C IS UPPER CASE" << endl;}
else { cout << "C is not a letter" << endl;}
return 0;
}
它完全按照预期的方式工作,我什至不需要 cin.ignore(),因为出于某种原因它只在 switch 语句中跳过用户输入部分。我在这里错过了什么?
我建议您使用 cin>>
而不是 cin.get()
因为 cin.get()
在每次初始化之后都会 "grab" 放入流中的换行符每次按回车。
#include <iostream>
using namespace std;
int main () {
int mi;
cout << "1) Area of Circle" << endl;
cout << "2) Character Detection" << endl;
cout << "3) Capitalization 1-3-5" << endl;
cout << "4) Binomial Roots" << endl;
cout << "0) Quit" << endl;
cin >> mi;
switch (mi) {
case 2:
{
char c;
cout << "input a character: ";
cin>>c;
cin.ignore(); /////// unsure if using this properly
if ('a' <= c && c <= 'z') {cout << "c is lower case" << endl;}
else if ('A' <= c && c <= 'Z') {cout << "C IS UPPER CASE" << endl;}
else { cout << "C is not a letter" << endl;}
}
break;
}
return 0;
}
所以我正在开发这个程序来执行基本任务的菜单,其中一项是判断用户输入的字符是大写字母、小写字母还是非字母。
#include <iostream>
using namespace std;
int main () {
int mi;
cout << "1) Area of Circle" << endl;
cout << "2) Character Detection" << endl;
cout << "3) Capitalization 1-3-5" << endl;
cout << "4) Binomial Roots" << endl;
cout << "0) Quit" << endl;
cin >> mi;
switch (mi) {
case 2:
{
char c;
cout << "input a character: ";
cin.get(c);
cin.ignore(); /////// unsure if using this properly
if ('a' <= c && c <= 'z') {cout << "c is lower case" << endl;}
else if ('A' <= c && c <= 'Z') {cout << "C IS UPPER CASE" << endl;}
else { cout << "C is not a letter" << endl;}
}
break;
}
return 0;
}
选择 2 并输入 字母 (或任何其他字符)后,输出始终为 "C is not a letter."
令我困惑的是,如果我将案例 2 中的内容放在一个单独的程序中,即
using namespace std;
int main () {
char c;
cout << "input a character: ";
cin.get(c);
if ('a' <= c && 'z' >= c) {cout << "c is lower case" << endl;}
else if ('A' <= c && c <= 'z') {cout << "C IS UPPER CASE" << endl;}
else { cout << "C is not a letter" << endl;}
return 0;
}
它完全按照预期的方式工作,我什至不需要 cin.ignore(),因为出于某种原因它只在 switch 语句中跳过用户输入部分。我在这里错过了什么?
我建议您使用 cin>>
而不是 cin.get()
因为 cin.get()
在每次初始化之后都会 "grab" 放入流中的换行符每次按回车。
#include <iostream>
using namespace std;
int main () {
int mi;
cout << "1) Area of Circle" << endl;
cout << "2) Character Detection" << endl;
cout << "3) Capitalization 1-3-5" << endl;
cout << "4) Binomial Roots" << endl;
cout << "0) Quit" << endl;
cin >> mi;
switch (mi) {
case 2:
{
char c;
cout << "input a character: ";
cin>>c;
cin.ignore(); /////// unsure if using this properly
if ('a' <= c && c <= 'z') {cout << "c is lower case" << endl;}
else if ('A' <= c && c <= 'Z') {cout << "C IS UPPER CASE" << endl;}
else { cout << "C is not a letter" << endl;}
}
break;
}
return 0;
}