2015-12-08 45 views
-4
#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' 
+3

我想你需要在[The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)中找到一本初学者手册,并从那里开始。 –

+1

使用[cpp-reference](http://en.cppreference.com/w/)等参考文件查找需要为“ifstream”包含哪个标题。开始时可能看起来不太可能,但只要尝试一下,就会发现它主要是结构良好的,再加上它是可搜索的。等等。 –

+1

@ Cheersandhth.-Alf,' array [name] = MAX;'lines,我认为问题出现在另一个地方。如果OP在正确的位置交换'inFile'的'cin',几乎可以解决ifstream问题。 – Zeta

回答

2

希望这有助于一点点... ...(和建议得到一个C++的书和参考资料)

您的许多变量在使用前没有声明。

如 '姓名' 必须是:string name[];

让我们来看看下面的语句:

string array [name] = MAX;

看起来你并不完全熟悉C++。

“String”是数据类型。 “数组”是数组的实际名称。

“name”是一个未声明的变量,并且必须是一个整数才能起作用。

“MAX”是一个常数。(你是好那里。)

从你写的方式,我猜你想拥有的串并联阵列和双所谓的“名称“和”等级“,共600个元素。

在C++中,将被写成:

string name[MAX]; // this creates an empty string array of 600 elements

和档次是: double grade[MAX]; // this creates an empty double array of 600 elements

从您的代码下面这部分不会有几个原因的工作:

// 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]; }

有几个原因是:

  1. while(not-end-of-file)是不是真正的代码,你想:while(!inFile.eof())

  2. cin >> name[x];从未访问文件,并且永远不会赋值给一个元素

祝你好运! Google是你的朋友!让人们知道你是一个新手,所以他们不会把你分开在堆栈溢出!获得像dev ++或CLion这样的好IDE。

0

array不是C++中的关键字。

如果要声明字符串数组,可容纳最大值= MAX,那么你应该试试这个

字符串名称[MAX]。

其中“name”是字符串数组的名称,MAX是数组可以存储的字符串的总数。

同为整数,你应该使用

双级[MAX]。

其中grade是整数数组的名称。