### What am I Doing Wrong :(

Hi guys, my teacher assigned a basic degree planner, I already coded most of it but for some reason I cannot seem to get the Boolean function to work correctly keeps returning true. Maybe a fresh set of eyes can spot what i am doing wrong. Any help is welcomed.

Thanks a Bunch

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104`` ``````#include #include int classesNeeded = 0; int classesNeeded2 = 0; int classesTaken = 0; int classesTaken2 = 0; int classFailed = false; double totalPoints = 0.0; char classGrade; double GPA = 0.0; using namespace std; void enterGrade(char classGrade) { if (classGrade == 'F') { classesTaken ++; classFailed = true; totalPoints = totalPoints + 0.0; } else if (classGrade == 'A') { classesTaken ++; totalPoints = totalPoints + 12.0; } else if (classGrade == 'B') { classesTaken ++; totalPoints = totalPoints + 9.0; } else if (classGrade == 'C') { classesTaken ++; totalPoints = totalPoints + 6.0; } else if (classGrade == 'D') { classesTaken ++; totalPoints = totalPoints + 3.0; } else { cout << "Please Enter a valid Grade... \n" << endl; } } void gpa() { classesTaken2 = classesTaken; GPA = totalPoints / (classesTaken2 * 3); } bool eligibleToGraduate() { if(classFailed == true) { return false; } else if(GPA < 2.0) { return false; } else if(classesTaken != classesNeeded) { return false; } else { return true; } } int main() { cout << "Welcome to my Degree Planner, please tell me how many classes you need to graduate first.." << endl; cin >> classesNeeded2; classesNeeded = classesNeeded2; while (classesNeeded2 > 0) { cout << "Enter your grades... A or a for 90-100 " << endl; cout << "Enter your grades... B or b for 80-89 " << endl; cout << "Enter your grades... C or c for 70-79 " << endl; cout << "Enter your grades... D or d for 60-69 " << endl; cout << "Enter your grades... F or f for anything less than 60 " << endl; cin >> classGrade; enterGrade(classGrade); classesNeeded2 --; cout << "Total Classes Taken: " << classesTaken << endl; cout << "Total Points Accumulated: " << totalPoints << endl; cout << "Classes Failed: " << classFailed << endl; gpa(); cout << "This is you GPA up to this point: " << GPA << endl; eligibleToGraduate(); cout << "Now lets see if you are eligible to graduate: " << eligibleToGraduate << " (1) means yes (0) means no " << endl; } return 0; } ``````
Why did you put your other functions before the main function?
The only reason I put them outside the main was because part of the assignment was to put your functions outside of the main, and have main invoke them.
here `cout << "Now lets see if you are eligible to graduate: " << eligibleToGraduate << .....` eligibleToGraduate is functions so it should be eligibleToGraduate()
omg dude you are awesome, i feel so dumb for overlooking that :( lol

Thanks So much
closed account (Dy7SLyTq)
fun fact... you can call ruby methods(? i cant remember the name for them) that way
First, all of your variables are global. Why?

Here is a better revision of your code:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364`` `````` #include int enterGrade() { char classGrade; std::cout << "\nEnter you grade for the class... \n A for 90% - 100%\n B for 80% - 89%\n C for 70% - 79%\n D for 60% - 69%\n F for 59% or less.\n\n"; std::cin >> classGrade; switch(classGrade) { case 'f': case 'F': return 0; case 'a': case 'A': return 12; case 'b': case 'B': return 9; case 'c': case 'C': return 6; case 'd': case 'D': return 4; default: return enterGrade(); //Recursive function :) My first time using it. } } float getGPA(int score, int classes_taken) { return (score / (classes_taken * 3)); } bool eligibletograd(int GPA) { return (GPA < 2.0); } int main() { std::cout << "Welcome to my Degree Planner. First, please enter how many classes you need to graduate.\n\n"; int num_classes; int score; std::cin >> num_classes; for(int i = 0; i < num_classes; i++) { score += enterGrade(); } std::cout << "Total classes taken: "<< num_classes << "\n\n"; std::cout << "Total points accumulated: " << score << "\n\n"; std::cout << "GPA: " << getGPA(score, num_classes) << "\n\n"; if(eligibletograd(getGPA(score,num_classes))) { std::cout << "You graduated!\n\n"; } else { std::cout << "You didn't graduate.\n\n"; } return 0; }``````

There may be an added feature that you want to put in, but that is easy. Lots of global variables != encapsulation.
You were overwriting the variables before the data needed in them was able to be used in another function. Also, a for loop is really what you need. ;)

Hope this helps!
Last edited on
Topic archived. No new replies allowed.