I created this thread not only to tell everyone about my personal achievement but to teach others that are new to C++. My code simply demonstrates the basics of C++:
Code:
/* Plain Practice for Myself ~ aTTiC */
/* v1.0 */
#include <iostream> // Include 2 Headers
#include <string>
using namespace std; // This is optional; cout instead of std::cout
struct Students // Characteristics of Students (main battery for code)
{
char Name [80];
string Grade;
short testScore [3], Average;
};
Students*worms1 = NULL; // Pointer for Students to be used in functions
void assignCharacteristics (void); // Functions
void changeSettings (void);
void displayGrade (void);
short MAX = NULL; // Variable to determine the amount of students (Global Variable)
int main (void)
{
short amountStudents; // Variable to determine the amount of students (Local Variable)
cout << "Enter # of students: "; // Displays "Enter # of Students" and expects a numerical value
cin >> amountStudents;
cin.ignore ();
cout << "Amount of students = " << amountStudents << endl << endl;
MAX = amountStudents; // Local Variable will have the full scope w/ the name of MAX
assignCharacteristics (); // All the functions doing their work
changeSettings ();
displayGrade ();
return 0; // int main (void) returns o since int is an integer.
}
void assignCharacteristics (void)
{
Students*worms = new Students [MAX]; // Dynamic Memory Allocation to determine the amount of Students is present
worms1 = worms; // Applies the pointer mentioned all the way above to the Dynamic MA
for (short x = 0; x < MAX; x++) // Loop starts
{
cout << "Student #" << x + 1 << "'s name is: "; // 1st Partion of the loop; Makes you input names for each student
cin.getline (worms [x].Name, 80);
cout << endl;
cout << worms [x].Name << ";" << endl;
for (short y = 0; y < 3; y++) // Sub loop; makes you input the test scores for x student
{
cout << "Test score #" << y + 1 << ": ";
cin >> worms [x].testScore [y];
cin.ignore ();
if (worms [x].testScore [y] > 100) // Test scores are from 0 to 100. If it surpasses ethier, it will reset to closest
worms [x].testScore [y] = 100;
else if (worms [x].testScore [y] < 0)
worms [x].testScore [y] = 0;
}
cout << endl;
}
} // Ends this function-NEXT FUNCTION
void changeSettings (void)
{
short Choice;
bool Continue = false;
do // The menu. do...while loop is always shown at least once
{
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: // If 1st option was selected
{
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++) // loop displays all students
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++) // Determines which student was picked and changes name
{
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: // If 2nd option was chosen
{
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++) // loop displays all students
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++) // determines which students was selected
{
if (Choice == (x + 1))
{
cout << endl;
cout << "'" << worms1 [x].Name << "' selected and grades are below:" << endl << endl;
for (short y = 0; y < 3; y++) // once determined, displays students test scores
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: // If 3rd option was selected
Continue = true;
break;
default: // If user inputted any other character
Continue = true;
}
}
while (!Continue);
} // Function ended
void displayGrade (void)
{
cout << endl;
cout << "**GRADE DETERMINATION**" << endl;
cout << "----" << endl;
for (short x = 0; x < MAX; x++) // displays students name, average, grade
{
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;
}
}
Use this code in a Console Application and run it.
What this program does:
Tells user how many students are in class
Then applies the characteristics to them (Test Scores, Name) depending on your input
After that, it gives you a menu that lets you change the students names and scores one last time
Last, calculates the students average and gives them a final grade (A+, C-, D+, ETC.)
I learned all the code names and what not by reading and finishing my first C++ book called "C++ Demystified" by Jeff Kent.
I don't care if you claim this code is yours due to the fact I just don't care. I'm the one learning, not you. Anyways, on that note, I'll leave a farewell. If you have any questions on my code then please... don't hesitate to ask. Thank you.
*My pseudo code looked A LOT simpler
**If you know anything to shorten the length of my code please say so
EDIT: CHANGED CODE w/ HELP SECTIONS "//"