2013-11-29 74 views
0

所以我一直在構建一個充當成績簿的程序,並且main.cpp中的特定函數存在問題。 這是代碼(這是C++,順便說一句):C++的main.cpp中的struct聲明問題

#include <iostream> 
#include <string> 

using namespace std; 

struct Classes 
{ 
    double accousticGuitarEnsemble; 
    double biology; 
    double english; 
    double enteringAKehillah; 
    double geometry; 
    double hebrew; 
    double worldHistory; 
}; 

void gradeEditor() 
{ 
    cout << "GradeBook 1.0" << endl; 
    newGrade: 
    cout << "Which grade are you entering today? (Use the following format: exampleFormat): "; 
    string classBeingEntered; 
    getline(cin, classBeingEntered); 
    Classes Eitan; 
    cout << "Enter the new grade: "; 
    double grade; 
    cin >> grade; 
    cout << "Grade entered." << endl; 
    if (classBeingEntered == "accousticGuitarEnsemble") 
     Eitan.accousticGuitarEnsemble = grade; 
    else if (classBeingEntered == "biology") 
     Eitan.biology = grade; 
    else if (classBeingEntered == "english") 
     Eitan.english = grade; 
    else if (classBeingEntered == "enteringAKehillah") 
     Eitan.enteringAKehillah = grade; 
    else if (classBeingEntered == "geometry") 
     Eitan.geometry = grade; 
    else if (classBeingEntered == "hebrew") 
     Eitan.hebrew = grade; 
    else if (classBeingEntered == "worldHistory") 
     Eitan.worldHistory = grade; 
    else 
     cout << "Invalid class name. Try again." << endl; 
     goto newGrade; 
} 

void choice() 
{ 
    choiceBack: 
    cout << "Do you want to edit another grade? Press Y or N: "; 
    char chChoice; 
    cin >> chChoice; 
    switch (chChoice) { 
     case 'Y': 
      cout << "Alright then!" << endl; 
      do { 
      gradeEditor(); 
      goto choiceBack; 
      } while (chChoice == 'Y'); 
     case 'N': 
      cout << "Printing grades..." << endl; 
      break; 
     case 'y': 
      cout << "Alright then!" << endl; 
      do { 
      gradeEditor(); 
      goto choiceBack; 
      } while (chChoice == 'y'); 
     case 'n': 
      cout << "Printing grades..." << endl; 
      break; 
    } 
} 

void printGrades(Classes Eitan) 
{ 
    cout << "Accoustic Guitar Ensemble: " << Eitan.accousticGuitarEnsemble << endl; 
    cout << "Biology: " << Eitan.biology << endl; 
    cout << "English: " << Eitan.english << endl; 
    cout << "Entering a Kehillah: " << Eitan.enteringAKehillah << endl; 
    cout << "Geometry: " << Eitan.geometry << endl; 
    cout << "Hebrew: " << Eitan.hebrew << endl; 
    cout << "World History: " << Eitan.worldHistory << endl; 
    system("PAUSE"); 
} 

int main() 
{ 
    gradeEditor(); 
    choice(); 
    printGrades(); 
} 

然而,對於printGrades(),我得到這個錯誤: 錯誤C2660: 'printGrades':函數不接受0參數 智能感知:函數調用中的參數太少 這兩個錯誤都發生在第92行,其中printGrades在main中被調用。 無論我放在printGrades括號中,它都會出現未聲明的標識符錯誤。 有誰知道如何解決這個問題?另外,有沒有人看到這個代碼有什麼問題?

更新:我已修復代碼(排序)。它編譯和運行,這是它現在是什麼:

#include <iostream> 
#include <string> 
using namespace std; 

struct Classes 
{ 
    double acousticGuitarEnsemble; 
    double biology; 
    double english; 
    double enteringAKehillah; 
    double geometry; 
    double hebrew; 
    double worldHistory; 
}; 

