Program ends abruptly if tried to enter a value or enters infinite loops

Hi everyone.

I am a beginner with C++ and I created a program to check if the person can drive or not.

For it to run properly I added a line where if a person enters a wrong value when asked his age the program would ask whether it should run again or not but upon pressing any key the program ends and closes abruptly.

There are two places where this is happening, in one place if you type alphabets or numericals it ends and at the other place if you type alphabets or any special character the program behaves unnaturally.

I am mentioning the code. 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
#include <iostream>
#include "conio.h"
using namespace std;

void main(){
	int age;
	char insurance;
	char rainsurance;
	char raage;
		do{cout << "What is your age?" << endl;
		cin >> age;
		cout << "You entered: " << age << endl;
			if(age >= 18 && age <130){
				raage = 'n';				
				do{cout << "Now, Do you have an insurance? Y/N" << endl;
				cin >> insurance;
					if(insurance == 'Y' || insurance == 'y')
					cout << "Congratulations! You can drive legally." << endl;
					rainsurance = 'n';
					}
					else if(insurance == 'N' || insurance == 'n'){
					cout << "Your age is correct but an insurance is required for a legal permission to drive." << endl;
					rainsurance = 'n';
					}
					else{
					cout << "This is an incorrect value! Do you want an another go? Y/N" << endl;
					cin >> rainsurance; // over here if we type any alphabet or numerical the program ends abruptly on the next hit of enter and doesn't accept any value.
					}}while(rainsurance == 'Y' || rainsurance == 'y');
			}
			else if(age > 0 && age < 18){
				cout << "I am afraid but, you are underage. The minimum age is 18 for a legal permission to drive." << endl;
				raage = 'n';
			}
			else if(age >= 130){
				cout << "You are too old to drive." << endl;
				raage = 'n';
			}
			else{
				cout << "This is an incorrect value! Do you want another go? Y/N" << endl;
				cin >> raage; // Over here if you type an alphabet the program goes into infinite loops and if you type a special character umm like "\" the program ends on next hit of enter.
			}}while(raage == 'Y' || raage == 'y');
	_getch();
}


Please help I think its the data type I have selected wrong.
Last edited on
your program does not compile.

here it is with proper indentation
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
#include <iostream>
//#include "conio.h" //try to not use this header
using namespace std;

int main(){ //main must return int
	int age;
	char insurance;
	char rainsurance;
	char raage;
	do{
		cout << "What is your age?" << endl;
		cin >> age;
		cout << "You entered: " << age << endl;
		if(age >= 18 && age <130){
			raage = 'n';				
			do{
				cout << "Now, Do you have an insurance? Y/N" << endl;
				cin >> insurance;
				if(insurance == 'Y' || insurance == 'y')
					cout << "Congratulations! You can drive legally." << endl;
				rainsurance = 'n';
			} //¿?
			else if(insurance == 'N' || insurance == 'n'){ //no previous `if'
				cout << "Your age is correct but an insurance is required for a legal permission to drive." << endl;
				rainsurance = 'n';
			}
			else{
				cout << "This is an incorrect value! Do you want an another go? Y/N" << endl;
				cin >> rainsurance;
			}
		}while(rainsurance == 'Y' || rainsurance == 'y');
	}
	else if(age > 0 && age < 18){ //no previous `if'
		cout << "I am afraid but, you are underage. The minimum age is 18 for a legal permission to drive." << endl;
		raage = 'n';
	}
	else if(age >= 130){
		cout << "You are too old to drive." << endl;
		raage = 'n';
	}
	else{
		cout << "This is an incorrect value! Do you want another go? Y/N" << endl;
		cin >> raage;
	}
}while(raage == 'Y' || raage == 'y'); //this is outside main()
//_getch();
}
Last edited on
@ne555

your program runs but does not function properly. Please check
My program is the same as yours.
It does not run, because it doesn't compile.
My program was running and also functioning its main task. Its just that there was some problem with the loops. However the program you sent above doesn't let somebody reach that stage. Maybe there is some problem with the programs we use. I use Microsoft Visual C++ 2010 Express.
Sorry prakhar1, your code snippet given above simply cant compile as there above note states from ne555

the last while loop is outside main.

Its possible that you running the last successful compiled project, try and do a rebuild of your project

ctrl+alt+f7

and see how it goes.
How can it be outside the main. See there is a curly bracket at the very end of my program that's the end of main. The last while is inside it only.
the issue on the code you provided is indentation,

when I also format it properly that last while is out of main. on your IDE there is a - sign on the left hand side by void main, if you click on it it becomes a + sign hiding whatever you have inside the main function,

click on it and if you still see the last while that means its outside main.
copy and past your code again please. as that not how it shows on my system.

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
#include <iostream>
#include "conio.h"
using namespace std;

