程序编译,但我认为开关被忽略。

时间:2016-12-05 22:16:38

标签: c++

我目前正在学习c ++,但我落后了一点,所以如果我的问题很明显,我会道歉。

我必须创建一个程序,要求学生的姓名,GPA,入学年份,并获得为该人生成的随机5位数字。学生人数不会超过42人。

我的程序编译(某种程度上),我能够得到无效菜单选择的错误,但是,每当我给出有效选择(当前为1)时没有任何反应。

也许我错过了什么,这就是我需要帮助的原因。

这是我的代码。

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;




//print all the menu options
void print_menu()
{
    cout<<"\nRCNJ Registrar Menu:"<<"\n"
        <<"\n"
        <<"[1] Add a student"<<"\n"
        <<"[2] Display all students"<<"\n"
        <<"[3] Display by year"<<"\n"
        <<"[4] Display statistics"<<"\n"
        <<"[5] Quit"<<"\n";

}



//get and return the student's name
void get_name(string& student_name) //call student_name after that.
{
    cout<<"Please enter the sudent's name: ";
    cin >> student_name;
    cout<<"\n";
}

//validate and return gpa
double get_gpa()
{
    double student_gpa = 0;

        cout<<"Please enter the GPA: ";
        cin >>student_gpa;
        cout<<"\n";

            while (student_gpa > 4 || student_gpa < 0)
            {
                cout<<"Please enter a valid GPA for the student (0.00 - 4.00): ";
                cin >> student_gpa;
                cout<<"\n";
            }   

    return student_gpa;
}

//validateand return year
int get_year()
{
    int student_year = 0;

    cout<<"Please enter the year: ";
    cin >> student_year;
    cout<<"\n";

        while (student_year >2016 || student_year <1972)
        {
            cout<<"Please enter a valid year (min 1972, max 2016): ";
            cin >> student_year;
            cout<<"\n";
        }   

    return student_year;    
}

//generate the student's R#
int generate_number()
{
    int r_number;

        srand (time(NULL));
        r_number = rand() % 89999 + 10000;

    return r_number;
}


//save info. Include get_name, get_gpa, get_year
void input_new_student()
{
    string student_name;
    double student_gpa;
    int student_year;
    int r_number;

    int s_name, s_gpa, s_year, r_num;



    get_name(student_name);
    get_gpa();
    get_year();
    generate_number();


}


//display all students in the proper format
void print_all()
{

}

//get a year as selection and print all students that are the same year
void print_by_year()
{

}


//display statistics based on entered students
void print_statistics()
{

}

//validate and return the menu option selected by the user. 
//it should call print_menu defined earlier
int get_selection(int menu_choice)
{
    menu_choice = 0;

    cout<<"\n"
        <<"Selection: ";
    cin >> menu_choice;
    cout<<"\n";

        while (menu_choice > 5 || menu_choice< 1)
        {
            cout<<" Menu choice is invalid. Please re-enter (1 - 5): ";
            cin>> menu_choice;
            cout<<"\n";
        }   

    return menu_choice;
}


int main()
{
    string student_name;
    double student_gpa;
    int student_year;
    int r_number;
    int menu_choice;

    int s_name=0;
    int s_gpa=0;
    int s_year=0;
    int r_num=0;

                string nameArray[42];
            s_name++;

            double gpaArray[42];
            s_gpa++;

            int yearArray[42];
            s_year++;

            int ramapoArray[42];
            r_num++;

        print_menu();
        get_selection(menu_choice);

            switch (menu_choice)
            {
                case 1: 
                input_new_student();

                nameArray[s_name] = student_name;
                gpaArray[s_gpa] = student_gpa;
                yearArray[s_year] = student_year;
                ramapoArray[r_num] = r_number;
                break;
            }







return 0;
}

1 个答案:

答案 0 :(得分:0)

我没有权限发表评论,因此在此处添加。 在你的主(),         get_selection(menu_choice);             开关(menu_choice) 你返回menu_choice,但没有取值,你终止使用垃圾值,因为它是未初始化的。 所以有两种方法可以通过传递menu_choice的地址/引用或返回值来实现。尝试其中任何一个它应该工作,虽然我没有完成你的程序的其余部分。 根据其他人的建议,尝试使用调试器,例如GDB?