在我的程序中使用if、else-if和else语句的c++?(新手)
c++ with the if, else if, and else statements on my program? (newbie)
我不明白循环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;
}
问题在if
条件下的=
中。
=
是赋值运算符,而==
是等式比较运算符。
当您为int
指定一个值时,它也会计算为true,因此if条件也会得到满足。
此外,如注释中所述,将等级的数据类型从int
更改为char
。
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 我似乎对if/else的基本语句有问题:/
- 如果 else 语句不断循环
- C++ If/Else 语句被跳过
- 如果条件不相关,我应该更喜欢两个 if 语句而不是 if-else 语句吗?
- 如何将 if else 语句重写为 switch 语句
- 在 C++ 中的 if-else if- else 语句期间更改变量
- 为什么我的 if else 语句不起作用并从数组中输出正确的索引?
- 我的 If Else 语句无法在向量 (C++) 中提供最大值
- 如果 else 语句使用大写和小写字母
- 为什么无论输入如何,所有 if-else 语句都会打印?
- 多个'if'语句,不带'else'
- 嵌套的 else 语句 c++
- 为什么切换 if else 语句的顺序会导致错误?
- 如何使用 if else 语句
- if/else 语句输出由于加号或减号而未显示正确的消息
- if-else 语句仅按特定顺序工作,我不知道为什么
- "if else"被视为单个语句吗?
- 试图避免在 for 循环中出现 if-else 语句,但代码似乎有一些错误