terminating properly

how to terminate properly display message before terminating
need the program to stop when i input a zero it needs to display you have entered 0 number same goes for negative number (you have entered negative number then stop)

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
  #include<iostream>

using namespace std;

int main()

{
	int grade, a;


	do{
	cout<<"Enter Grade: \n";
	cin>>grade;
	if(grade<=0)
	cout<<"You have entered a negative number Please Exit! \n";
	
 return 0;

	a++;
		
	

	

      if(grade>=90)

		cout<<"Your grade is A \n";

	else if(grade>=80)

		cout<<"Your grade is B \n";

	else if(grade>=70)

		cout<<"Your grade is C \n";

	else if(grade>=60)

		cout<<"Your grade is D \n";

	else if(grade<60)

		cout<<"Your grade is E \n";
	
	}


	while(a != 0);

	
}

the grade part is ok would really appreciate if someone can help thanks
Last edited on
If your program was to accepts and display grades between 1 to 100, and exit if the user inputs a zero or negative number, the code is as follows:

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
 #include<iostream>
 #include <stdio.h>

using namespace std;

int main()

{
	int grade, a=1;


	do{
	cout<<"Enter Grade: \n";
	cin>>grade;
	if(grade<0){
	cout<<"You have entered a negative number Please Exit! : " << grade << endl;
	getchar();
	return 0;
	a=0;
	}

    if(grade>=90)
		cout<<"Your grade is A \n";

	else if(grade>=80)
		cout<<"Your grade is B \n";

	else if(grade>=70)
		cout<<"Your grade is C \n";

	else if(grade>=60)
		cout<<"Your grade is D \n";

	else if(grade<60 && grade >0)
		cout<<"Your grade is E \n";
		
    else if(grade == 0)
        {
            cout << "\nYourentered zero " << grade << endl;
            getchar();
            return 0;
        }
	}while(a != 0);
}


Hope that helped
is there a way to display the message and not terminate quickly??

i mean theres no point in displaying that mesage if it terminates
Last edited on
closed account (Dy7SLyTq)
the illsuionist's only works if the buffer is clear. replace line 40 with:
1
2
cin.ignore(1024, '\n');
cin.get();
Last edited on
omg whats that 1024??
closed account (Dy7SLyTq)
its an int...

EDIT: didnt mean for that to seem rude
Last edited on
i have seen codes with exit(1) does it work the same??
closed account (Dy7SLyTq)
no exit exits. what i did pauses so they can see the message, then when they hit enter it return's 0 to the system, which terminates the program. you could have also written exit(1) or abort() for the same result
i see return 0 and exit abort is the same thanks for the info
closed account (Dy7SLyTq)
well kind of. on a program this small, yes, but if i remember correctly, abort just exits right away and exit performs some clean up first
Topic archived. No new replies allowed.