所以我一直在構建一個充當成績簿的程序,並且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);
}
然而,當我運行該程序,我可以在一年級進入,但隨後的整個過程「休息」,成爲無法彌補的。 如果有人能夠幫助我,請運行我的程序並在下面留言。
因爲它的竊聽我:「聲」,而不是「accoustic 」。 – cHao
使類成爲全局類,或通過引用或地址將其傳遞給每個函數; (類和類)或(類*類)。正如John3136所提到的,gradeEditor中的本地實例實際上是無用的。另外,在gradeEditor的最後的「else」子句中添加兩條語句。 – bvj
代碼中有很多錯誤。你應該從簡單的事情開始,一旦你測試了它就可以擴展它。 – juanchopanza