输入错误后回圈,fgetc(标准输入)和回圈出现问题
我找到了这个简单的代码。我想让它循环工作。我用不同的方法试了好几次,但输出不好。在Ubuntu Visual Studio中工作输入错误后回圈,fgetc(标准输入)和回圈出现问题,c,loops,date,fgetc,C,Loops,Date,Fgetc,我找到了这个简单的代码。我想让它循环工作。我用不同的方法试了好几次,但输出不好。在Ubuntu Visual Studio中工作 编辑我已经添加了if(y>=2015&&y=2015&&y如果您在函数void main()中有一个合适的代码,并且需要按照您的描述在一个循环中重复它,您可以执行以下操作: 将原始的void main()重命名为int one\u step() 将返回0;放在printf(“这是一个合法日期!\n”); 将return 1;放在printf(“这不是合法日期!\n”)
编辑我已经添加了
if(y>=2015&&y=2015&&y如果您在函数void main()
中有一个合适的代码,并且需要按照您的描述在一个循环中重复它,您可以执行以下操作:
将原始的void main()
重命名为int one\u step()
将返回0;
放在printf(“这是一个合法日期!\n”);
将return 1;
放在printf(“这不是合法日期!\n”);
创建新的void main()
函数,如下所示:
您不需要break;
和returnmain();
。您可能需要上述注释中所述的一些增强功能。祝您编码顺利
更新#1
我的意思是对您的代码进行这样的改进:
#include <stdio.h>
#include <stdlib.h>
int fdateCheck();
int main()
{
while (fdateCheck());
}
int fdateCheck()
{
int res;
int legit = 0;
unsigned int d,m,y;
unsigned int daysinmonth[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// get input block
printf("Enter the date\n"); // prompt for data
res = scanf("%u.%u.%u",&d,&m,&y); // get input data
if (res == EOF) { // check if no more input
printf("Input error\n");
exit(1);
}
// check if input data is valid
if (res != 3) { // check if 3 numbers scanned
system("clear");
printf("It is not a date\n");
fflush(stdin);
return 1;
}
// make leap year correction
if (y % 400 == 0 || (y % 100 != 0 && y % 4 == 0)) {
daysinmonth[1]=29; // leap year correction
}
// check if year, month and day is valid
if (y >= 2015 && y <= 3000) { // check if year in this range
if (0 < m && m < 13) { // check if month in this range
if (0 < d && d <= daysinmonth[m-1]) // check if day in this range
legit = 1;
}
}
// print a message and finish the iteration
if (legit == 1) {
printf("It is a legitimate date!\n");
return 0;
}
else {
system("clear");
printf("It's not a legitimate date!\n");
printf("Please retry.\n");
fflush(stdin);
return 1;
}
}
不要使用like0x0A
。如果你指的是换行符“\n”
,那么就明确地说出来。此外,递归调用main
是一个非常坏的习惯。请改用实循环。你需要学习如何检查函数返回值的错误条件。最明确的是,你需要检查返回的内容。为了解决这个崩溃问题,考虑使用<代码> sSCANF。检查SCANF的返回值。句柄错误。然后事情可能开始生成。另外,U应该在闰年检查中有另一个,否则如果它被设置为29,那么它将保持所有的年份。谢谢这个答案,这将有助于未来代码正常工作,B。但是有一个错误。当输入是例如a.b.c.
程序处于无限循环时,我添加了这个system(“clear”);int ch=fgetc(stdin);if(ch='\n'){system(“clear”);printf(“这不是合法日期!\n”);fflush(stdin);在if(res!=3)中返回1;}
太好了!你抓到我了。但我仍然认为没有必要检查ch
的值。请参阅更新#2。
while ((rc = scanf("%u.%u.%u", &d, &m, &y)) != EOF)
{
if (rc != 3)
//...oops data problems...
else
//...all OK...
}
int fdateCheck();
unsigned int d,m,y;
unsigned int daysinmonth[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int rc;
int legit = 0;
int main()
{
printf("Enter the date\n");
scanf("%u.%u.%u",&d,&m,&y);
while (fdateCheck());
}
int fdateCheck()
{
if (y % 400 == 0 || (y % 100 != 0 && y % 4 == 0))
{
daysinmonth[1]=29;
}
else if (y >= 2015 && y <= 3000)
{
if (m < 13)
{
if (d <= daysinmonth[m-1])
legit = 1;
}
if (legit == 1)
{
system("clear");
printf("It is a legitimate date!\n");
return 0;
}
}
else
system("clear");
int ch = fgetc(stdin);
if (ch == '\n')
{
system("clear");
printf("It's not a legitimate date!\n");
printf("\nRetry: ");
return 1;
}
fflush(stdin);
}
void main() {
while(one_step());
}
#include <stdio.h>
#include <stdlib.h>
int fdateCheck();
int main()
{
while (fdateCheck());
}
int fdateCheck()
{
int res;
int legit = 0;
unsigned int d,m,y;
unsigned int daysinmonth[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// get input block
printf("Enter the date\n"); // prompt for data
res = scanf("%u.%u.%u",&d,&m,&y); // get input data
if (res == EOF) { // check if no more input
printf("Input error\n");
exit(1);
}
// check if input data is valid
if (res != 3) { // check if 3 numbers scanned
system("clear");
printf("It is not a date\n");
fflush(stdin);
return 1;
}
// make leap year correction
if (y % 400 == 0 || (y % 100 != 0 && y % 4 == 0)) {
daysinmonth[1]=29; // leap year correction
}
// check if year, month and day is valid
if (y >= 2015 && y <= 3000) { // check if year in this range
if (0 < m && m < 13) { // check if month in this range
if (0 < d && d <= daysinmonth[m-1]) // check if day in this range
legit = 1;
}
}
// print a message and finish the iteration
if (legit == 1) {
printf("It is a legitimate date!\n");
return 0;
}
else {
system("clear");
printf("It's not a legitimate date!\n");
printf("Please retry.\n");
fflush(stdin);
return 1;
}
}
#include <stdio.h>
#include <stdlib.h>
int fdateCheck();
int main()
{
while (fdateCheck());
}
int fdateCheck()
{
int res;
int legit = 0;
unsigned int d,m,y;
unsigned int daysinmonth[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// get input block
printf("Enter the date\n"); // prompt for data
res = scanf("%u.%u.%u",&d,&m,&y); // get input data
if (res == EOF) { // check if no more input
printf("Input error\n");
exit(1);
}
// check if input data is valid
if (res != 3) { // check if 3 numbers scanned
fgetc(stdin); // remove a barrier
system("clear");
printf("It is not a date\n");
fflush(stdin);
return 1;
}
// make leap year correction
if (y % 400 == 0 || (y % 100 != 0 && y % 4 == 0)) {
daysinmonth[1]=29; // leap year correction
}
// check if year, month and day is valid
if (y >= 2015 && y <= 3000) { // check if year in this range
if (0 < m && m < 13) { // check if month in this range
if (0 < d && d <= daysinmonth[m-1]) // check if day in this range
legit = 1;
}
}
// print a message and finish the iteration
if (legit == 1) {
printf("It is a legitimate date!\n");
return 0;
}
else {
system("clear");
printf("It's not a legitimate date!\n");
printf("Please retry.\n");
fflush(stdin);
return 1;
}
}