2015-12-08 43 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是整數數組的名稱。