程序编译,但我认为开关被忽略
Program compiles but I think switch is ignored.
我目前正在学习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;
}
我没有权限发表评论,因此在此处添加。在您的main()中, get_selection(menu_choice); 开关(菜单_CHOICE)您返回Menu_Choice,但是没有任何值可以使用该值,您可以使用垃圾价值结束,因为它是非初始化的。因此,您可以通过传递菜单_CHOICE的地址/参考或通过返回值来完成两种方法。尝试其中的任何一个都应该起作用,尽管我尚未完成您的计划的其余部分。正如他人建议的那样,尝试一个调试器,例如GDB?