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.
#include <iostream>
#include "conio.h"
usingnamespace 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';
}
elseif(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');
}
elseif(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';
}
elseif(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.
#include <iostream>
//#include "conio.h" //try to not use this header
usingnamespace 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';
} //¿?
elseif(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');
}
elseif(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';
}
elseif(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();
}
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.
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.
#include <iostream>
#include "conio.h"
usingnamespace 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';
}
elseif(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');
}
elseif(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';
}
elseif(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();
}
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';
}
elseif(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');
}
elseif(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';
}
elseif(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.
#include <iostream>
#include <conio.h>
usingnamespace 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;
}
elseif(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;
}
elseif(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
}
}
}
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.