#include <iostream>
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 array [name] = MAX;
// Declare an array of whole numbers named grade that holds up to MAX
double array [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] respectively
cin >> name[x];
cin >> grade[x];
// While (not-end-of-file)
while(inFile)
{
//Increment x
x++;
// Read a name and a grade from the file and assign them to name[x] and grade[x] respectively
cin >> name[x];
cin >> 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)
{
//16. Accumulate grade[x] in 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;
}
以下是我們在20中的一些錯誤。大多數重複未聲明的標識符。也不知道在哪裏我需要添加更多的大括號來使它在語法上是正確的。還有一件事..我如何積累等級[x]成爲總和?任何幫助將不勝感激,謝謝。如何在C++中聲明字符串和整數數組?
error C2065: 'name' : undeclared identifier
error C2075: 'array' : array initialization needs curly braces
error C2065: 'grade' : undeclared identifier
error C2371: 'array' : redefinition; different basic types
error C2440: 'initializing' : cannot convert from 'const int' to 'double [1]'
error C2228: left of '.open' must have class/struct/union1> type is 'int'
error C2143: syntax error : missing ';' before ')'
error C2059: syntax error : ')'
error C2143: syntax error : missing ';' before '{'
error C2228: left of '.close' must have class/struct/union1> type is 'int'
我想你需要在[The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)中找到一本初學者手冊,並從那裏開始。 –
使用[cpp-reference](http://en.cppreference.com/w/)等參考文件查找需要爲「ifstream」包含哪個標題。開始時可能看起來不太可能,但只要嘗試一下,就會發現它主要是結構良好的,再加上它是可搜索的。等等。 –
@ Cheersandhth.-Alf,' array [name] = MAX;'lines,我認爲問題出現在另一個地方。如果OP在正確的位置交換'inFile'的'cin',幾乎可以解決ifstream問題。 –
Zeta