C++ 条件正确但不起作用
C++ condition is right but does not work
我正在尝试构建一个简单的程序来询问成绩,无论是数字成绩还是一般加权平均成绩,例如分别为 75、1.5。当用户输入 if 条件 中规定的非数字 a 时,它会打印错误,如 else 条件 中所述。问题是当我输入 3 位、4 位、5 位数字等时,我得到一个错误,应该是 Grade is not within the scope as stated in the nested if(else 在 if 条件内)。当我输入 0 时也会发生同样的事情。但好处是,当我输入负数或整数时,它可以正常工作。请问我的病情有什么问题吗? (提前发送)
#include <iostream>
using namespace std;
int main ()
{
double grade;
cout<<"Enter Grade or GWA: ";
cin>>grade;
if (grade!=grade>='a'&&grade<='z'||grade!=grade>='A'&&grade<='Z')
/*The first If condition states that the user will get an error if
input a non-numerical integer or number.*/
{
if (grade<=5&&grade>=1)
{
if (grade>2.5)
cout<<"Failed"<<endl;
else
cout<<"Passed"<<endl;
}
else if (grade<75&&grade>=0)
cout << "Failed" << endl;
else if (grade>=75&&grade<=100)
cout << "Passed" << endl;
else
cout<<"Grade is not within the scope!"<<endl;
}
else
cout<<"Error!"<<endl;
cin.get();
return 0;
}
任何大于 122 的数字都会失败,因为 z 的 ascii 是 122,所以 grade!=grade>='a'&&grade<='z'
会失败。
并且grade!=grade>='A'&&grade<='Z'
也会失败(Z ascii 为 90)
顺便说一句,由于成绩是双精度的,因此如果用户输入非法 number.Just 检查 if(grade !=0)
,则成绩将为零
好的,这里有一些提示:
grade!=grade>='a'&&grade<='z'
这只是行不通。 grade<='z'
很好(语法正确)。但是,grade!=grade>='a'
不是。例如;
int a =10;
int b=10;
int c=10;
if (a==b==c){
cout<< "GOOD";
}
else{
cout<< "NOT GOOD";
}
如你所见,你会一直变得不好。除非你写 (a==b && b==c)
.
阅读@melpomene 的评论后,我研究了更多应该使用的数据类型,float、double 或 long double。我能说的是,这一切都取决于。然而,几乎每个人都同意的要点是:
- 双精度数是浮点类型的常见用途。
- double 在现代硬件中优于 float。
- 双精度浮点数具有相同或更好的精度。 float(小数点后6位精度) doubles(小数点后15位精度).
- 双倍存储大小 8 字节与浮点存储大小 4 字节。
因此,这一切都取决于,在这种情况下,我会同意双打 :) 但同样,这完全取决于条件和我们想要达到的目标;性能?,精度?,space?。
此外,您要求的是双精度值,对吗?范围是从 0 到 100 对吗?如果是这样,请检查是否 (grade >= 0 && grade <= 100)
.
那么,如果用户输入1,你怎么说它是在0-100还是0-5的范围内。你看你也必须记住这一点,否则它只会进入它找到的第一个 if 语句。哦,是的,我得到了 4/100,但是因为我正在接受我通过的 0 到 5 等级的检查:)
我正在尝试构建一个简单的程序来询问成绩,无论是数字成绩还是一般加权平均成绩,例如分别为 75、1.5。当用户输入 if 条件 中规定的非数字 a 时,它会打印错误,如 else 条件 中所述。问题是当我输入 3 位、4 位、5 位数字等时,我得到一个错误,应该是 Grade is not within the scope as stated in the nested if(else 在 if 条件内)。当我输入 0 时也会发生同样的事情。但好处是,当我输入负数或整数时,它可以正常工作。请问我的病情有什么问题吗? (提前发送)
#include <iostream>
using namespace std;
int main ()
{
double grade;
cout<<"Enter Grade or GWA: ";
cin>>grade;
if (grade!=grade>='a'&&grade<='z'||grade!=grade>='A'&&grade<='Z')
/*The first If condition states that the user will get an error if
input a non-numerical integer or number.*/
{
if (grade<=5&&grade>=1)
{
if (grade>2.5)
cout<<"Failed"<<endl;
else
cout<<"Passed"<<endl;
}
else if (grade<75&&grade>=0)
cout << "Failed" << endl;
else if (grade>=75&&grade<=100)
cout << "Passed" << endl;
else
cout<<"Grade is not within the scope!"<<endl;
}
else
cout<<"Error!"<<endl;
cin.get();
return 0;
}
任何大于 122 的数字都会失败,因为 z 的 ascii 是 122,所以 grade!=grade>='a'&&grade<='z'
会失败。
并且grade!=grade>='A'&&grade<='Z'
也会失败(Z ascii 为 90)
顺便说一句,由于成绩是双精度的,因此如果用户输入非法 number.Just 检查 if(grade !=0)
好的,这里有一些提示:
grade!=grade>='a'&&grade<='z'
这只是行不通。 grade<='z'
很好(语法正确)。但是,grade!=grade>='a'
不是。例如;
int a =10;
int b=10;
int c=10;
if (a==b==c){
cout<< "GOOD";
}
else{
cout<< "NOT GOOD";
}
如你所见,你会一直变得不好。除非你写 (a==b && b==c)
.
阅读@melpomene 的评论后,我研究了更多应该使用的数据类型,float、double 或 long double。我能说的是,这一切都取决于。然而,几乎每个人都同意的要点是:
- 双精度数是浮点类型的常见用途。
- double 在现代硬件中优于 float。
- 双精度浮点数具有相同或更好的精度。 float(小数点后6位精度) doubles(小数点后15位精度).
- 双倍存储大小 8 字节与浮点存储大小 4 字节。
因此,这一切都取决于,在这种情况下,我会同意双打 :) 但同样,这完全取决于条件和我们想要达到的目标;性能?,精度?,space?。
此外,您要求的是双精度值,对吗?范围是从 0 到 100 对吗?如果是这样,请检查是否 (grade >= 0 && grade <= 100)
.
那么,如果用户输入1,你怎么说它是在0-100还是0-5的范围内。你看你也必须记住这一点,否则它只会进入它找到的第一个 if 语句。哦,是的,我得到了 4/100,但是因为我正在接受我通过的 0 到 5 等级的检查:)