面向C++的main.cpp中的可视化结构声明问题



所以我一直在构建一个可以充当成绩册的程序,但main.cpp中的一个特定函数出现了问题。这是代码(顺便说一下,这是C++):

#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个参数IntelliSense:函数调用中的参数太少这两个错误都发生在第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);
}

然而,当我运行该程序时,我可以进入一个年级,但随后整个过程"中断",变得无法弥补。如果有人能进一步帮助我,请运行我的程序并在下面留言。

您的printGrades()方法需要一个类型为Classes的参数,但您没有给它传递一个。

还建议让变量名/参数以小写字母开头,这样它们看起来就不像类型。即void printGrades(Classes Eitan)应该是void printGrades(Classes eitan)

Classes的唯一实例在gradeEditor()中是本地的,因此没有任何东西可以"存储在任何地方"。

最后但同样重要的是:

您正在使用GOTO

main()中创建structClasses的对象,并将该对象作为参数传递给printGrades()rintGrades()根据函数定义,需要一个结构对象作为其参数。像这样的东西。

Classes cl;
printGrades(cl);

即使你做了上面的事情,printGrades()函数也会打印垃圾值,因为结构对象没有初始化,所以我建议你将gradeEditor()返回类型从void更改为struct类型,将对象返回到结构中,并将该对象复制到c1中。这样的东西会更合适,你可以删除方法选择也可以删除

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中,您正在使用goto,请仔细观察,控件永远不会从gradeEditor()方法中出来,因为您没有指定任何要出来的条件。

如果您在除windows之外的系统上使用以下代码,则使用system("pause") 也需要包含#include <cstdlib>

#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 );
}

最新更新