Im new to c++, right now Im doing a small app that helps me to understand and develop knowledge about it, however Ive got an unwanted loop. When I input a letter instead of a number its being picked out and recognized as an error - because it is an integer, however I need the script to go back and ask for a correct value, which makes me go into a loop which Im not able to get through. Any advises and help appreciated, Thanks
Code:
#include <iostream>
#include <time.h>
#include <conio.h>
using namespace std;
int age;
string namevar;
int born(int age){
time_t theTime = time(NULL);
struct tm *aTime = localtime(&theTime);
int year = aTime->tm_year + 1900;
cout <<"Current year is " << year << endl;
int cage;
cage=year-age-1;
cout << "Born in " << cage << endl;
}
void namefun(string namevar){
cout << "Your name is " << namevar << " there is no reason behind this comment\n you may leave now." << endl;
}
void yes(){
cout << "You are 18+\n";
}
void no (){
cout << "You are less than 18" << endl;
}
int main(){
cout << "Hello, enter your age bruh" << endl;
cin >> age;
if (cin.fail()) {
cout << "Make sure to input a number only" <<endl;
main();
};
if (age >= 0 & age <120){
cout << "You are " << age << " years old\n\n";
if (age >= 18){
yes();}
else{
no();}}
else{
cout << "sorry invalid input"<< endl;
main();};
born(age);
cout << "Please enter your name for no reason!\n";
cin >> namevar;
namefun(namevar);
getch();
return 0;
}