C语言怎么实现输错了 重新输入

Python017

C语言怎么实现输错了 重新输入,第1张

可以设置一个while循环在其体内输入数据,经判断输入正确则跳出循环,否则提示重新输入,直到输入正确。举例代码如下:

//#include "stdafx.h"//If the vc++6.0, with this line.

#include "stdio.h"

int main(void){

    int m[12]={31,28,31,30,31,30,31,31,30,31,30,31},year,month

    while(1){//这个循环就是解决输入错误的

        printf("Please enter the year & month...\n")

        fflush(stdin)

        if(scanf("%d%d",&year,&month) && year>0 && month>0 && month<13)

            break

        printf("Error, redo: ")

    }

    m[1]+=year%4==0 && year%100!=0 || year%400==0

    printf("%d/%d is %d days.\n",month,year,m[month-1])

    return 0

}

cin>>sth

while(!check(sth))

{

//check

函数检查输入值,如果满足就不需要在输入,这是就返回1或者true

cout<<"enter

again,

because

it

is

invalidate"

cin>>sth

}