我是c +的新手,我正在尝试制作一个简单的课程名单程序,接受新学生将学生数据存储在一个数组中,然后对其进行排序并显示数组的内容。但是,在运行程序并进入菜单选择时,三个功能中的两个不起作用。非常感谢任何帮助或指导。我的代码在这里。
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
using namespace std;
//Create Students class
class Students
{
public:
char sFirstName[256];
char sLastName[256];
int sStudentID;
double sGrade;
double sGPA;
double nCreditHours;
};
//functions
Students addStudent();
//void displayRoster();
//void sortRoster();
void showMenu();
void showWelcome();
//Welcome function
void showWelcome()
{
cout << "Welcome to my class roster program. \n"
<< "This program can be used to add students to the roster, \n"
<< "which can then be sorted by either name or I.D. number. \n"
<< endl;
}
//Menu function
void showMenu()
{
cout << " Student Roster: \n"
<< "MAIN MENU PLEASE SELECT AN OPTION" << endl;
cout << "1) Add student to roster: " << endl;
cout << "2) Display current roster: " << endl;
cout << "3) Sort roster: " << endl;
cout << "4) Exit program: " << endl;
//cout << "5) Display roster sorted by 'student I.D.': " << endl;
//cout << "6) Display roster sorted by 'Grade': " << endl;
//cout << "7) Display roster sorted by 'GPA': \n" << endl;
cout << " Make your selection: \n" << endl;
}
//Add student function
Students addStudent()
{
Students student;
cout << "Add student to roster. \n"
<< "Enter first name: " << endl;
cin >> student.sFirstName;
cout << "Enter last name: " << endl;
cin >> student.sLastName;
cout << "Enter student I.D.: " << endl;
cin >> student.sStudentID;
return student;
}
void displayStudent(Students student)
{
cout << "Student name: " << student.sFirstName << " "
<< student.sLastName << endl;
cout << "I.D. # " << student.sStudentID << endl;
}
void displayRoster()
{
Students student[256];
int nCount;
for (int index = 0; index < nCount; index++)
{
displayStudent(student[index]);
}
}
int getStudents(Students student[], int nMaxSize)
{
int index;
for (index = 0; index < nMaxSize; index++)
{
char uInput;
cout << "Enter another student to the roster? (Y/N): ";
cin >> uInput;
if (uInput != 'y' && uInput != 'Y')
{
break;
}
student[index] = addStudent();
}
return index;
}
void sortRoster()
{
Students student[256];
int nCount;
//bubble swap
int nSwaps = 1;
while (nSwaps != 0)
{
nSwaps = 0;
for (int n = 0; n < (nCount - 1); n++)
{
if (student[n].sStudentID > student[n+1].sStudentID)
{
Students temp = student[n+1];
student[n+1] = student[n];
student[n] = temp;
nSwaps++;
}
}
}
}
int main()
{
int selection; //menu selection variable
//constants for menu selection
const int ADD_STUDENT = 1,
DISPLAY_ROSTER = 2,
SORT_ROSTER = 3,
QUIT_PROGRAM = 4;
Students student[256];
//int nCount = getStudents(student, 256);
do
{
showWelcome(); //Show welcome message
showMenu(); //Show menu options
cin >> selection;
while (selection < ADD_STUDENT || selection > QUIT_PROGRAM)
{
cout << "Enter a valid selection: ";
cin >> selection;
}
if (selection != QUIT_PROGRAM)
{
switch (selection)
{
case ADD_STUDENT:
addStudent();
break;
case DISPLAY_ROSTER:
displayRoster();
break;
case SORT_ROSTER:
sortRoster();
break;
}
}
}
while (selection != QUIT_PROGRAM);
return 0;
}
答案 0 :(得分:1)
您的nCount
未初始化。由于此变量用于这两个函数(并假设它指的是总计数),因此可以将其声明为全局变量:
nCount=0;
每次添加新条目时,都可以将计数器增加为:
nCount++;
使您的代码真正有效的另一个建议:
student[i++]=addStudent();
其中i
是一个初始化为0
的计数器。您的addStudent()
函数返回一个对象,然后将其丢弃。将其存储在您创建的对象数组中:
Students student[256];
此外,由于您在几乎所有函数中都使用了上述函数,因此最好将其声明为全局而不是在每个函数中重新声明。
答案 1 :(得分:1)
问题不在switch
。
addStudent()
未将学生添加到任何列表或数组中。此外,由于返回类型为Students
,因此您应将其添加到Students
的任意数组中。由于您尚未存储任何数据显示,因此无法显示任何内容。
另一个问题是nCount
。您正在使用它进行比较而不进行初始化。同时保持nCount
同步使其成为全局,用作指针或使用return来处理它。
问题出在displayRoster()
。您将Students
数组声明为Students student[256];
并且您正在使用它而不进行初始化。此外,如果初始化,它将不会获得作为输入提供的数据。
注意:再次坐下来阅读你的代码,还有很多错误。尝试可视化数据的存储方式以及代码的行为方式,然后开始编写代码。