当我输入0到9之间的任何数字时,我希望我的代码打印“您的汽车停在A区”。它正在输入,但是程序终止而没有进入if条件(我认为)
{
int value=0;
cout<<"Enter car serial number"<<endl;
cin>>value;
if(value >= 48 && value <= 57)
{
cout<<"your car is parked in section Z"<<endl;
}
}
答案 0 :(得分:5)
您混淆获取整数和获取字符
拥有
int value ... cin >> value;
您读取了一个 int ,因此输入2将 value 设置为值2,而不是字符'2'的(ascii)代码,当然如果输入的是123 value 我设置为123等
当然可以
if(value >= 48 && value <= 57)
不是正确的,除了您输入介于48和57之间的值
做
char c;
cin >> c;
如果您输入2 c ,那么该时间将设置为'2',因此ascii为50
当我输入0到9之间的任何数字时,我希望我的代码打印“您的汽车停在A区”。
我想0表示A,1表示B等,而不是总是A
例如,您可以做:
{
int value=0;
cout<<"Enter car serial number"<<endl;
cin>>value;
if(value >= 0 && value <= 9)
{
cout<<"your car is parked in section " << *("ABCDEFGHIJ" + value) << endl; // makes no assumption concerning the alpha codes
}
}
但要检查输入是否为有效整数更好:
{
int value=0;
cout<<"Enter car serial number"<<endl;
if (!(cin>>value))
{
cerr << "invalid input" << endl;
cin.clear(); // raz error
string s;
if (! (cin >> s)) // fluxh input
..EOF do what you want...
}
else if(value >= 0 && value <= 9)
{
cout<<"your car is parked in section " << *("ABCDEFGHIJ" + value) << endl; // makes no assumption concerning the alpha codes
}
else
cerr << "invalid serial number" << endl
}
根据您的评论:
我希望我的代码打印“您的汽车在“ this”部分中,其中“ this”可以是A,B或C,具体取决于汽车的序列号。如果是1-10,则应在此部分中A.如果是11-20,则在B部分。
{
int value=0;
cout<<"Enter car serial number"<<endl;
if (!(cin>>value))
{
cerr << "invalid input" << endl;
cin.clear(); // raz error
string s;
if (! (cin >> s)) // fluxh input
..EOF do what you want...
}
else if(value >= 1 && value <= 260)
{
cout<<"your car is parked in section " << *("ABCDEFGHIJKLMNOPQRSTUVWXYZ" + (value-1)/10) << endl; // makes no assumption concerning the alpha codes
}
else
cerr << "invalid serial number" << endl
}
示例:
#include <iostream>
using namespace std;
int main()
{
for (;;) {
int value=0;
cout<<"Enter car serial number"<<endl;
if (!(cin>>value))
{
cerr << "invalid input" << endl;
cin.clear(); // raz error
string s;
if (! (cin >> s)) // fluxh input
return -1; // ..EOF do what you want...
}
else if(value >= 1 && value <= 260)
{
cout<<"your car is parked in section " << *("ABCDEFGHIJKLMNOPQRSTUVWXYZ" + (value-1)/10) << endl; // makes no assumption concerning the alpha codes
return 0;
}
else
cerr << "invalid serial number" << endl;
}
}
编译和执行:
pi@raspberrypi:/tmp $ g++ -pedantic -Wall -Wextra s.c
pi@raspberrypi:/tmp $ ./a.out
Enter car serial number
-12
invalid serial number
Enter car serial number
0
invalid serial number
Enter car serial number
a
invalid input
Enter car serial number
1
your car is parked in section A
Enter car serial number
10
your car is parked in section A
pi@raspberrypi:/tmp $ ./a.out
Enter car serial number
11
your car is parked in section B
pi@raspberrypi:/tmp $ ./a.out
Enter car serial number
54
your car is parked in section F
pi@raspberrypi:/tmp $ ./a.out
Enter car serial number
250
your car is parked in section Y
pi@raspberrypi:/tmp $ ./a.out
Enter car serial number
251
your car is parked in section Z
pi@raspberrypi:/tmp $ ./a.out
Enter car serial number
260
your car is parked in section Z