Need help with if-else statements with for loop

I want the program to do this this:
The application requires that the user enters integer mark between 0 and 100 (both marks inclusive). If the mark entered by the user is negative (below 0) or above 100, the application prints error message – “Error – marks are out of range: marks must be between 0 and 100 both marks inclusive”.
When user enters marks between 90 and 100 the letter grade assigned is ‘A’. When the user enters marks between 80 and 89, the application assigns the letter grade as ‘B’. When the user enters marks between 70 and 79, the letter grade assigned is ‘C’. When user enters marks between 60 and 70, user gets letter grade of ‘D’ and mark below 60 means letter grade of ‘F’.

However, when i do the code it wont work how i want the program to work and i dont know whats the problem.

The only parts that work the way i want it to is from 100- 90 and from 0- 60.
Whenever I try to input the other numbers the program just breaks from the loop.
I really need 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
#include <stdlib.h>
#include <stdio.h>

int main (void)

{
	int mark;

	while(1){

	printf("\n\n\n\t\tEnter mark between 0 and 100:\t\t\t");
	scanf("%d",&mark);

	if (mark>100 && mark<0)
	{
		printf("\n\n\tError- mark must be between 0 and 100 inclusive ....\n");
		system("pause");
	}
	

	else if (mark<=100 && mark>=90)
	{
		printf("\n\n\n\tYou entered the mark as:\t\t%d", mark);
		printf("\n\n\tYour calculated grade is:\t\tA");
		system("pause");
	}
	

	else if (mark<=80 && mark>90)
	{
		printf("\n\n\n\tYou entered the mark as:\t\t%d", mark);
		printf("\n\n\tYour calculated grade is:\t\tB");
		system("pause");
	}
	
	else if (mark<=70 && mark>80)
	{
		printf("\n\n\n\tYou entered the mark as:\t\t%d", mark);
		printf("\n\n\tYour calculated grade is:\t\tC");
		system("pause");
	}
	
	
	else if (mark<=60 && mark>=69)
	{
		printf("\n\n\n\tYou entered the mark as:\t\t%d", mark);
		printf("\n\n\tYour calculated grade is:\t\tD");
		system("pause");
	}
	

	else if (mark<60 && mark>=0)
	{
		printf("\n\n\n\tYou entered the mark as:\t\t%d", mark);
		printf("\n\n\tYour calculated grade is: F");
		system("pause");
	}
	

	else if (mark= -1000)
	{
		break;
	}

		}

printf("Thank you for using the application....\n");
system("pause");
}
Last edited on
closed account (N36fSL3A)
1
2
3
4
5
	if (mark>100 && mark<0)
	{
		printf("\n\n\tError- mark must be between 0 and 100 inclusive ....\n");
		system("pause");
	}
I'm sure this is a typo. A number is never going to be larger than 100 and less than 0 at the same time. Try using the or operator ('||'):

1
2
3
4
5
	if (mark>100 || mark<0)
	{
		printf("\n\n\tError- mark must be between 0 and 100 inclusive ....\n");
		system("pause");
	}


----------------------------

Oh, and I'm not sure on what this is there for.
1
2
3
4
	else if (mark= -1000)
	{
		break;
	}
Last edited on
Thanks that fixed one of the problems. The second problem is that whenever i input any number from 89- 60 the program just loops without showing the printf statements... cant really figure it out

and the coding on lines 60- 63 is the input key to exit the program... maybe i should fix it up

-------------------------------------

Heres the new 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
65
66
67
68
69
70
#include <stdlib.h>
#include <stdio.h>

int main (void)

{
	int mark;

	while(1){

	printf("\n\n\n\t\tEnter mark between 0 and 100:\t\t\t");
	scanf("%d",&mark);

	if (mark>100 || mark<0 && mark!= -1000)
	{
		printf("\n\n\tError- mark must be between 0 and 100 inclusive ....\n");
		system("pause");
	}
	

	else if (mark<=100 && mark>=90)
	{
		printf("\n\n\n\tYou entered the mark as:\t\t%d", mark);
		printf("\n\n\tYour calculated grade is:\t\tA\n");
		system("pause");
	}
	

	else if (mark<=80 && mark>=89)
	{
		printf("\n\n\n\tYou entered the mark as:\t\t%d", mark);
		printf("\n\n\tYour calculated grade is:\t\tB\n");
		system("pause");
	}
	
	else if (mark<=70 && mark>=79)
	{
		printf("\n\n\n\tYou entered the mark as:\t\t%d", mark);
		printf("\n\n\tYour calculated grade is:\t\tC\n");
		system("pause");
	}
	
	
	else if (mark<=60 && mark>=70)
	{
		printf("\n\n\n\tYou entered the mark as:\t\t%d", mark);
		printf("\n\n\tYour calculated grade is:\t\tD\n");
		system("pause");
	}
	

	else if (mark<60 && mark>=0)
	{
		printf("\n\n\n\tYou entered the mark as:\t\t%d", mark);
		printf("\n\n\tYour calculated grade is: F\n");
		system("pause");
	}
	

	else if (mark== -1000)
	{
		printf("Thank you for using the application....\n");
		break;
	}

		}

system("pause");
}
Your problem is that a mark can not be <=80 AND >=89 at the same time.
Look at how you used to put the biggest mark first, but switched that around starting at line 29.

You can either switch the numbers around or code it this way:

1
2
3
4
5
else if (mark>=80 && mark<=89)
	{
		printf("\n\n\n\tYou entered the mark as:\t\t%d", mark);
		printf("\n\n\tYour calculated grade is:\t\tB\n");
		system("pause");
why don't you just do your if-else if structure like this?

1
2
3
4
5
6
7
8
9
10
11
12
if (score <= 100 && score >= 90)
		cout << "You got an A!" << endl;
	else if (score <= 89 && score >= 80)
		cout << "You got a B!" << endl;
	else if (score <= 79 && score >= 70)
		cout << "You got a C!" << endl;
	else if (score <= 69 && score >= 60)
		cout << "You got a D!" << endl;
	else if (score <= 59 && score >= 0)
		cout << "You got a F!" << endl;
	else 
		cout << "Error – marks are out of range: marks must be between 0 and 100 both marks inclusive" << endl;
You should also rewrite it in a way that "You entered..." and "Your calculated..." is only present once.

Your code has useless repetitions and that is something you'll want to avoid as much as possible when writing more complex programs. It can lead to mistakes, especially if you ever need to go back and change the code a little bit.
Thanks everyone i figured it out. And yeah i know that it has lots of repetitions and ill try to fix that. I appreciate the help

-----------------------

And the reason im not using cout is because im not making it in c++ im making it in c
Last edited on
Topic archived. No new replies allowed.