c++ - C++ 的 main.cpp 中的结构声明问题

标签 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 括号中放入什么,它都会出现未声明的标识符错误。 有谁知道如何解决这一问题?另外,有人看到这段代码还有什么问题吗?

更新:我已经修复了代码(某种程度上)。它编译并运行,这就是现在的样子:

#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

关于c++ - C++ 的 main.cpp 中的结构声明问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20279108/

相关文章:

C++ : Trailing zeroes in array multiplication

c++ - 使返回类型依赖于调用源?

wpf - 2012 Visual Studio 自定义控件 Xaml/样式设计器 - 如何编辑通用 xaml 样式?

visual-studio - TFS 构建损坏 - 源目录上的目录不为空

c++ - 索引增量如何在 C++ 中工作

c++ - 用于划分非常大的数字的算法

c++ - dsym 文件 UUID 与 xx 中的不匹配,Eclipse CDT 中未加载符号表

c++ - 如何通过引用或指针访问二维 vector

c++ - 如何有条件地为模板头设置编译器优化

c# - 如何测试 Web Api 服务?