Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;在我的程序中使用if、elseif和else语句?(新手)_C++_If Statement - Fatal编程技术网

C++ c++;在我的程序中使用if、elseif和else语句?(新手)

C++ c++;在我的程序中使用if、elseif和else语句?(新手),c++,if-statement,C++,If Statement,我不明白循环if语句或者为什么我的程序不能正常工作。该程序将根据您的输入反映您的成绩。有人知道为什么它不起作用吗 #include <iostream> using namespace std; int main() { int grade; cout << "What grade did you earn in Programming I ?" << endl; cin >> grade; if (grade =

我不明白循环if语句或者为什么我的程序不能正常工作。该程序将根据您的输入反映您的成绩。有人知道为什么它不起作用吗

#include <iostream>
using namespace std;
int main()
{
    int grade;
    cout << "What grade did you earn in Programming I ?" << endl;
    cin >> grade;
    if (grade = 'A')
        cout << "an A - excellent work !" << endl;
    if (grade = 'B')
        cout << "you got a B - good job" << endl;
    else if (grade = 'C')
        cout << "earning a C is satisfactory" << endl;
    else if (grade = 'D')
        cout << "while D is passing, there is a problem" << endl;
    else if (grade = 'F')
        cout << "you failed - better luck next time" << endl;
    else
        cout << "You did not enter an A,B,C,D or F" << endl;
    return 0;
}
#包括
使用名称空间std;
int main()
{
国际等级;
cout分级;
如果(等级='A')

cout问题出现在
if
条件下的
=

=
是赋值运算符,而
=
是相等比较运算符

当您为
int
赋值时,它的计算结果也为true,因此也满足if条件


此外,如评论中所述,将分数的数据类型从
int
更改为
char

在任何情况下将
=
更改为
=
也将分数的数据类型从
int
更改为
char
。阅读一本好的初学者书籍。或者,实际上,任何初学者书籍。此外,请不要养成调用if语句循环。它们是条件语句,与循环没有任何关系。并且您需要在“
B
”分支前面使用
else if
,而不仅仅是
if