使用嵌套结构将结构作为引用传递的错误

时间:2015-05-04 00:15:06

标签: c++ function struct

#include <iostream>
#include <string>


struct Class
{
    std::string name;
    int units;
    char grade;
};
struct Student
{
    std::string name;
    int id;
    int num;
    double gpa;
    Class classes[20];
};

int get_info(Student students[100],const int);
double get_gpa(Class local_classes[20],const int);

int main()
{
    const int MAX_STUDENTS = 100;
    int num_of_students;
    Student students[MAX_STUDENTS];
    num_of_students = get_info(students,MAX_STUDENTS);
    for (int i = 0; i < num_of_students; i++)
    {
        int amount_classes = students[i].num;
        std::cout << "\n*******x2 = " << students[i].name;
        std::cout << "\n*******Units: " << students[i].classes[0].units;
        students[i].gpa = get_gpa(students[i].classes,amount_classes);
        std::cout << "\nGPA of " << students[i].name << " is: " << students[i].gpa;
    }

    return 0;
}

int get_info(Student students[100],const int MAX)
{   
    int counter = 0, local_id;
    while (counter < MAX && students[counter].id != -99)
    {
        std::cout << "Enter the Student's ID: ";
        std::cin >> local_id;
        std::cin.ignore();
        if (local_id != -99)
        {
            students[counter].id = local_id;
            students[counter].gpa = 0.0;
            std::cout << "\nEnter the Student's name: ";
            std:getline(std::cin,students[counter].name);
            std::cout << "\nEnter the Student's number of classes: ";
            std::cin >> students[counter].num;
            std::cin.ignore();
            for (int i = 0; i < students[counter].num; i++)
            {
                Class students_classes = students[counter].classes[i];
                std::cout << "Enter the name of class #" << i << ": ";
                std::getline(std::cin, students_classes.name);
                std::cout << "Enter the units of " << students_classes.name << ": ";
                std::cin >> students_classes.units;
                std::cout << "Enter the grade recieved for " << students_classes.name << ": ";
                std::cin >> students_classes.grade;
                std::cout << "************* = " << students_classes.grade;
                std::cin.ignore();
            }
        }
        counter++;
        return counter;
    }
}

double get_gpa(Class local_classes[20],const int classNum)
{
    int counter = 0;
    char grade;
    int intGrade = 0, intGradeTotal = 0, units = 0, unitsTotal = 0, numerator = 0;
    double num = 0.0;
    while (counter < classNum)
    {
        grade = local_classes[counter].grade;
        std::cout << "\nGrade for this class is: " << grade;
        std::cout << "\nGrade for this class is: " <<  local_classes[counter].grade;
        units = local_classes[counter].units;
        std::cout << "\nUnits for this class is: " << units;
        switch (grade)
        {
        case 'A':
            intGrade = 4;
            break;
        case 'B':
            intGrade = 3;
        case 'C':
            intGrade = 2;
            break;
        case 'D':
            intGrade = 1;
            break;
        case 'F':
            intGrade = 0;
            break;
        }
        unitsTotal += units;
        std::cout << "\nAll units = " << unitsTotal;
        num = intGrade * units;
        std::cout << "\nNum = " << num;
        numerator += num;
        counter++;
    }
    std::cout << numerator << "/" << unitsTotal;
    std::cout << "\nAnswer is: " << numerator / unitsTotal;

    return 0.0;
}

您好。我正在为课程做一个作业,我正在尝试在函数get_gpa()中进行一些数学运算,但是我遇到了麻烦,因为我无法弄清楚{{{{}中嵌套结构内的信息。函数Students完成后,1}}结构未保存。例如,在get_info()函数结束时,会询问收到的成绩,如果用户输入“A”,则会在支票中打印出来。 (紧接着),但在功能完成后检查时不再存在。这真让我感到困惑,最大的问题是我不知道完全我做错了什么,否则我会尝试修复它。由于此错误,get_info()的数学运算不起作用,因为它全部基于switch语句,该语句假定get_gpa()中有值。如果你运行我的代码我已经添加了调试语句来帮助显示整个事情的消失。

2 个答案:

答案 0 :(得分:2)

您混合使用cin >>getline。这会导致混淆行为,因为cin >>不会消耗输入缓冲区中的尾随换行符,而getline不希望看到缓冲区中保留尾随换行符。

对所有用户输入使用getline,此问题可能会消失。如果您需要阅读号码,请使用getline以及stoi或其他数字转换功能。

答案 1 :(得分:2)

执行此操作时:

Class students_classes = students[counter].classes[i];

您正在制作该对象的副本 ...然后填充副本中的信息。这绝不会影响您想要填充的原始students[counter].classes[i]对象。

您需要改为使用引用

Class& students_classes = students[counter].classes[i];
     ^