|
I finally wrote a complex (at least for my level) C++ program for class! We had to write a program that asks for a student's name, ask how many credits and the grade, and the output the student's name, Total credits, and GPA (GPA being the total of all the (credits times grade) for the classes divided by the total number of classes. This is what I got:
#include <iostream.h> #include <stdlib.h> int multiply (int x, int y) { int z; z = x*y; return z; } float divide (float p, float q) {float r; r = p/q; return r; } int main() { char student [20], grade, ans, Y; int sum, ob, numcredits, numclasses, Totalcredits, A, B, C, D, someNum, coursegrade, coursegrades; float Mult, Div, avg; sum= 0; coursegrades= 0; cout << "Would you like to find out your GPA?: "; cin >> ans; while (ans=='Y') { cout << "What is your name?: "; cin >> student; cout << "How many classes?: "; cin >> numclasses; for (int i = 0; i < numclasses; i++) { cout << "How many credits for class?: " << "\n"; cin >> numcredits; sum = sum + numcredits; cout << "What is grade in class?: " << "\n"; cin >> grade; if (grade=='A') {someNum=4; coursegrade = multiply (someNum, numcredits); } else if (grade=='B') {someNum=3; coursegrade = multiply (someNum, numcredits); } else if (grade=='C') {someNum=2; coursegrade = multiply (someNum, numcredits); } else if (grade=='D') {someNum=1; coursegrade = multiply (someNum, numcredits); } else if (grade=='F') {someNum=0; coursegrade = multiply (someNum, numcredits); } coursegrades = coursegrades + coursegrade; } ob = coursegrades; Totalcredits = sum; avg = divide (ob, Totalcredits); cout << "Name: " << student << "\n"; cout << "Total Credits: " << Totalcredits << "\n"; cout << "GPA: " << avg << "\n"; cout << "\n"; cout << "Would you like to find out your GPA?: "; cin >> ans; } system("PAUSE"); return 0; }
You can check it out if you want, but it seems to be working fine for me according to what the teacher asked the program to do. I'm surprised I was actually able to write this with little to no help (I asked for help on assigning a numerical value to a letter, but I figured it out before I got the response). Thanks for the inspiration guys!
|