在这段代码中,我要求用户输入用一个空格分隔的输入,gradeOne space gradeTwo。然而,它并没有按预期运行,所以我在末尾添加了输出语句,以查看值是否正确存储。
如果我输入:59 95 gradeOne应该是59,temp应该是",gradeTwo应该是95,但输出显示gradeOne是59,emp是9,gradeTwo是5。发生了什么?谢谢你的帮助!
#include <iostream>
using namespace std;
int main()
{
int gradeOne, gradeTwo;
char temp;
cout<<"Please enter 2 grades, separated by a space: ";
cin>>gradeOne>>temp>>gradeTwo;
if(gradeOne < 60 && gradeTwo < 60)
cout<<"Student Failed:("<<endl;
else if(gradeOne >= 95 && gradeTwo >= 95)
cout<<"Student Graduated with Honors:)"<<endl;
else
cout<<"Student Graduated!"<<endl;
cout<<gradeOne<<endl;
cout<<gradeTwo<<endl;
cout<<temp<<endl;
return 0;
}
运算符>gt;自动跳过空间。只需更改为:
cin>>gradeOne>>gradeTwo;
您不应该需要char变量。我把它移走了,下面的就起作用了。
#include <iostream>
using namespace std;
int main()
{
int gradeOne, gradeTwo;
cout << "Please enter 2 grades";
cin >> gradeOne >> gradeTwo;
if (gradeOne < 60 && gradeTwo < 60)
cout << "Student Failed:(" << endl;
else if (gradeOne >= 95 && gradeTwo >= 95)
cout << "Student Graduated with Honors:)" << endl;
else
cout << "Student Graduated!" << endl;
cout << gradeOne << endl;
cout << gradeTwo << endl;
return 0;
}
您想使用char变量是出于特定的原因吗?