I need to calculate the grade of a student in one method

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
#include <iostream>
using namespace std;

void qualityPoints();

int main()
{
	qualityPoints();


	system("pause");
	return 0;
}


void qualityPoints()
{
	int studentGrade;
	cout << "Type in your grade average 0 - 100 percent: " << endl;
	cin >> studentGrade;

	if (studentGrade >= 90) {
		cout << "4" << endl;
	}
	if (studentGrade <= 89) {
		cout << "3" << endl;
	}
	if (studentGrade <= 79) {
		cout << "2" << endl;
	}
	if (studentGrade <= 69) {
		cout << "1" << endl;
	}
	else if (studentGrade < 60) {
		cout << "0" << endl;
	}
}


How come it gives me like 3,2,1 when I type 75 when it should just be 2.
How come it gives me like 3,2,1 when I type 75 when it should just be 2.


Okay. So you type 75.

The code reaches this:

1
2
3
	if (studentGrade >= 90) {
		cout << "4" << endl;
	}

Is the studentGrade over 90? No. So the if block doesn't execute.

Now, the code reaches this:
1
2
3
if (studentGrade <= 89) {
		cout << "3" << endl;
	}
Is the studentGrade under 89? Yes. So the if block DOES execute. "3" is output.

Now, the code reaches this:
1
2
3
if (studentGrade <= 79) {
		cout << "2" << endl;
	}
Is the studentGrade under 79? Yes. So the if block DOES execute."2" is output.

Now, the code reaches this:
1
2
3
4
5
6
if (studentGrade <= 69) {
		cout << "1" << endl;
	}
	else if (studentGrade < 60) {
		cout << "0" << endl;
	}
Is the studentGrade under 69? No. So the first if block doesn't execute, and we move on to the else section, which looks like this:
1
2
else if (studentGrade < 60) {
		cout << "0" << endl;}
and now we see that studentGrade is NOT under 60, so this final if block doesn't execute.


Put another way, see this?
1
2
3
4
5
6
	if (studentGrade >= 90) {
		cout << "4" << endl;
	}
	if (studentGrade <= 89) {
		cout << "3" << endl;
	}

This is TWO independent blocks. Maybe you meant to use a few more else in your code.



when it should just be 2.
Why would it be just 2? You first test for it being over 90, and then you test for it being under 89. Is it under 89? Yes. With your current logic, ANY value under 89 will get a "2". With your current logic, the score of -567 will get a "2", because any number less than 89 gets a "2".
Last edited on
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
void qualityPoints()
{
	int studentGrade;
	cout << "Type in your grade average 0 - 100 percent: " << endl;
	cin >> studentGrade;

	if (studentGrade >= 90) {
		cout << "4" << endl;
	}
	if (studentGrade <= 89) {
		cout << "3" << endl;
	}
	else if (studentGrade <= 79) {
		cout << "2" << endl;
	}
	else if (studentGrade <= 69)
	{
		cout << "1" << endl;
	}
	else if (studentGrade < 60)
	{
		cout << "0" << endl;
	}
}
		

im a bit rusty I am having some trouble here what you guys says makes sense how should the if nest be set up in a statement like this then?
1
2
3
if (studentGrade <= 89) {
		cout << "3" << endl;
	}


Every studentGrade that is below 89 will cause 3 to be output. Including, for example, studentGrade=54, studentGrade=3, and studentGrade=-463.

Is that what you want, or perhaps do you want every studentGrade that is below 89 AND OVER 79 to cause 3 to be output?
think I have it
I'm a bit rusty and forgot about some things, I have it all figured out thanks guys!
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
#include <iostream>
using namespace std;

void qualityPoints();

int main()
{
	qualityPoints();


	system("pause");
	return 0;
}


void qualityPoints()
{
	int studentGrade;
	cout << "Type in your grade average 0 - 100 percent: " << endl;
	cin >> studentGrade;

	if (studentGrade >= 90 && studentGrade == 100) {
		cout << "4" << endl;
	}
	if (studentGrade >= 80 && studentGrade >= 89) {
		cout << "3" << endl;
	}
	if (studentGrade >= 70 && studentGrade <= 79) {
		cout << "2" << endl;
	}
	else if (studentGrade >= 60 && studentGrade <= 69)
	{
		cout << "1" << endl;
	}
	else if (studentGrade < 60)
	{
		cout << "0" << endl;
	}
}
		
In that code, a score of 91 gives an output of 3.

A score of 86 gives no output at all.
Last edited on
With minimal rewriting:
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
#include <iostream>

void qualityPoints();

int main()
{
   qualityPoints();
}


void qualityPoints()
{
   int studentGrade;

   std::cout << "Type in your grade average 0 - 100 percent: ";
   std::cin >> studentGrade;
   std::cout << '\n';

   if (studentGrade >= 90)
   {
      std::cout << "4\n";
   }
   else if (studentGrade < 90 && studentGrade >= 80)
   {
      std::cout << "3\n";
   }
   else if (studentGrade < 80 && studentGrade >= 70)
   {
      std::cout << "2\n";
   }
   else if (studentGrade < 70 && studentGrade >= 60)
   {
      std::cout << "1\n";
   }
   else
   {
      std::cout << "0\n";
   }
}

IMO your function is doing too much, it should only check for what the grade is. You should ask for what the student's grade is in main().
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
#include <iostream>

void qualityPoints(int);

int main()
{
   int grade;

   std::cout << "Type in your grade average 0 - 100 percent: ";
   std::cin >> grade;
   std::cout << '\n';

   qualityPoints(grade);
}


void qualityPoints(int studentGrade)
{
   if (studentGrade >= 90)
   {
      std::cout << "4\n";
   }
   else if (studentGrade < 90 && studentGrade >= 80)
   {
      std::cout << "3\n";
   }
   else if (studentGrade < 80 && studentGrade >= 70)
   {
      std::cout << "2\n";
   }
   else if (studentGrade < 70 && studentGrade >= 60)
   {
      std::cout << "1\n";
   }
   else
   {
      std::cout << "0\n";
   }
}
Topic archived. No new replies allowed.