void main(){
	int age;
	char insurance;
	char rainsurance;
	char raage;
		do{cout << "What is your age?" << endl;
		cin >> age;
		cout << "You entered: " << age << endl;
			if(age >= 18 && age <130){ 
				raage = 'n';				
				do{cout << "Now, Do you have an insurance? Y/N" << endl;
				cin >> insurance;
					if(insurance == 'Y' || insurance == 'y'){
					cout << "Congratulations! You can drive legally." << endl;
					rainsurance = 'n';
					}
					else if(insurance == 'N' || insurance == 'n'){
					cout << "Your age is correct but an insurance is required for a legal permission to drive." << endl;
					rainsurance = 'n';
					}
					else{
					cout << "This is an incorrect value! Do you want an another go? Y/N" << endl;
					cin >> rainsurance;
					}}while(rainsurance == 'Y' || rainsurance == 'y');
			}
			else if(age > 0 && age < 18){
				cout << "I am afraid but, you are underage. The minimum age is 18 for a legal permission to drive." << endl;
				raage = 'n';
			}
			else if(age >= 130 && age < 200){
				cout << "You are too old man" << endl;
				raage = 'n';
			}
			else{
				cout << "This is an incorrect value! Do you want another go? Y/N" << endl;
				cin >> raage;
			}}while(raage == 'Y' || raage == 'y');
	_getch();
}
If you still don't believe me you can have a look on this:
http://postimg.org/image/w2gav179n/
There we go, that's much better,

Okay Below I have formatted your code according to where each statement starts and ends.

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
nt main()
{
	int age;
	char insurance;
	char rainsurance;
	char raage;
	do
	{
		cout << "What is your age?" << endl;
		cin >> age;
		cout << "You entered: " << age << endl;
		if(age >= 18 && age <130)
		{ 
			raage = 'n';				
			do
			{
				cout << "Now, Do you have an insurance? Y/N" << endl;
				cin >> insurance;
				if(insurance == 'Y' || insurance == 'y')
				{
					cout << "Congratulations! You can drive legally." << endl;
					rainsurance = 'n';
				}
				else if(insurance == 'N' || insurance == 'n'){
					cout << "Your age is correct but an insurance is required for a legal permission to drive." << endl;
					rainsurance = 'n';
				}
				else{
					cout << "This is an incorrect value! Do you want an another go? Y/N" << endl;
					cin >> rainsurance;
				}
			}
			while(rainsurance == 'Y' || rainsurance == 'y');	
		}
		else if(age > 0 && age < 18)
		{
			cout << "I am afraid but, you are underage. The minimum age is 18 for a legal permission to drive." << endl;
			raage = 'n';
		}
		else if(age >= 130 && age < 200)
		{
			cout << "You are too old man" << endl;
			raage = 'n';
		}
		else{
			cout << "This is an incorrect value! Do you want another go? Y/N" << endl;
			cin >> raage;
		}
	}while(raage == 'Y' || raage == 'y');

	_getch();
}

Thanks this program runs in exactly the same way my initial code did but this didn't solve the problem. I told earlier also I think the problem is with the data type.
Try typing some abstract thing when program asks you for your age and follow every step. I think you'll understand what I meant.
Maybe this will 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
#include <iostream>
#include <conio.h>
using namespace std;

int main(){
	int age;
	char insurance;
	char rainsurance;
	char raage;

		cout << "What is your age?" << endl;
		get_age:
		cin >> age;
		cout << "You entered: " << age << endl;
			if(age >= 18 && age <130){

				    cout << "Now, Do you have an insurance? Y/N" << endl;
				    get_insurance: //makes a label named get_insurance
				    cin >> insurance;
					if(insurance == 'Y' || insurance == 'y'){}
					cout << "Congratulations! You can drive legally." << endl;

					}
					else if(insurance == 'N' || insurance == 'n'){
					cout << "Your age is correct but an insurance is required for a legal permission to drive." << endl;

					}
					else{
					cout << "This is an incorrect value! Do you want an another go? Y/N" << endl;
					cin >> rainsurance;
					if(rainsurance=='y' || rainsurance=='Y')// || means OR
					{
					    goto get_insurance; //goes to the label get_insurance:
					}
					else  //no need to put (rainsurance=="n" || rainsurance=="N") because it is the only option left.
                    {
                        return 0; //closes the program
                    }

			}


            if(age > 0 && age < 18){
				cout << "I am afraid but, you are underage. The minimum age is 18 for a legal permission to drive." << endl;

			}
			else if(age >= 130){
				cout << "You are too old to drive." << endl;

			}
			else{
				cout << "This is an incorrect value! Do you want another go? Y/N" << endl;
				cin >> raage;
				if(raage=='y' || raage=='Y')// || means OR
					{
					    goto get_age; //goes to the label get_age:
					}
					else  //no need to put (rainsurance=="n" || rainsurance=="N") because it is the only option left.
                    {
                        return 0; //closes the program
                    }
			}
}



Last edited on
Wow, 15 replies and we still haven't figured it out? :)

Anyways, try this:
1
2
3
4
5
6
7
8
9
cout << "What is your age?" << endl;
while (!(cin >> age))
{
    cout << "You entered an invalid value, try again: ";
    cin.clear(); // Clear error flags
    cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Clear the input buffer
    // (Requires #include <limits> for the above line)
}
cout << "You entered: " << age << endl;

For your other three cin >> someCharVariable; statements, stick a cin.ignore(numeric_limits<streamsize>::max(), '\n'); right after each of them to clear out any other random gibberish the user might have entered after the first character.
Topic archived. No new replies allowed.