Problem With If statements please help!

Hi I'm trying to write a program that the user enters their marks for coursework and exam, then put out their total marks and the grade (A* to F).
It looks correct to me but if i enter any marks it puts out A* for all of them.
Not sure what I'm doing wrong so please help.

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
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int Course, Exam; //Input course and exam marks (Coursework and exam must be between 15 and 50)
	int Aplus = 0, A = 0, B = 0, C = 0, F = 0; //counts up the grades of students  
	int Count = 0; // Adds up the total of student grades entered
	int TotalM = 0; // The total 
	char Ans; // A Loop for adding more student grades before showing results  

		do
		{
			do
			{
				cout << "\n\n\tPlease enter Coursework Mark: ";
				cin >> Course;

				
				cout << "\n\n\tPlease enter Exam Mark: ";
				cin >> Exam;

				
				TotalM = (Course + Exam) / 2;

				if(TotalM >= 85 || TotalM <= 100 && Course > 15 && Exam > 15)
				{					
					Aplus = Aplus + 1;
					cout << "\n\tTotal Mark: "<<TotalM;
					cout << "\n\t Grade: A*";
				}	
				else if(TotalM >= 70 || TotalM <= 84 && Course > 15 && Exam > 15)
				{					
					A = A + 1;
					cout << "\n\tTotal Mark: "<<TotalM;
					cout << "\n\t Grade: A";
				}						
				else if(TotalM >= 55 || TotalM <= 69 && Course > 15 && Exam > 15)
				{					
					B = B + 1;
					cout << "\n\tTotal Mark: "<<TotalM;
					cout << "\n\t Grade: B";
				}
				else if(TotalM >= 39 || TotalM <= 54 && Course > 15 && Exam > 15)
				{					
					C = C + 1;
					cout << "\n\tTotal Mark: "<<TotalM;
					cout << "\n\t Grade: C";
				}
				else if(TotalM >= 0 || TotalM <= 38)
				{					
					F = F + 1;
					cout << "\n\tTotal Mark: "<<TotalM;
					cout << "\n\t Grade: F";
				}
				else(TotalM > 40 && Course < 15 && Exam < 15);
				{
					F = F + 1;
					cout << "\n\tTotal Mark: "<<TotalM;
					cout << "\n\t Grade: Techincal Fail";
				}

				cout << "\n\n\tDo you want to enter another students grade (Y/N): ";
				cin >> Ans;
				Ans = toupper(Ans);
				
				if(Ans != 'Y' && Ans != 'N')
				{
					cout << "\n\n\tERROR Please enter (Y/N) only";
				}

			}while (Ans != 'Y' && Ans != 'N');

			system("cls");

		}while (Ans == 'Y');

		Count = F + C + B + A + Aplus;

		cout << "\n\n\tTotal student's Grades entered: "<<Count;
		cout << "\n\n\tTotal F Grades: "<<F;
		cout << "\n\n\tTotal C Grades: "<<C;
		cout << "\n\n\tTotal B Grades: "<<B;
		cout << "\n\n\tTotal A Grades: "<<A;
		cout << "\n\n\tTotal A* Grades: "<<Aplus;

		

	_getch();
	return 0;
}
You have an operator precedence problem. See the table at the bottom of the page here:
http://www.cplusplus.com/doc/tutorial/operators/

In your statement:
 
if (TotalM >= 85 || TotalM <= 100 && Course > 15 && Exam > 15)


&& has a higher operator precedence than || so your statement evaluates as
 
if (TotalM >= 85 || (TotalM <= 100 && Course > 15 && Exam > 15) )

Note the parens added for explanation.

What you want is:
 
if ( (TotalM >= 85 || TotalM <= 100) && Course > 15 && Exam > 15)



Last edited on
Thank you with the operator that makes sense now, but i'm still getting A* for all the marks I enter in.
 
if (TotalM >= 85 || TotalM <= 100 && Course > 15 && Exam > 15)


Why are you using an OR operator on TotalM?
Any total <= 100 is going to cause that statement to be true as long as the counse and exam are > 15.

Last edited on
Was looking at your problem in the doctor's office but I got called in and AbstractionAnon beat me to the reply =P

As AbstractionAnon said,
Your first statement is saying:

If the total is greater than 85
****OR****
If the total is less than 100 and both the coursework and exam scores are above 15
(which I would assume if a student attempted either the coursework or exam, their score better be above 15%)

Hence, you are the nicest teacher in the world, giving all students A*, as long as their coursework and exam scores are greater than 15% (and their total grade is below 100%).
Last edited on
Ok It's working now thank you very much for your help :D
Topic archived. No new replies allowed.