| | |
program blows up
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2008
Posts: 189
Reputation:
Solved Threads: 0
I know it has to do something with my main and around the "how many classes" but my program runs without any errors or warnings but blows up so I was wanting to know if someone could take a look at it.....
I know I havent worked on the gpa so that can be ignored for now....
my code:
output should be:
Programmed by <your name>
Type xxx for the name to exit
Enter the student's last name: Mickey
How many classes taken: 3
Enter letter grade 1: a
Enter letter grade 2: B
Enter letter grade 3: b
Student Mickey has a semester GPA of 3.33
Enter the student's last name: Goofy
How many classes taken: 5
Enter letter grade 1: c
Enter letter grade 2: C
Enter letter grade 3: d
Enter letter grade 4: b
Enter letter grade 5: a
Student Goofy has a semester GPA of 2.40
Enter the student's last name: Donald
How many classes taken: 2
Enter letter grade 1: c
Enter letter grade 2: B
Student Donald has a semester GPA of 2.50
Enter the student's last name: Minnie
How many classes taken: 4
Enter letter grade 1: A
Enter letter grade 2: A
Enter letter grade 3: B
Enter letter grade 4: A
Student Mickey has a semester GPA of 3.75
Enter the student's last name: xxx
I know I havent worked on the gpa so that can be ignored for now....
my code:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int Get_Name_and_Num_Classes(string prompt); double Calculate_GPA(char grade, int number); void Get_Letter_Grade(char grade); int Convert_Let_to_Num(int number); void Print_Results(string name, double gpa); int main() { string name; int count = 1; int classes, number = 0; char grade = 0; double gpa = 0.0; cout << "Programmed by Jim Johnson"; cout << endl << endl << "Type xxx for the name to Exit"; do { cout << endl << endl << "Enter the student's last name: "; name = Get_Name_and_Num_Classes("name"); while (name != "xxx") { cout << endl << "How many classes taken: "; classes = Get_Name_and_Num_Classes("classes"); }while (count <= classes) { cout << endl << endl << "Enter letter grade " << count << ": "; Get_Letter_Grade(grade); count ++; } //gpa = Calculate_GPA(grade, count); number = Convert_Let_to_Num(number); Print_Results(name, gpa); }while (name != "xxx"); return 0; } int Get_Name_and_Num_Classes(string variable) { int x = 0; cin >> x; return x; } void Get_Letter_Grade(char grade) { cin >> grade; } //calculate gpa here int Convert_Let_to_Num(int number) { char grade = 0; if ((grade == 'a') || (grade == 'A')) { number = 4; } if ((grade == 'b') || (grade == 'B')) { number = 3; } if ((grade == 'c') || (grade == 'C')) { number = 2; } if ((grade == 'd') || (grade == 'D')) { number = 1; } if ((grade == 'f') || (grade == 'F')) { number = 0; } return (number); } void Print_Results(string name, double gpa) { cout << "Student " << name << " has a semester GPA of " << gpa; }
Programmed by <your name>
Type xxx for the name to exit
Enter the student's last name: Mickey
How many classes taken: 3
Enter letter grade 1: a
Enter letter grade 2: B
Enter letter grade 3: b
Student Mickey has a semester GPA of 3.33
Enter the student's last name: Goofy
How many classes taken: 5
Enter letter grade 1: c
Enter letter grade 2: C
Enter letter grade 3: d
Enter letter grade 4: b
Enter letter grade 5: a
Student Goofy has a semester GPA of 2.40
Enter the student's last name: Donald
How many classes taken: 2
Enter letter grade 1: c
Enter letter grade 2: B
Student Donald has a semester GPA of 2.50
Enter the student's last name: Minnie
How many classes taken: 4
Enter letter grade 1: A
Enter letter grade 2: A
Enter letter grade 3: B
Enter letter grade 4: A
Student Mickey has a semester GPA of 3.75
Enter the student's last name: xxx
Last edited by Ancient Dragon; Mar 27th, 2008 at 1:56 pm. Reason: add code tags
line 35: you need to pass variable grade by reference to that Get_Letter_Grade() can change its value. Then in main() you need to save all the graded in an array so that the GPA can be calculated.
line 39: the value of number is never set to anything so all it is passing to Convert_Let_to_Num() is the3 value 0 (line 14)
line 39: the value of number is never set to anything so all it is passing to Convert_Let_to_Num() is the3 value 0 (line 14)
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Ok, so another way to do it is to sum up all the grades and divide the the number of grades. Such as:
C++ Syntax (Toggle Plain Text)
while (count <= classes) { number = Get_Letter_Grade(grade); number = Convert_Let_to_Num(number); sum = sum + number; count = count + 1; } GPA = sum / classes;
Last edited by Ancient Dragon; Mar 27th, 2008 at 2:23 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jan 2008
Posts: 189
Reputation:
Solved Threads: 0
still blowing up.....
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int Get_Name_and_Num_Classes(string prompt); double Calculate_GPA(char grade, int number); void Get_Letter_Grade(char grade); int Convert_Let_to_Num(int number); void Print_Results(string name, double gpa); int main() { string name; int classes, sum, count = 1, number = 0; char grade = 0; double gpa = 0.0; cout << "Programmed by Jim Johnson"; cout << endl << endl << "Type xxx for the name to Exit"; do { cout << endl << endl << "Enter the student's last name: "; name = Get_Name_and_Num_Classes("name"); while (name != "xxx") { cout << endl << "How many classes taken: "; classes = Get_Name_and_Num_Classes("classes"); }while (count <= classes) { cout << endl << endl << "Enter letter grade " << count << ": "; Get_Letter_Grade(grade); number = Convert_Let_to_Num(number); sum = sum + number; count = count + 1; count ++; } //gpa = Calculate_GPA(grade, count); number = Convert_Let_to_Num(number); Print_Results(name, gpa); }while (name != "xxx"); return 0; } int Get_Name_and_Num_Classes(string variable) { int x = 0; cin >> x; return x; } void Get_Letter_Grade(char grade) { cin >> grade; } //calculate gpa here int Convert_Let_to_Num(int number) { char grade = 0; if ((grade == 'a') || (grade == 'A')) { number = 4; } if ((grade == 'b') || (grade == 'B')) { number = 3; } if ((grade == 'c') || (grade == 'C')) { number = 2; } if ((grade == 'd') || (grade == 'D')) { number = 1; } if ((grade == 'f') || (grade == 'F')) { number = 0; } return (number); } void Print_Results(string name, double gpa) { cout << "Student " << name << " has a semester GPA of " << gpa; }
•
•
Join Date: Jan 2008
Posts: 189
Reputation:
Solved Threads: 0
I tried putting in by reference and this is what I got.....
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int Get_Name_and_Num_Classes(string prompt); double Calculate_GPA(char& grade, int number); void Get_Letter_Grade(char grade); int Convert_Let_to_Num(int number); void Print_Results(string name, double gpa); int main() { string name; int classes, sum = 0, count = 1, number = 0; char grade = 0; double gpa = 0.0; cout << "Programmed by Jim Johnson"; cout << endl << endl << "Type xxx for the name to Exit"; do { cout << endl << endl << "Enter the student's last name: "; name = Get_Name_and_Num_Classes("name"); while (name != "xxx") { cout << endl << "How many classes taken: "; classes = Get_Name_and_Num_Classes("classes"); }while (count <= classes) { cout << endl << endl << "Enter letter grade " << count << ": "; Get_Letter_Grade(grade); number = Convert_Let_to_Num(number); sum = sum + number; count = count + 1; count ++; } //gpa = Calculate_GPA(grade, count); number = Convert_Let_to_Num(number); Print_Results(name, gpa); }while (name != "xxx"); return 0; } int Get_Name_and_Num_Classes(string variable) { int x = 0; cin >> x; return x; } void Get_Letter_Grade(char& grade) { cin >> grade; } //calculate gpa here int Convert_Let_to_Num(int number) { char grade = 0; if ((grade == 'a') || (grade == 'A')) { number = 4; } if ((grade == 'b') || (grade == 'B')) { number = 3; } if ((grade == 'c') || (grade == 'C')) { number = 2; } if ((grade == 'd') || (grade == 'D')) { number = 1; } if ((grade == 'f') || (grade == 'F')) { number = 0; } return (number); } void Print_Results(string name, double gpa) { cout << "Student " << name << " has a semester GPA of " << gpa; }
line 49: Get_Name_and_Num_Classes() -- that seems to be a misnamed function. Your program expects it to return a string from line 24 but it actually returns an integer. You could hack it to return both a string and an integer but it would be a lot better to use two different functions -- GetNumClasses() and GetName(). If you do that then there will be no confusion about what it should return. And neither function needs a parameter.
line 36 of your most recent attempt is incorrect. Pass grade as the parameter, not number.
line 36 of your most recent attempt is incorrect. Pass grade as the parameter, not number.
Last edited by Ancient Dragon; Mar 27th, 2008 at 3:02 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jan 2008
Posts: 189
Reputation:
Solved Threads: 0
we are not allowed to do that either....here let me give u the instructions for this program so you might have a better understanding.
Write a program to compute a student’s GPA for one semester. It should ask the user for the student’s last name, and how many classes were taken during the semester. It will then ask the user to type in the letter grade for each class and print the GPA to the screen.
The program should continue doing this until the user types an xxx for the last name.
Program Requirements
• To make the program easier, assume all classes are 3 credits.
• There should be two digits of precision
• You should have the following functions:
• Get_Name_and_Num_Classes - This function asks the user to type in their name and the number of classes taken
• Calculate_GPA - This function computes GPA. It does this by looping. It call Get_Letter_Grade to allow the user to enter a valid letter grade and then it calls Convert_Let_to_Num. It uses this information to compute the GPA. It returns the GPA. (you should be able to determine the formal parameters)
• Get_Letter_Grade – This function is a void function. It asks the user to type in a letter grade, and then makes sure that the letter grade is valid. It must be an A, B, C, D, or F. You should be able to determine the formal parameters.
• Convert_Let_to_Num – Takes in a letter grade and converts it to its number equivalent (A–4, B–3, C–2, D–1, F–0) It should return the number equivalent.
• Print_Results - This prints all results to the screen. It should not return a value.
here is my code:
Write a program to compute a student’s GPA for one semester. It should ask the user for the student’s last name, and how many classes were taken during the semester. It will then ask the user to type in the letter grade for each class and print the GPA to the screen.
The program should continue doing this until the user types an xxx for the last name.
Program Requirements
• To make the program easier, assume all classes are 3 credits.
• There should be two digits of precision
• You should have the following functions:
• Get_Name_and_Num_Classes - This function asks the user to type in their name and the number of classes taken
• Calculate_GPA - This function computes GPA. It does this by looping. It call Get_Letter_Grade to allow the user to enter a valid letter grade and then it calls Convert_Let_to_Num. It uses this information to compute the GPA. It returns the GPA. (you should be able to determine the formal parameters)
• Get_Letter_Grade – This function is a void function. It asks the user to type in a letter grade, and then makes sure that the letter grade is valid. It must be an A, B, C, D, or F. You should be able to determine the formal parameters.
• Convert_Let_to_Num – Takes in a letter grade and converts it to its number equivalent (A–4, B–3, C–2, D–1, F–0) It should return the number equivalent.
• Print_Results - This prints all results to the screen. It should not return a value.
here is my code:
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int Get_Name_and_Num_Classes(string prompt); double Calculate_GPA(char& grade, int number); void Get_Letter_Grade(char grade); int Convert_Let_to_Num(int number); void Print_Results(string name, double gpa); int main() { string name; int classes, sum = 0, count = 1, number = 0; char grade = 0; double gpa = 0.0; cout << "Programmed by Jim Johnson"; cout << endl << endl << "Type xxx for the name to Exit"; do { cout << endl << endl << "Enter the student's last name: "; name = Get_Name_and_Num_Classes("name"); while (name != "xxx") { cout << endl << "How many classes taken: "; classes = Get_Name_and_Num_Classes("classes"); }while (count <= classes) { cout << endl << endl << "Enter letter grade " << count << ": "; Get_Letter_Grade(grade); number = Convert_Let_to_Num(grade); sum = sum + number; count = count + 1; count ++; } //gpa = Calculate_GPA(grade, count); number = Convert_Let_to_Num(number); Print_Results(name, gpa); }while (name != "xxx"); return 0; } int Get_Name_and_Num_Classes(string variable) { int x = 0; cin >> x; return x; } void Get_Letter_Grade(char& grade) { cin >> grade; } //calculate gpa here int Convert_Let_to_Num(int number) { char grade = 0; if ((grade == 'a') || (grade == 'A')) { number = 4; } if ((grade == 'b') || (grade == 'B')) { number = 3; } if ((grade == 'c') || (grade == 'C')) { number = 2; } if ((grade == 'd') || (grade == 'D')) { number = 1; } if ((grade == 'f') || (grade == 'F')) { number = 0; } return (number); } void Print_Results(string name, double gpa) { cout << "Student " << name << " has a semester GPA of " << gpa; }
int Convert_Let_to_Num(int number) { char grade = 0; if ((grade == 'a') || (grade == 'A')) { number = 4;
Q2: If all you do is assign 1 to 4 to number, what do you think happens to the value you pass as a parameter?
line 5: the parameter prompt is misnamed -- should be name and passed by reference
you can delete lines 23 and 24 because that info belongs in function Get_Name_and_Num_Classes().
I think the do loop lines 21-44 can be abbreviated
int Get_Name_and_Num_Classes(string& name); then change line 24 to use the above function prototype and change the function beginning at line 45 to do as instructed storing the name the the user typed into the parameter name and returning the number of classes. The function you wrote does not do that.you can delete lines 23 and 24 because that info belongs in function Get_Name_and_Num_Classes().
I think the do loop lines 21-44 can be abbreviated
C++ Syntax (Toggle Plain Text)
std::string name; int numClasses = 0; int sum = 0; do { numClasses = Get_Name_and_Num_Classes(name); if( name != "xxx" ) { GPA = Calculate_GPA(numClasses); } } while( name != "xxx");
Last edited by Ancient Dragon; Mar 27th, 2008 at 6:19 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Help Porn Pop-Ups, Error #317 and a red circle icon with white X in Tray (Viruses, Spyware and other Nasties)
- Help needed please VB5 (Visual Basic 4 / 5 / 6)
- Vista programming in C++? (Windows Vista and Windows 7)
- connecting characters (C)
- How can I make a download when my program is opened? (Pascal and Delphi)
- Coding Question (C++)
- spyware has taken control of my pc (Viruses, Spyware and other Nasties)
- error #317 (yep another one ! ) (Viruses, Spyware and other Nasties)
- Need XP Partitioning Advice (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: to split a string using operator overloading
- Next Thread: Complex Number Class
Views: 2110 | Replies: 39
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






