C++中要输入一个四位年份 例如0000正确而0不正确怎么写?拜托

来源:百度知道 编辑:UC知道 时间:2024/05/26 19:37:22

////////////////////////////////////////////////////////
// InputYear
// Prameter: szYear [out]
// Return: 1 if Success, otherwise 0 is returned
int InputYear(char *szYear)
{
char szTemp[255];
int i;

scanf("%s", szTemp);
if (strlen(szTemp) != 4)
return 0;

for (i = 0; i < 4; i ++)
{
if (szTemp[i] < '0' || szTemp[i] > '9')
return 0;
}

strcpy(szYear, szTemp);
return 1;
}

用字符串

个人认为 输入不到4为 在前面补0比较好

int year = 0;
while(4 != scanf("%d", &year)) {
printf("Bad Input\n");
}

printf("%04d\n", year );

我给你一个吧:
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
class Date {
private:
int year;
int month;
int day;
bool pass;
bool checkForma