program blows up

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

program blows up

 
0
  #1
Mar 27th, 2008
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:
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int Get_Name_and_Num_Classes(string prompt);
  5. double Calculate_GPA(char grade, int number);
  6. void Get_Letter_Grade(char grade);
  7. int Convert_Let_to_Num(int number);
  8. void Print_Results(string name, double gpa);
  9.  
  10. int main()
  11. {
  12. string name;
  13. int count = 1;
  14. int classes, number = 0;
  15. char grade = 0;
  16. double gpa = 0.0;
  17.  
  18. cout << "Programmed by Jim Johnson";
  19. cout << endl << endl << "Type xxx for the name to Exit";
  20.  
  21. do
  22. {
  23. cout << endl << endl << "Enter the student's last name: ";
  24. name = Get_Name_and_Num_Classes("name");
  25.  
  26. while (name != "xxx")
  27. {
  28. cout << endl << "How many classes taken: ";
  29. classes = Get_Name_and_Num_Classes("classes");
  30.  
  31. }while (count <= classes)
  32.  
  33. {
  34. cout << endl << endl << "Enter letter grade " << count << ": ";
  35. Get_Letter_Grade(grade);
  36. count ++;
  37. }
  38. //gpa = Calculate_GPA(grade, count);
  39. number = Convert_Let_to_Num(number);
  40. Print_Results(name, gpa);
  41. }while (name != "xxx");
  42.  
  43. return 0;
  44. }
  45.  
  46. int Get_Name_and_Num_Classes(string variable)
  47. {
  48. int x = 0;
  49. cin >> x;
  50. return x;
  51.  
  52. }
  53.  
  54. void Get_Letter_Grade(char grade)
  55. {
  56.  
  57. cin >> grade;
  58.  
  59.  
  60. }
  61.  
  62. //calculate gpa here
  63.  
  64. int Convert_Let_to_Num(int number)
  65. {
  66. char grade = 0;
  67. if ((grade == 'a') || (grade == 'A'))
  68. {
  69. number = 4;
  70. }
  71. if ((grade == 'b') || (grade == 'B'))
  72. {
  73. number = 3;
  74. }
  75. if ((grade == 'c') || (grade == 'C'))
  76. {
  77. number = 2;
  78. }
  79. if ((grade == 'd') || (grade == 'D'))
  80. {
  81. number = 1;
  82. }
  83. if ((grade == 'f') || (grade == 'F'))
  84. {
  85. number = 0;
  86. }
  87. return (number);
  88. }
  89.  
  90. void Print_Results(string name, double gpa)
  91. {
  92. cout << "Student " << name << " has a semester GPA of " << gpa;
  93. }
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
Last edited by Ancient Dragon; Mar 27th, 2008 at 1:56 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,658
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: program blows up

 
0
  #2
Mar 27th, 2008
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)
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

Re: program blows up

 
0
  #3
Mar 27th, 2008
not allowed to use arrays
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,658
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: program blows up

 
0
  #4
Mar 27th, 2008
Ok, so another way to do it is to sum up all the grades and divide the the number of grades. Such as:
  1. while (count <= classes)
  2. {
  3. number = Get_Letter_Grade(grade);
  4. number = Convert_Let_to_Num(number);
  5. sum = sum + number;
  6. count = count + 1;
  7. }
  8. 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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

Re: program blows up

 
0
  #5
