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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <cstdlib>
#include <iostream>

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

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

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