Classes gradeEditor() 
{ 
    Classes eitan; 
    cout << "GradeBook 1.0" << endl; 
    newGrade: 
    cout << "Which grade are you entering today? (Use the following format: exampleFormat): "; 
    string classBeingEntered; 
    getline(cin, classBeingEntered); 
    cout << "Enter the new grade: "; 
    double grade; 
    cin >> grade; 
    cout << "Grade entered." << endl; 
    if (classBeingEntered == "acousticGuitarEnsemble") 
     eitan.acousticGuitarEnsemble = grade; 
    else if (classBeingEntered == "biology") 
     eitan.biology = grade; 
    else if (classBeingEntered == "english") 
     eitan.english = grade; 
    else if (classBeingEntered == "enteringAKehillah") 
     eitan.enteringAKehillah = grade; 
    else if (classBeingEntered == "geometry") 
     eitan.geometry = grade; 
    else if (classBeingEntered == "hebrew") 
     eitan.hebrew = grade; 
    else if (classBeingEntered == "worldHistory") 
     eitan.worldHistory = grade; 
    else 
     cout << "Invalid class name. Try again." << endl; 
     goto newGrade; 
} 

void choice() 
{ 
    choiceBack: 
    cout << "Do you want to edit another grade? Press Y or N: "; 
    char chChoice; 
    cin >> chChoice; 
    switch (chChoice) { 
     case 'Y': 
      cout << "Alright then!" << endl; 
      do { 
      gradeEditor(); 
      goto choiceBack; 
      } while (chChoice == 'Y'); 
     case 'N': 
      cout << "Printing grades..." << endl; 
      break; 
     case 'y': 
      cout << "Alright then!" << endl; 
      do { 
      gradeEditor(); 
      goto choiceBack; 
      } while (chChoice == 'y'); 
     case 'n': 
      cout << "Printing grades..." << endl; 
      break; 
    } 
} 

void printGrades(Classes eitan) 
{ 
    cout << "Acoustic Guitar Ensemble: " << eitan.acousticGuitarEnsemble << endl; 
    cout << "Biology: " << eitan.biology << endl; 
    cout << "English: " << eitan.english << endl; 
    cout << "Entering a Kehillah: " << eitan.enteringAKehillah << endl; 
    cout << "Geometry: " << eitan.geometry << endl; 
    cout << "Hebrew: " << eitan.hebrew << endl; 
    cout << "World History: " << eitan.worldHistory << endl; 
    system("PAUSE"); 
} 

int main() 
{ 
    Classes eitan = gradeEditor(); 
    choice(); 
    printGrades(eitan); 
} 

然而,當我運行該程序,我可以在一年級進入,但隨後的整個過程「休息」,成爲無法彌補的。 如果有人能夠幫助我,請運行我的程序並在下面留言。

+0

因爲它的竊聽我:「聲」,而不是「accoustic 」。 – cHao

+0

使類成爲全局類,或通過引用或地址將其傳遞給每個函數; (類和類)或(類*類)。正如John3136所提到的,gradeEditor中的本地實例實際上是無用的。另外,在gradeEditor的最後的「else」子句中添加兩條語句。 – bvj

+0

代碼中有很多錯誤。你應該從簡單的事情開始,一旦你測試了它就可以擴展它。 – juanchopanza

回答

2

您的printGrades()方法需要Classes類型的參數,但您不要傳遞它。

還建議讓您的變量名稱/參數以小寫字母開頭,因此它們看起來不像類型。即void printGrades(Classes Eitan)應該是void printGrades(Classes eitan)

Classes的唯一實例是gradeEditor()的本地,所以沒有任何東西被「存儲在任何地方」。

最後但並非最不重要的:

您使用GOTO

+0

這不是說使用goto,而是他如何使用goto。 – bvj

+0

@bvj No no。在我的書中,這是他使用'goto' – John3136

+0

的事實,但它是語言的一部分。儘管如此,GSD的規則。 – bvj

2

的main()創建的結構類一個對象,並傳遞對象作爲參數傳遞給printGrades()。函數printGrades()根據函數定義需要一個結構對象作爲它的參數。像這樣的東西。

Classes cl; 
printGrades(cl); 