Mar 27th, 2008
still blowing up.....

  1.  
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. int Get_Name_and_Num_Classes(string prompt);
  6. double Calculate_GPA(char grade, int number);
  7. void Get_Letter_Grade(char grade);
  8. int Convert_Let_to_Num(int number);
  9. void Print_Results(string name, double gpa);
  10.  
  11. int main()
  12. {
  13. string name;
  14. int classes, sum, count = 1, number = 0;
  15. char grade = 0;
  16. double gpa = 0.0;
  17.  
  18. cout << "Programmed by Jim Johnson";
  19. cout << endl << endl << "Type xxx for the name to Exit";
  20.  
  21. do
  22. {
  23. cout << endl << endl << "Enter the student's last name: ";
  24. name = Get_Name_and_Num_Classes("name");
  25.  
  26. while (name != "xxx")
  27. {
  28. cout << endl << "How many classes taken: ";
  29. classes = Get_Name_and_Num_Classes("classes");
  30.  
  31. }while (count <= classes)
  32.  
  33. {
  34. cout << endl << endl << "Enter letter grade " << count << ": ";
  35. Get_Letter_Grade(grade);
  36. number = Convert_Let_to_Num(number);
  37. sum = sum + number;
  38. count = count + 1;
  39. count ++;
  40. }
  41. //gpa = Calculate_GPA(grade, count);
  42. number = Convert_Let_to_Num(number);
  43. Print_Results(name, gpa);
  44. }while (name != "xxx");
  45.  
  46. return 0;
  47. }
  48.  
  49. int Get_Name_and_Num_Classes(string variable)
  50. {
  51. int x = 0;
  52. cin >> x;
  53. return x;
  54.  
  55. }
  56.  
  57. void Get_Letter_Grade(char grade)
  58. {
  59.  
  60. cin >> grade;
  61.  
  62.  
  63. }
  64.  
  65. //calculate gpa here
  66.  
  67. int Convert_Let_to_Num(int number)
  68. {
  69. char grade = 0;
  70. if ((grade == 'a') || (grade == 'A'))
  71. {
  72. number = 4;
  73. }
  74. if ((grade == 'b') || (grade == 'B'))
  75. {
  76. number = 3;
  77. }
  78. if ((grade == 'c') || (grade == 'C'))
  79. {
  80. number = 2;
  81. }
  82. if ((grade == 'd') || (grade == 'D'))
  83. {
  84. number = 1;
  85. }
  86. if ((grade == 'f') || (grade == 'F'))
  87. {
  88. number = 0;
  89. }
  90. return (number);
  91. }
  92.  
  93. void Print_Results(string name, double gpa)
  94. {
  95. cout << "Student " << name << " has a semester GPA of " << gpa;
  96. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

Re: program blows up

 
0
  #6
Mar 27th, 2008
I tried putting in by reference and this is what I got.....

  1.  
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. int Get_Name_and_Num_Classes(string prompt);
  6. double Calculate_GPA(char& grade, int number);
  7. void Get_Letter_Grade(char grade);
  8. int Convert_Let_to_Num(int number);
  9. void Print_Results(string name, double gpa);
  10.  
  11. int main()
  12. {
  13. string name;
  14. int classes, sum = 0, count = 1, number = 0;
  15. char grade = 0;
  16. double gpa = 0.0;
  17.  
  18. cout << "Programmed by Jim Johnson";
  19. cout << endl << endl << "Type xxx for the name to Exit";
  20.  
  21. do
  22. {
  23. cout << endl << endl << "Enter the student's last name: ";
  24. name = Get_Name_and_Num_Classes("name");
  25.  
  26. while (name != "xxx")
  27. {
  28. cout << endl << "How many classes taken: ";
  29. classes = Get_Name_and_Num_Classes("classes");
  30.  
  31. }while (count <= classes)
  32.  
  33. {
  34. cout << endl << endl << "Enter letter grade " << count << ": ";
  35. Get_Letter_Grade(grade);
  36. number = Convert_Let_to_Num(number);
  37. sum = sum + number;
  38. count = count + 1;
  39. count ++;
  40. }
  41. //gpa = Calculate_GPA(grade, count);
  42. number = Convert_Let_to_Num(number);
  43. Print_Results(name, gpa);
  44. }while (name != "xxx");
  45.  
  46. return 0;
  47. }
  48.  
  49. int Get_Name_and_Num_Classes(string variable)
  50. {
  51. int x = 0;
  52. cin >> x;
  53. return x;
  54.  
  55. }
  56.  
  57. void Get_Letter_Grade(char& grade)
  58. {
  59.  
  60. cin >> grade;
  61.  
  62.  
  63. }
  64.  
  65. //calculate gpa here
  66.  
  67. int Convert_Let_to_Num(int number)
  68. {
  69. char grade = 0;
  70. if ((grade == 'a') || (grade == 'A'))
  71. {
  72. number = 4;
  73. }
  74. if ((grade == 'b') || (grade == 'B'))
  75. {
  76. number = 3;
  77. }
  78. if ((grade == 'c') || (grade == 'C'))
  79. {
  80. number = 2;
  81. }
  82. if ((grade == 'd') || (grade == 'D'))
  83. {
  84. number = 1;
  85. }
  86. if ((grade == 'f') || (grade == 'F'))
  87. {
  88. number = 0;
  89. }
  90. return (number);
  91. }
  92.  
  93. void Print_Results(string name, double gpa)
  94. {
  95. cout << "Student " << name << " has a semester GPA of " << gpa;
  96. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,658
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: program blows up

 
0
  #7
Mar 27th, 2008
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

Re: program blows up

 
0
  #8
Mar 27th, 2008
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:

  1.  
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. int Get_Name_and_Num_Classes(string prompt);
  6. double Calculate_GPA(char& grade, int number);
  7. void Get_Letter_Grade(char grade);
  8. int Convert_Let_to_Num(int number);
  9. void Print_Results(string name, double gpa);
  10.  
  11. int main()
  12. {
  13. string name;
  14. int classes, sum = 0, count = 1, number = 0;
  15. char grade = 0;
  16. double gpa = 0.0;
  17.  
  18. cout << "Programmed by Jim Johnson";
  19. cout << endl << endl << "Type xxx for the name to Exit";
  20.  
  21. do
  22. {
  23. cout << endl << endl << "Enter the student's last name: ";
  24. name = Get_Name_and_Num_Classes("name");
  25.  
  26. while (name != "xxx")
  27. {
  28. cout << endl << "How many classes taken: ";
  29. classes = Get_Name_and_Num_Classes("classes");
  30.  
  31. }while (count <= classes)
  32.  
  33. {
  34. cout << endl << endl << "Enter letter grade " << count << ": ";
  35. Get_Letter_Grade(grade);
  36. number = Convert_Let_to_Num(grade);
  37. sum = sum + number;
  38. count = count + 1;
  39. count ++;
  40. }
  41. //gpa = Calculate_GPA(grade, count);
  42. number = Convert_Let_to_Num(number);
  43. Print_Results(name, gpa);
  44. }while (name != "xxx");
  45.  
  46. return 0;
  47. }
  48.  
  49. int Get_Name_and_Num_Classes(string variable)
  50. {
  51. int x = 0;
  52. cin >> x;
  53. return x;
  54.  
  55. }
  56.  
  57. void Get_Letter_Grade(char& grade)
  58. {
  59.  
  60. cin >> grade;
  61.  
  62.  
  63. }
  64.  
  65. //calculate gpa here
  66.  
  67. int Convert_Let_to_Num(int number)
  68. {
  69. char grade = 0;
  70. if ((grade == 'a') || (grade == 'A'))
  71. {
  72. number = 4;
  73. }
  74. if ((grade == 'b') || (grade == 'B'))
  75. {
  76. number = 3;
  77. }
  78. if ((grade == 'c') || (grade == 'C'))
  79. {
  80. number = 2;
  81. }
  82. if ((grade == 'd') || (grade == 'D'))
  83. {
  84. number = 1;
  85. }
  86. if ((grade == 'f') || (grade == 'F'))
  87. {
  88. number = 0;
  89. }
  90. return (number);
  91. }
  92.  
  93. void Print_Results(string name, double gpa)
  94. {
  95. cout << "Student " << name << " has a semester GPA of " << gpa;
  96. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: program blows up

 
0
  #9
Mar 27th, 2008
int Convert_Let_to_Num(int number)
{
	char grade = 0;
	if ((grade == 'a') || (grade == 'A'))
	{
		number = 4;
Q1: What answer do you expect given that you set grade to 0, then proceed to test it against various letters.

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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,658
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: program blows up

 
0
  #10
Mar 27th, 2008
line 5: the parameter prompt is misnamed -- should be name and passed by reference 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
  1. std::string name;
  2. int numClasses = 0;
  3. int sum = 0;
  4. do
  5. {
  6. numClasses = Get_Name_and_Num_Classes(name);
  7. if( name != "xxx" )
  8. {
  9. GPA = Calculate_GPA(numClasses);
  10. }
  11. } 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 2110 | Replies: 39
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC