Tips on Making my Code Shorter?
Jan 28, 2012 at 2:16pm UTC
Awhile ago I created a C++ program that has a structure that contains students names, students grades and their overall grade (A, B, C-); just to practice. This code works in a Console Application and I have to tell ya, it's pretty neat (as in awesome (my opinion)). I'm a bit rusty due to the fact I've been playing MMOs for the past 3 weeks. Also, the title of this topic explains what I'm asking for. Thanks!
Note: The code is decent in size so read it when you have time. This isn't a code red topic :)
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
/* Plain Practice for Myself ~ aTTiC, Brian, Brain, PopEye <- Multiple Usernames */
/* v1.0 */
#include <iostream>
#include <string>
using namespace std;
struct Students
{
char Name [80];
string Grade;
short testScore [3], Average;
};
Students*worms1 = NULL;
void assignCharacteristics (void );
void changeSettings (void );
void displayGrade (void );
short MAX = NULL;
int main (void )
{
short amountStudents;
cout << "Enter # of students: " ;
cin >> amountStudents;
cin.ignore ();
cout << "Amount of students = " << amountStudents << endl << endl;
MAX = amountStudents;
assignCharacteristics ();
changeSettings ();
displayGrade ();
return 0;
}
void assignCharacteristics (void )
{
Students*worms = new Students [MAX];
worms1 = worms;
for (short x = 0; x < MAX; x++)
{
cout << "Student #" << x + 1 << "'s name is: " ;
cin.getline (worms [x].Name, 80);
cout << endl;
cout << worms [x].Name << ";" << endl;
for (short y = 0; y < 3; y++)
{
cout << "Test score #" << y + 1 << ": " ;
cin >> worms [x].testScore [y];
cin.ignore ();
if (worms [x].testScore [y] > 100)
worms [x].testScore [y] = 100;
else if (worms [x].testScore [y] < 0)
worms [x].testScore [y] = 0;
}
cout << endl;
}
}
void changeSettings (void )
{
short Choice;
bool Continue = false ;
do
{
bool loopContinue = true ;
cout << "*****MENU*****" << endl;
cout << "1 - Change Names" << endl;
cout << "2 - Change Grades" << endl;
cout << "3 - Continue" << endl;
cout << "Default - Continue" << endl;
cout << "Choice: " ;
cin >> Choice;
cin.ignore ();
if (Choice < 1 || Choice > 3)
Choice = 0;
cout << endl;
switch (Choice)
{
case 1:
{
cout << "**'Change Names' selected**" << endl;
cout << "Choose 1 Student to whom you would like to change names:" << endl;
for (short x = 0; x < MAX; x++)
cout << x + 1 << " - " << worms1 [x].Name << endl;
cout << "Choice: " ;
cin >> Choice;
cin.ignore ();
if (Choice < 1)
break ;
else if (Choice > MAX)
break ;
for (short x = 0; (x < MAX) && (loopContinue == true ); x++)
{
if (Choice == (x + 1))
{
cout << endl;
cout << "'" << worms1 [x].Name << "' selected" << endl;
cout << "Type new name for this student: " ;
cin.getline (worms1 [x].Name, 80);
cout << "New Name for student #" << x + 1 << " is " << worms1 [x].Name << endl << endl;
loopContinue = false ;
}
}
}
break ;
case 2:
{
cout << "**'Change Grades' selected**" << endl;
cout << "Choose 1 student to whom you would like to change grades:" << endl;
for (short x = 0; x < MAX; x++)
cout << x + 1 << " - " << worms1 [x].Name << endl;
cout << "Choice: " ;
cin >> Choice;
cin.ignore ();
if (Choice < 1)
break ;
else if (Choice > MAX)
break ;
for (short x = 0; (x < MAX) && (loopContinue == true ); x++)
{
if (Choice == (x + 1))
{
cout << endl;
cout << "'" << worms1 [x].Name << "' selected and grades are below:" << endl << endl;
for (short y = 0; y < 3; y++)
cout << y + 1 << " - " << "Test Score: " << worms1 [x].testScore [y] << endl;
cout << "Choice: " ;
cin >> Choice;
cin.ignore ();
if (Choice < 1)
break ;
else if (Choice > 3)
break ;
cout << endl;
cout << "Change score " << worms1 [x].testScore [Choice - 1] << " to: " ;
cin >> worms1 [x].testScore [Choice - 1];
cin.ignore ();
if (worms1 [x].testScore [Choice - 1] > 100)
worms1 [x].testScore [Choice - 1] = 100;
else if (worms1 [x].testScore [Choice - 1] < 0)
worms1 [x].testScore [Choice - 1] = 0;
cout << "Changed!" << endl << endl;
loopContinue = false ;
}
}
}
break ;
case 3:
Continue = true ;
break ;
default :
Continue = true ;
}
}
while (!Continue);
}
void displayGrade (void )
{
cout << endl;
cout << "**GRADE DETERMINATION**" << endl;
cout << "----" << endl;
for (short x = 0; x < MAX; x++)
{
cout << worms1 [x].Name << ";" << endl;
short sum = 0;
for (short y = 0; y < 3; y++)
{
cout << "Test Score #" << y + 1 << " - " << worms1 [x].testScore [y] << endl;
sum += worms1 [x].testScore [y];
}
worms1 [x].Average = (sum / 3);
if (worms1 [x].Average >= 98)
worms1 [x].Grade = "A+" ;
else if ((worms1 [x].Average >= 95) && (worms1 [x].Average < 98))
worms1 [x].Grade = "A" ;
else if ((worms1 [x].Average >= 90) && (worms1 [x].Average < 95))
worms1 [x].Grade = "A-" ;
else if ((worms1 [x].Average >= 87) && (worms1 [x].Average < 90))
worms1 [x].Grade = "B+" ;
else if ((worms1 [x].Average >= 82) && (worms1 [x].Average < 87))
worms1 [x].Grade = "B" ;
else if ((worms1 [x].Average >= 79) && (worms1 [x].Average < 82))
worms1 [x].Grade = "B-" ;
else if ((worms1 [x].Average >= 74) && (worms1 [x].Average < 79))
worms1 [x].Grade = "C+" ;
else if ((worms1 [x].Average >= 71) && (worms1 [x].Average < 74))
worms1 [x].Grade = "C" ;
else if ((worms1 [x].Average >= 66) && (worms1 [x].Average < 71))
worms1 [x].Grade = "C-" ;
else if ((worms1 [x].Average >= 63) && (worms1 [x].Average < 66))
worms1 [x].Grade = "D+" ;
else if ((worms1 [x].Average >= 55) && (worms1 [x].Average < 63))
worms1 [x].Grade = "D" ;
else
worms1 [x].Grade = "F" ;
cout << "Average = " << worms1 [x].Average << endl;
cout << worms1 [x].Name << "'s Final Grade: " << worms1 [x].Grade << endl << endl;
}
}
Topic archived. No new replies allowed.