The following program reads in 10 students; name, ID, GPA, class, and balance. I
ID: 3633738 • Letter: T
Question
The following program reads in 10 students; name, ID, GPA, class, and balance. I am able to print out a list of all students, students on the Dean's list, and students on probation.Can someone help me print out the number of students on the Dean's list and the number of students on probation?
I would like the output to read
"The number of students on the Dean's list is ___."
"The number of students on probation is ___."
I would like to do this without rewriting code.
#include <iostream>
using namespace std;
#define MAX_STUD 10
struct stud_type { char name[20];
int id;
float gpa;
int cl;
int bal;
};
stud_type stud[MAX_STUD];
int num_stud;
// function prototypes
void add_student( int & );
void print_list ( int );
void deans_list (int);
void probation_list (int);
main()
{
int command;
num_stud = 0; // initialize some stuff
// ok, we can get started now
while(cin>>command)
{
// cout<<"Command is "<<command<<endl; // Debug
if (command == 1){
// cout<<"Adding a Student "; // Debug
add_student(num_stud);
}//command 1
if (command == 2){
// cout<<"Printing List "; // Debug
print_list(num_stud);
}//command 2
if (command == 3){
// cout<<"Dean's List "; // Debug
deans_list(num_stud);
}//command 3
if (command == 4){
// cout<<"Probation List "; // Debug
probation_list(num_stud);
}//command 4
}//while loop
system ("PAUSE");
}
void add_student(int &num_stud){
char n[20]; int temp; float tempf;
if ( num_stud < MAX_STUD ){
// cout<<"Got Space, Add the Student "; // Debug
cin>>stud[num_stud].name>>stud[num_stud].id>>stud[num_stud].gpa>>stud[num_stud].cl>>stud[num_stud].bal;
num_stud = num_stud + 1;
}//for if
else{
// cout<<"Dont have, Skip the Student "; // Debug
cin>>n>>temp>>tempf>>temp>>temp; // Read rest of student info
cout<<" Cannot Add student "<<n<<", list is full"<<endl; // Print message
}//for else
}//for void add student
void print_list( int num_stud){
int i;
int j = 0;
cout<<"List of Students "<<endl;
cout<<"Total number of students = "<<num_stud<<endl;
cout <<"#"<<" "<<"NAME"<<" "<<"ID"<<" "<<"GPA"<<" "<<"CLASS"<<" "<<"BALANCE"<<endl;
for (i=0; i<num_stud; ++i){
cout<<j+1<<" "<<stud[i].name<<" "<<stud[i].id<<" "<<stud[i].gpa<<" "<<stud[i].cl<<" "<<stud[i].bal<<endl;
j++;
}//for for-loop
}//for void print list
void deans_list( int num_stud){
int i;
int j = 0;
cout<<"Dean's List "<<endl;
cout<<" The number of students on the Dean's list is = "<<num_stud<<endl;
for (i=0; i<num_stud; ++i){
if ( stud[i].gpa >= 3.0){
cout<<j+1<<" "<<stud[i].name<<" "<<stud[i].id<<" "<<stud[i].gpa<<endl;
j++;
}//for if GPA >= 3.0
}//for for-loop
}//for void deans list
void probation_list( int num_stud){
int i;
int j = 0;
cout<<"Probation List "<<endl;
cout<<" The number of students on probation is = "<<num_stud<<endl;
for (i=0; i<num_stud; ++i){
if ( stud[i].gpa < 2.0){
cout<<j+1<<" "<<stud[i].name<<" "<<stud[i].id<<" "<<stud[i].gpa<<endl;
j++;
}//for if
}//for for-loop
}//for void probation
Explanation / Answer
please rate - thanks
I added some prompts, so I'd know what I was doing
#include <iostream>
using namespace std;
#define MAX_STUD 10
struct stud_type { char name[20];
int id;
float gpa;
int cl;
int bal;
};
stud_type stud[MAX_STUD];
int num_stud;
// function prototypes
void add_student( int & );
void print_list ( int );
void deans_list (int);
void probation_list (int);
void menu();
main()
{
int command;
num_stud = 0; // initialize some stuff
// ok, we can get started now
menu();
while(cin>>command)
{
// cout<<"Command is "<<command<<endl; // Debug
if (command == 1){
// cout<<"Adding a Student "; // Debug
add_student(num_stud);
}//command 1
if (command == 2){
// cout<<"Printing List "; // Debug
print_list(num_stud);
}//command 2
if (command == 3){
// cout<<"Dean's List "; // Debug
deans_list(num_stud);
}//command 3
if (command == 4){
// cout<<"Probation List "; // Debug
probation_list(num_stud);
}//command 4
menu();
}//while loop
system ("PAUSE");
}
void menu()
{cout<<"1) add student ";
cout<<"2) Print List ";
cout<<"3) Dean's List ";
cout<<"4) Probation List ";
}
void add_student(int &num_stud){
char n[20]; int temp; float tempf;
if ( num_stud < MAX_STUD ){
// cout<<"Got Space, Add the Student "; // Debug
cout<<"Enter name - id - gpa - class - balance ";
cin>>stud[num_stud].name>>stud[num_stud].id>>stud[num_stud].gpa>>stud[num_stud].cl>>stud[num_stud].bal;
num_stud = num_stud + 1;
}//for if
else{
// cout<<"Dont have, Skip the Student "; // Debug
cin>>n>>temp>>tempf>>temp>>temp; // Read rest of student info
cout<<" Cannot Add student "<<n<<", list is full"<<endl; // Print message
}//for else
}//for void add student
void print_list( int num_stud){
int i;
int j = 0;
cout<<"List of Students "<<endl;
cout<<"Total number of students = "<<num_stud<<endl;
cout <<"#"<<" "<<"NAME"<<" "<<"ID"<<" "<<"GPA"<<" "<<"CLASS"<<" "<<"BALANCE"<<endl;
for (i=0; i<num_stud; ++i){
cout<<j+1<<" "<<stud[i].name<<" "<<stud[i].id<<" "<<stud[i].gpa<<" "<<stud[i].cl<<" "<<stud[i].bal<<endl;
j++;
}//for for-loop
cout<<endl;
}//for void print list
void deans_list( int num_stud){
int i;
int j = 0;
cout<<"Dean's List "<<endl;
for (i=0; i<num_stud; ++i)
if ( stud[i].gpa >= 3.0)
j++;
cout<<" The number of students on the Dean's list is = "<<j<<endl;
j=0;
for (i=0; i<num_stud; ++i){
if ( stud[i].gpa >= 3.0){
cout<<j+1<<" "<<stud[i].name<<" "<<stud[i].id<<" "<<stud[i].gpa<<endl;
j++;
}//for if GPA >= 3.0
}//for for-loop
cout<<endl;
}//for void deans list
void probation_list( int num_stud){
int i;
int j = 0;
cout<<"Probation List "<<endl;
for (i=0; i<num_stud; ++i)
if ( stud[i].gpa < 2.0)
j++;
cout<<" The number of students on probation is = "<<j<<endl;
j=0;
for (i=0; i<num_stud; ++i){
if ( stud[i].gpa < 2.0){
cout<<j+1<<" "<<stud[i].name<<" "<<stud[i].id<<" "<<stud[i].gpa<<endl;
j++;
}//for if
}//for for-loop
cout<<endl;
}//for void probation
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.