I'm trying to make a program that'll add students and their ID's as well as courses. I'm stuck on the part of putting students into courses. I was thinking of using a 2D Array of strings but I don't know how to approach it. Any help would be appreciated. Here's my code.
#include <string>
#include <cstring>
#include <iostream>
#include <cstdlib>
usingnamespace std;
constint max_students = 25;
constint max_courses = 10;
int main()
{
string courses[max_courses]; //Array of strings for the courses
string course_id[max_courses]; //Array of strings for the course id
string student[max_students]; //Array of strings for the student names
string student_id[max_students]; //Array of strings for the students id
string major[max_students]; //Array of strings for the student majors
string addstutocourse[100][10] = {}; //2D array to store students to courses
int choice = 0;
do //Loop to keep the decision going if you need to repick
{
cout << "Please make a decision..." << endl;
cout << "1.) Add course. " << endl;
cout << "2.) Add student. " << endl;
cout << "3.) Add student to course. " << endl;
cout << "4.) Add grades for student in a course. " << endl;
cout << "5.) Show all courses. " << endl;
cout << "6.) Show all students. " << endl;
cout << "7.) Show all students in a course. " << endl;
cout << "8.) Find the average for a student. " << endl;
cout << "9.) Quit. " << endl;
cin >> choice;
/*switch statement for every situation you pick.*/
switch (choice)
{
case 1:
int i;
cout << "How many courses have you entered so far? (i.e. if you haven't entered a course yet, then you've entered 0 courses.)" << endl;
cin >> i; //Sets the beginning of the array
if(i >= max_courses) //Checks to see if there are already the maximum number of classes inputted. (10 is the max)
{
cout << "I'm sorry, but you've already entered too many courses. Shutting down..." << endl;
return 0;
}
cout << "Whats the name of the course?" << endl;
cin.ignore();
getline(cin, courses[i]); //Uses the array of the courses
cout << "Whats the ID number of the course?" << endl;
cin >> course_id[i];
cout << "Course " << courses[i] << " " << course_id[i] << " was added!" << endl;
break;
case 2:
int student_num;
cout << "How many students have you entered so far? (If none then please select 0)" << endl;
cin >> student_num;
if(i >= max_students)
{
cout << "I'm sorry but you've already entered too many students. Shutting down..." << endl;
return 0;
}
cout << "What's the student's last name?" << endl;
cin >> student[student_num];
cout << "What's the student's ID number?" << endl;
cin >> student_id[student_num];
cout << "What's the student's major? (i.e. CSCE, English, Math.)" << endl;
cin >> major[student_num];
cout << "Student has been added!" << endl;
break;
case 3:
break;
case 4:;
break;
case 5:
cout << "All the courses entered so far." << endl;
for(int i = 0; i < 10; i++)
{
cout << courses[i] << " " << course_id[i] << endl;
}
break;
case 6:
cout << "All students entered so far along with their majors." << endl;
for(int i = 0; i < 25; i++)
{
cout << student[i] << " " << student_id[i] << " " << major[i] << endl;
}
break;
case 7:;
break;
case 8:;
break;
};
}while (choice != 9); //quits the loop
return 0;
}
Ok,
Make a string array of all possible courses to take. In each Student, have a string array that contains the name of the courses that they are enrolled in. Then add a AddCourse() function to the Student class.
You should be all set.
Good luck!
okay guys I got it to added students to a course and show how many are in the course. i'm having trouble with the grades. here is my updated code. Please help ?
#include <string>
#include <cstring>
#include <iostream>
#include <cstdlib>
usingnamespace std;
constint max_students = 25; //Global constant to display the max students
constint max_courses = 10; //Global constant to display max courses
int main()
{
string courses[max_courses]; //Array of strings for the courses
string course_id[max_courses]; //Array of strings for the course id
string student[max_students]; //Array of strings for the student names
string student_id[max_students]; //Array of strings for the students id
string major[max_students]; //Array of strings for the student majors
string addstutocourse[max_courses][11]; //2D array to store students with their courses
int grades[5]; //1D array for grades for each course
int choice = 0;
do //Loop to keep the decision going if you need to repick
{
cout << "Please make a decision..." << endl;
cout << "1.) Add course. " << endl;
cout << "2.) Add student. " << endl;
cout << "3.) Add student to course. " << endl;
cout << "4.) Add grades for student in a course. " << endl;
cout << "5.) Show all courses. " << endl;
cout << "6.) Show all students. " << endl;
cout << "7.) Show all students in a course. " << endl;
cout << "8.) Find the average for a student. " << endl;
cout << "9.) Quit. " << endl;
cin >> choice;
/*switch statement for every situation you pick.*/
switch (choice)
{
case 1:
{
int i;
cout << "How many courses have you entered so far? (i.e. if you haven't entered a course yet, then you've entered 0 courses.)" << endl;
cin >> i; //Sets the beginning of the array
if(i >= max_courses) //Checks to see if there are already the maximum number of classes inputted. (10 is the max)
{
cout << "I'm sorry, but you've already entered too many courses. Shutting down..." << endl;
return 0;
}
cout << "Whats the name of the course?" << endl;
cin.ignore();
getline(cin, courses[i]); //Uses the array of the courses
cout << "Whats the ID number of the course?" << endl;
cin >> course_id[i];
addstutocourse[i][0] = courses[i]; //Adds the course to the full roster for students
cout << "Course " << courses[i] << " " << course_id[i] << " was added!" << endl; //Displays a confirmation message
}
break;
case 2:
{
int student_num; //Number of students so far
cout << "How many students have you entered so far? (If none then please select 0)" << endl;
cin >> student_num; //Enter the amount of students you've entered so far
if(student_num >= max_students) //Checks to see if you already added the max number of students
{
cout << "I'm sorry but you've already entered too many students. Shutting down..." << endl;
return 0; //Will shut down the program
}
cout << "What's the student's last name?" << endl; //Grabs the student's name
cin >> student[student_num];
cout << "What's the student's ID number?" << endl; //Grab the student's ID number
cin >> student_id[student_num];
cout << "What's the student's major? (i.e. CSCE, English, Math.)" << endl; //Grab the student's major
cin >> major[student_num];
cout << "Student " << student[student_num] << " " << student_id[student_num] << " " << major[student_num] << " has been added!" << endl; //Confirmation message
}
break;
case 3:
{
string name; //Ask for the student name
string cname; //Ask for the course name
cout << "Student name: ";
cin >> name;
for (int x = 0; x < max_students; x++) //For loop to go through array to see if there is a student with this name already
{
if(student[x] == name)
{
cout << "Which course would you like to add " << student[x] << " too?" << endl;
cin.ignore();
getline(cin, cname);
for(int y = 0; y < max_courses; y++) //For loop to go through the courses to see if this course has been added already
{
if(courses[y] == cname) //If the course is there then this will add the student
{
int num;
cout << "How many students do you have in " << courses[y] << "?" << endl;
cin >> num;
addstutocourse[y][num + 1] = name;
cout << "Process complete!" << endl;
}
}
}
}
}
break;
case 4:
/*{
string stu_name;
cout << "Which student would you like to see the grades of?" << endl;
cin >> stu_name;
for (int a = 0; a < max_students; a++)
{
if(student[a] == stu_name)
{
cout << "Which course would you like to see the grades for?" << endl;
string cour_name;
for (int b = 0; b < max_courses; b++)
{
if(courses[b] == cour_name)
{
}
}
}
}
}*/;
break;
case 5:
{
cout << "All the courses entered so far." << endl;
for(int i = 0; i < 10; i++) //A for loop to display all of the courses you've added along with their ID numbers
{
cout << courses[i] << " " << course_id[i] << endl;
}
}
break;
case 6:
{
cout << "All students entered so far along with their majors." << endl;
for(int i = 0; i < 25; i++) // A for loop to display all student names along with their majors
{
cout << student[i] << " " << student_id[i] << " " << major[i] << endl;
}
}
break;
case 7:
{
string see_course; //String variable to see which course you would like to see the roster of.
cout << "Which course would you like to see?" << endl;
cin.ignore();
getline(cin, see_course); //Gets the line of the course you entered
for (int m = 0; m < max_courses; m++) //For loop to find the course that was entered to see the roster of
if(courses[m] == see_course)
{
for (int n = 1; n < 11; n++) //For loop to display the roster of the course
{
cout << "Roster for " << endl;
cout << addstutocourse[m][n] << endl;
}
}
}
break;
case 8:;
break;
};
}while (choice != 9); //Quits the loop
return 0;
}