use*_*568 0 c++ visual-studio-2010
#include <iostream>
using namespace std;
int main()
{
int grade;
int aCount;
int bCount;
int cCount;
int dCount;
int fCount;
cout << "Enter the letter grades." << endl
<< "Enter the EOF character to end input." << endl;
while ((grade = cin.get()) != EOF)
{
switch (grade)
{
case 'A':
case 'a':
aCount++;
break;
case 'B':
case 'b':
bCount++;
break;
case 'C':
case 'c':
cCount++;
break;
case 'D':
case 'd':
dCount++;
break;
case 'F':
case 'f':
fCount++;
break;
case '\n':
case '\t':
case ' ':
break;
default:
cout << "Incorrect letter grade entered." << "Enter a new grade." << endl;
break;
}
}
cout << "\n\nNumber of students who received each letter grade:"
<< "\nA: " << aCount
<< "\nB: " << bCount
<< "\nC: " << cCount << "\nD: " << dCount << "\nF: " << fCount << endl;
system("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我的C++教科书提供的精确代码.当我通过复制这些代码然后编译它来练习这些switch语句代码时,我的Visual Studio 2010 express keep给了我一个错误,说"aCount正在使用而没有分配......"同样适用于fCount.该程序应该从键盘读取A到F的任何字母,然后增加任何被识别的字母.我认为在代码中某处应该有cin >> grade但我找不到它.顺便说一句,"cin.get()"可以作为cin >> grade?
当你声明你的变量时,尝试给它们0这样的值:
int grade = 0;
int aCount = 0;
int bCount = 0;
int cCount = 0;
int dCount = 0;
int fCount = 0;
Run Code Online (Sandbox Code Playgroud)
这将确保您在变量使用之前实际上为变量赋值.
然后尝试运行它,我敢打赌它有效!
归档时间: |
|
查看次数: |
110 次 |
最近记录: |