C ++循环因为整数中的文本

时间:2016-03-28 19:45:08

标签: c++ loops variables syntax-error codeblocks

我是c +的新手,现在我正在做一个小应用程序,帮助我理解和开发有关它的知识,但是我有一个不需要的循环。当我输入一个字母而不是一个数字时,它被挑出并被识别为错误 - 因为它是一个整数,但是我需要脚本返回并要求一个正确的值,这使我进入一个我不是的循环能够通过。 任何建议和帮助表示感谢,谢谢

代码:

#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;
}

0 个答案:

没有答案