我的作业有问题,要求我编写一个获取用户名的程序,10个单独的数字等级,然后返回这些数字等级,以及字母等级等值,数字平均值和字母平均值给用户。我收到错误C4700,未初始化的局部变量。关于我哪里出错的任何提示?
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main (){
//Define all variables for grades, average, username, and letter grades.
int grade1,
grade2,
grade3,
grade4,
grade5,
grade6,
grade7,
grade8,
grade9,
grade10;
double average = (grade1 + grade2 + grade3 + grade4 + grade5 + grade6 + grade7 + grade8 + grade9 + grade10) / 10;
string username;
string letter1,
letter2,
letter3,
letter4,
letter5,
letter6,
letter7,
letter8,
letter9,
letter10,
letteravg;
//Define definitions for letter grades for comparison
const int A_Score = 90,
B_Score = 80,
C_Score = 70,
D_Score = 60;
cout << "What is your user name without spaces? " << endl;
cin >> username;
cout << "What is the first grade? " << endl;
cin >> grade1;
cout << "What is the second grade? " << endl;
cin >> grade2;
cout << "What is the third grade? " << endl;
cin >> grade3;
cout << "What is the fourth grade? " << endl;
cin >> grade4;
cout << "What is the fifth grade? " << endl;
cin >> grade5;
cout << "What is the sixth grade? " << endl;
cin >> grade6;
cout << "What is the seventh grade? " << endl;
cin >> grade7;
cout << "What is the eighth grade? " << endl;
cin >> grade8;
cout << "What is the ninth grade? " << endl;
cin >> grade9;
cout << "What is the ninth grade? " << endl;
cin >> grade10;
if ( grade1 >= A_Score )
letter1 = "A";
else if ( grade1 >= B_Score )
letter1 = "B";
else if ( grade1 >= C_Score )
letter1 = "C";
else if ( grade1 >= D_Score )
letter1 = "D";
else if ( grade1 >= 0 )
letter1 = "F";
if ( grade2 >= A_Score )
letter2 = "A";
else if ( grade2 >= B_Score )
letter2 = "B";
else if ( grade2 >= C_Score )
letter2 = "C";
else if ( grade2 >= D_Score )
letter2 = "D";
else if ( grade2 >= 0 )
letter2 = "F";
if ( grade3 >= A_Score )
letter3 = "A";
else if ( grade3 >= B_Score )
letter3 = "B";
else if ( grade3 >= C_Score )
letter3 = "C";
else if ( grade3 >= D_Score )
letter3 = "D";
else if ( grade3 >= 0 )
letter3 = "F";
if ( grade4 >= A_Score )
letter4 = "A";
else if ( grade4 >= B_Score )
letter4 = "B";
else if ( grade4 >= C_Score )
letter4 = "C";
else if ( grade4 >= D_Score )
letter4 = "D";
else if ( grade4 >= 0 )
letter4 = "F";
if ( grade5 >= A_Score )
letter5 = "A";
else if ( grade5 >= B_Score )
letter5 = "B";
else if ( grade5 >= C_Score )
letter5 = "C";
else if ( grade5 >= D_Score )
letter5 = "D";
else if ( grade5 >= 0 )
letter5 = "F";
if ( grade6 >= A_Score )
letter6 = "A";
else if ( grade6 >= B_Score )
letter6 = "B";
else if ( grade6 >= C_Score )
letter6 = "C";
else if ( grade6 >= D_Score )
letter6 = "D";
else if ( grade6 >= 0 )
letter6 = "F";
if ( grade7 >= A_Score )
letter7 = "A";
else if ( grade7 >= B_Score )
letter7 = "B";
else if ( grade7 >= C_Score )
letter7 = "C";
else if ( grade7 >= D_Score )
letter7 = "D";
else if ( grade7 >= 0 )
letter7 = "F";
if ( grade8 >= A_Score )
letter8 = "A";
else if ( grade8 >= B_Score )
letter8 = "B";
else if ( grade8 >= C_Score )
letter8 = "C";
else if ( grade8 >= D_Score )
letter8 = "D";
else if ( grade8 >= 0 )
letter8 = "F";
if ( grade9 >= A_Score )
letter9 = "A";
else if ( grade9 >= B_Score )
letter9 = "B";
else if ( grade9 >= C_Score )
letter9 = "C";
else if ( grade9 >= D_Score )
letter9 = "D";
else if ( grade9 >= 0 )
letter9 = "F";
if ( grade10 >= A_Score )
letter10 = "A";
else if ( grade10 >= B_Score )
letter10 = "B";
else if ( grade10 >= C_Score )
letter10 = "C";
else if ( grade10 >= D_Score )
letter10 = "D";
else if ( grade10 >= 0 )
letter10 = "F";
if ( average >= A_Score )
letteravg = "A";
else if ( average >= B_Score )
letteravg = "B";
else if ( average >= C_Score )
letteravg = "C";
else if ( average >= D_Score )
letteravg = "D";
else if ( average >= 0 )
letteravg = "F";
cout << " Grade = " << grade1 << " = " << letter1 << endl;
cout << " Grade = " << grade2 << " = " << letter2 << endl;
cout << " Grade = " << grade3 << " = " << letter3 << endl;
cout << " Grade = " << grade4 << " = " << letter4 << endl;
cout << " Grade = " << grade5 << " = " << letter5 << endl;
cout << " Grade = " << grade6 << " = " << letter6 << endl;
cout << " Grade = " << grade7 << " = " << letter7 << endl;
cout << " Grade = " << grade8 << " = " << letter8 << endl;
cout << " Grade = " << grade9 << " = " << letter9 << endl;
cout << " Grade = " << grade10 << " = " << letter10 << endl;
cout << " Your average is " << average << " = " << letteravg << endl;
system("Pause");
return 0;
}
答案 0 :(得分:2)
double average = (grade1 + grade2 + grade3 + grade4 ...
这不符合您的想法。
将变量初始化为引用其他变量的表达式,立即计算表达式。当时grade1
等的值用于设置average
的初始值。它不像average
表达式grade1
随着时间的推移而变化的函数,因为grade1
等的值会发生变化。
不幸的是,在向average
等提供任何值之前,您已经做了很长时间,并且此错误告诉您变量甚至没有初始化,因此它们的值不确定
您应该将#include <iostream>
#include <string>
#include <algorithm>
const int A_Score = 90,
B_Score = 80,
C_Score = 70,
D_Score = 60;
char getLetterForGrade(unsigned int grade)
{
if (grade >= A_Score)
return 'A';
else if (grade >= B_Score)
return 'B';
else if (grade >= C_Score)
return 'C';
else if (grade >= D_Score)
return 'D';
else
return 'F';
}
int main()
{
const unsigned int N = 10;
unsigned int grades [N] = {};
char letters[N] = {};
char letterAvg = 0;
std::string username;
std::cout << "What is your user name without spaces? " << std::endl;
std::cin >> username;
for (unsigned int i = 0; i < N; i++) {
std::cout << "What is grade #" << i << "? " << std::endl;
std::cin >> grades[i];
letters[i] = getLetterForGrade(grades[i]);
}
const double average = std::accumulate(std::begin(grades), std::end(grades), 0u) / N;
letterAvg = getLetterForGrade(average);
for (unsigned int i = 0; i < N; i++) {
std::cout << " Grade: " << grades[i] << " = " << letters[i] << std::endl;
}
std::cout << " Your average is " << average << " = " << letterAvg << std::endl;
}
的声明向下移动到函数中的稍后位置,该位置实际上具有用于进行计算的值。
此外,顺便说一句,你应该真的在这里使用数组和循环;你没有充分的理由十次重复你的大部分计划。
这是我写的方式(虽然我没有包含输入验证/错误检查,你绝对应该这样做):
letters
事实上,你甚至不需要数组{{1}},因为它们的值可以简单地在最后一个循环中计算。
答案 1 :(得分:1)
在C ++中,程序在函数内从上到下执行。因此,您声明grade1
到grade10
而没有初始化它们,然后使用它们来计算平均值,甚至在输入之前。这与VHDL等语言不同,语句顺序通常不会产生影响。
答案 2 :(得分:0)
您声明平均值被初始化为您尚未初始化的10个变量之和。