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.
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()
#include <iostream>
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. ;)