而且,即使你做了以上的事情printGrades()功能將打印垃圾值結構的對象不會被初始化,所以我會建議你改變gradeEditor(返還型)voidstruct類型,它將對象返回到結構中並複製該對象在c1。這樣的事情會更合適,你可以刪除methoid 選擇

Classes gradeEditor() 
{ 
/* Function Body*/ 
    return Eitan; 
} 

main() 
{ 
    Classes cl; 


    char choice; 
    do{ 
     c1=gradeEditor(); 
     cout << "Do you want to edit another grade? Press Y or N: "; 
    }while((choice=getch())!='n' || (choice=getch())!='N'); 
    cout<<"Printing Grades: "<<endl; 
    printGrades(cl); 

} 

而在gradeEditor您使用轉到,仔細觀察它,控制永遠不會到來的gradeEditor出來()方法,因爲你沒有指定任何條件出來。

0

下面的代碼將正常工作適合你,如果你正在使用它的系統上的其他塔窗口,你還需要包括#include <cstdlib>使用system("pause")

#include <iostream> 
#include <string> 
#include <cstdlib> 
using namespace std; 

struct Classes 
{ 
    double accousticGuitarEnsemble; 
    double biology; 
    double english; 
    double enteringAKehillah; 
    double geometry; 
    double hebrew; 
    double worldHistory; 
}; 

void gradeEditor() 
{ 
    cout << "GradeBook 1.0" << endl; 
    newGrade: 
    cout << "Which grade are you entering today? (Use the following format: exampleFormat): "; 
    string classBeingEntered; 
    getline(cin, classBeingEntered); 
    Classes Eitan; 
    cout << "Enter the new grade: "; 
    double grade; 
    cin >> grade; 
    cout << "Grade entered." << endl; 
    if (classBeingEntered == "accousticGuitarEnsemble") 
     Eitan.accousticGuitarEnsemble = grade; 
    else if (classBeingEntered == "biology") 
     Eitan.biology = grade; 
    else if (classBeingEntered == "english") 
     Eitan.english = grade; 
    else if (classBeingEntered == "enteringAKehillah") 
     Eitan.enteringAKehillah = grade; 
    else if (classBeingEntered == "geometry") 
     Eitan.geometry = grade; 
    else if (classBeingEntered == "hebrew") 
     Eitan.hebrew = grade; 
    else if (classBeingEntered == "worldHistory") 
     Eitan.worldHistory = grade; 
    else 
     cout << "Invalid class name. Try again." << endl; 
     goto newGrade; 
} 

void choice() 
{ 
    choiceBack: 
    cout << "Do you want to edit another grade? Press Y or N: "; 
    char chChoice; 
    cin >> chChoice; 
    switch (chChoice) { 
     case 'Y': 
      cout << "Alright then!" << endl; 
      do { 
      gradeEditor(); 
      goto choiceBack; 
      } while (chChoice == 'Y'); 
     case 'N': 
      cout << "Printing grades..." << endl; 
      break; 
     case 'y': 
      cout << "Alright then!" << endl; 
      do { 
      gradeEditor(); 
      goto choiceBack; 
      } while (chChoice == 'y'); 
     case 'n': 
      cout << "Printing grades..." << endl; 
      break; 
    } 
} 

void printGrades(Classes Eitan) 
{ 
    cout << "Accoustic Guitar Ensemble: " << Eitan.accousticGuitarEnsemble << endl; 
    cout << "Biology: " << Eitan.biology << endl; 
    cout << "English: " << Eitan.english << endl; 
    cout << "Entering a Kehillah: " << Eitan.enteringAKehillah << endl; 
    cout << "Geometry: " << Eitan.geometry << endl; 
    cout << "Hebrew: " << Eitan.hebrew << endl; 
    cout << "World History: " << Eitan.worldHistory << endl; 
    system("PAUSE"); 
} 

int main() 
{ 
    Classes Eitan; 
    gradeEditor(); 
    choice(); 
    printGrades(Eitan); 
} 
+0

測試你的代碼。還有一些問題需要糾正。 – bvj

+0

我剛剛更正了語法哥們沒有驗證邏輯 –

+0

不是真的,哥們。 – bvj