#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// Declare variable
ifstream inFile;
// Declare constant
const int MAX = 600;
// Declare an array of strings named name that holds up to MAX
string name[MAX] = {};
// Declare an array of whole numbers named grade that holds up to MAX
double grade[MAX] = {};
// Declare variables quantity, x, avg, and sum (initialized to zero) that hold whole numbers
int sum = 0, avg, x, quantity;
// Open input file
inFile.open("indata3.txt");
// Check if the file was opened
if (!inFile)
{
cout << "File was not found!" << endl;
return 1;
}
// Set x to 0
x = 0;
// Read name and grade from the file and assign them to name[x] and grade[x]
inFile >> name[x];
inFile >> grade[x];
// While (not-end-of-file)
while(!inFile.eof())
{
//Increment x
x++;
// Read a name and a grade from the file and assign them to name[x] and grade[x]
inFile >> name[x];
inFile >> grade[x];
}
// Print message
cout << "Enter quantity of grades to be processed (0-" << x << "): " << endl;
// Read a value from the keyboard and assign it to quantity
cin >> quantity;
// For (x = 0 to quantity-1)
for (x = 0; x <= quantity-1)
{
// Accumulate grade[x] in sum
grade[x]+=sum;
}
// Assign to avg the value returned by average (sum, quantity)
avg = sum/quantity;
// Print "Average grade: ", avg
cout << "Average grade: " << avg << endl;
// Print "Name", "Grade", " Comment"
cout << "Name" << "," << "Grade" << "," << " Comment" << endl;
// For (x = 0 to quantity-1)
for (x = 0; x <= quantity-1)
{
// Print name[x], grade[x]
cout << name[x] << ", " << grade[x] << endl;
// If (grade[x] < avg)
if (grade[x] < avg)
{
// Print " below average"
cout << " below average" << endl;
}
// Else if (grade[x] > avg)
else if (grade[x] > avg)
{
// Print " above average"
cout << " above average" << endl;
}
// Else
else()
{
// Print " average"
cout << " average" << endl;
}
}
// Close the file.
inFile.close();
return 0;
}
我相信我的代码很好,但我有4个语法错误,包括以下内容:
.cpp(49) : error C2143: syntax error : missing ';' before ')'
.cpp(61) : error C2143: syntax error : missing ';' before ')'
.cpp(78) : error C2059: syntax error : ')'
.cpp(79) : error C2143: syntax error : missing ';' before '{'
问题可能在于for循环是如何嵌套的,但我不太确定究竟在哪里修复它,因为我已经玩了一下它。任何帮助都会非常感谢!还警告你,我是一个菜鸟,为丑陋的代码感到抱歉。
答案 0 :(得分:4)
您的编译器抱怨您有语法错误?很可能是因为你有语法错误......
for (a; b; c) {
| For loop has 3 parts...
else() // It should be else {
| else cannot have a condition, let alone an empty one
阅读编译器错误并修复它所说的内容。
http://ideone.com/3ZJjaO - 编译器错误
http://ideone.com/kMCKIV - 编译器错误已修复
注意:你有一些工作要领先,你有一系列无限的looooooooops以及其他问题。可能值得拿起一本书并尝试所有的例子。