在main.cpp for C ++中的结构声明问题

时间:2013-11-29 05:42:38

标签: c++ visual-c++ visual-studio-2012

所以我一直在构建一个可以作为成绩册的程序,而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括号中添加什么内容,它都会出现未声明的标识符错误。 有谁知道如何解决这一问题?此外,是否有人看到此代码有任何其他错误?

更新:我修复了代码(Sort of)。它编译并运行,现在就是这样:

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

然而,当我运行该程序时,我可以进入一个等级,但随后整个过程“中断”并变得无法修复。 如果有人可以帮助我,请运行我的程序并在下面发表评论。

3 个答案:

答案 0 :(得分:2)

您的printGrades()方法需要Classes类型的参数,但您不会将其传递一个。

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

Classes中唯一的gradeEditor()实例是{{1}}中的本地实例,因此不会“存储在任何地方”。

最后但并非最不重要:

你正在使用GOTO

答案 1 :(得分:2)

main()中创建 struct Classes 的对象,并将该对象作为参数传递给 printGrades()。函数 printGrades()期望根据函数定义将结构对象作为其参数。这样的事情。

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()方法中,因为你没有指定任何条件出来。

答案 2 :(得分: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 );
}