1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
/*
write an app to get students names/course names and their marks!!
then show the marks in courses accending (in menu by tapping 2 and enter )
or show the card for the chosen student(in menu by tapping 3 and enter);
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//change these values how you please.
const int MAX_COURSES = 10;
const int MAX_STUDENTS = 10;
char getChoice() {
char choice;
cout << "===simple student data show app===" << endl;
cout << "1) Save the information" << endl;
cout << "2) Show course information" << endl;
cout << "3) Show card for specific student" << endl;
cout << "4) Exit" << endl;
cout << "\tSelection: ";
// I use a char instead of an int because it's much more difficult to fail where as if someone
// typed out a 'w' instead of a '2' your program would stop unless you knew how to fix it
cin >> choice;
return choice;
}
void setData(
string students[MAX_STUDENTS],
string courses[MAX_COURSES],
double marks[MAX_COURSES][MAX_STUDENTS],
int & num_students,
int & num_courses)
{
// input students their class and their marks
//im not sure if you've been taught how to deal with invalid
//input in streams so lets pray noone puts a 'r' instread of a '6' here
// and also that they dont put higher than 10.
cout << "Number of students(1-" << MAX_STUDENTS << "): ";
cin >> num_students;
cout << "Number of courses(1-" << MAX_COURSES << "): ";
cin >> num_courses;
for (int i = 0; i < num_students; ++i) {
cout << "Student name: ";
getline(cin, students[i]);
}
for (int i = 0; i < num_courses; ++i) {
cout << "Course name: ";
getline(cin, courses[i]);
}
for (int course = 0; course < num_students; ++course) {
for (int student = 0; student < num_courses; ++student) {
cout << "(" << students[student] << ": " << courses[course] << ": ";
//please don't put a 'g' here only numbers D:
cin >> marks[course][student];
}
}
}
void copyData(
string students[MAX_STUDENTS],
double marks[MAX_STUDENTS],
string cpy_students[MAX_STUDENTS],
double cpy_marks[MAX_STUDENTS],
const int NUM_STUDENTS) {
for (int i = 0; i < NUM_STUDENTS; ++i) {
cpy_students[i] = students[i];
cpy_marks[i] = marks[i];
}
}
void sortData(
string cpy_students[],
double cpy_marks[],
const int NUM_STUDENTS) //selection sort b/c idk if you can use <algorithm>
{
for (int i = 0; i < MAX_STUDENTS; ++i) {
int highest_mark = i;
for (int j = i; j < MAX_STUDENTS; ++j)
if (cpy_marks[j] > cpy_marks[highest_mark])
highest_mark = j;
std::swap(cpy_marks[i], cpy_marks[highest_mark]);
std::swap(cpy_students[i], cpy_students[highest_mark]);
}
}
void displayData(
string cpy_students[],
double cpy_marks[],
const int NUM_STUDENTS)
{
for (int i = 0; i < NUM_STUDENTS; ++i)
cout << i + 1 << " " << cpy_students[i] << ": " << cpy_marks[i];
}
void displayAndSortData(
string students[MAX_STUDENTS],
string courses[MAX_COURSES],
double marks[MAX_COURSES][MAX_STUDENTS],
const int NUM_STUDENTS,
const int NUM_COURSES)
{
for (int course = 0; course < NUM_COURSES; ++course) {
string cpy_students[MAX_STUDENTS];
double cpy_marks[MAX_STUDENTS];
copyData(students, marks[course], cpy_students, cpy_marks, NUM_STUDENTS);
sortData(cpy_students, cpy_marks, NUM_STUDENTS);
cout << courses[course] << endl;
displayData(cpy_students, cpy_marks, NUM_STUDENTS);
}
}
bool evalChoice(
char choice,
string students[MAX_STUDENTS],
string courses[MAX_COURSES],
double marks[MAX_COURSES][MAX_STUDENTS],
int & num_students,
int & num_courses)
{
bool cont = true;
switch (choice) {
case '1':
setData(students, courses, marks, num_students, num_courses);
break;
case '2':
displayAndSortData(students, courses, marks, num_students, num_courses);
break;
case '3':
// you fill in the blank for this one :)
break;
case '4':
cont = false;
break;
default:
cout << "A non valid character was entered." << endl;
cout << "Please try again." << endl;
}
return cont;
}
int main() {
bool cont = true; //continue looping?
char choice = 0;
string students[MAX_STUDENTS];
string courses[MAX_COURSES];
double marks[MAX_COURSES][MAX_STUDENTS] = {}; // make all the values 0.
int num_students = 0;
int num_courses = 0;
do {
choice = getChoice();
cont = evalChoice(choice, students, courses, marks, num_students, num_courses);
} while (cont);
}
|