有没有办法制作一个错误计数器,在特定数量的用户输入后终止程序?
Is there a way to make an error counter that terminates the program after a specific number of user inputs?
我是一名编程初学者。我制作了一个将整数输入转换为罗马数字的程序。这是一个非终止程序,只有当用户输入 5 个错误时它才会终止。
int main(){
int i;
for(i = 1; i > 0; i++)
{
int num;
printf("\n\nEnter a positive integer: ");
scanf("%d", &num);
while(num == 0 || num < 0 || num > 3999 || i == 5)
{
if(i == 5)
{
int ch;
printf("\n\nYou have reached 5 retries. Do you wish to continue?\n");
printf("Press 1 to continue.\n");
printf("Press 2 to terminate the program.\n");
scanf("%d", &ch);
if(ch == 1)
{
break;
}
else if(ch == 2)
{
return 0;
}
else
{
printf("Invalid input. The program will now terminate.");
return 0;
}
}
else if(num == 0)
{
printf("Error: Zero is Nulla");
break;
}
else if(num < 0)
{
printf("Error: Input cannot be negative");
break;
}
else if(num > 3999)
{
printf("Error: Input cannot be greater than 3999");
break;
}
else
{
printf("Error: Input should be a positive integer");
break;
}
}
while(num > 0 && num <4000)
{
if (num >= 1000) // 1000 - m
{
printf("m");
num -= 1000;
}
else if (num >= 900) // 900 - cm
{
printf("cm");
num -= 900;
}
else if (num >= 500) // 500 - d
{
printf("d");
num -= 500;
}
else if (num >= 400) // 400 - cd
{
printf("cd");
num -= 400;
}
else if (num >= 100) // 100 - c
{
printf("c");
num -= 100;
}
else if (num >= 90) // 90 - xc
{
printf("xc");
num -= 90;
}
else if (num >= 50) // 50 - l
{
printf("l");
num -= 50;
}
else if (num >= 40) // 40 - xl
{
printf("xl");
num -= 40;
}
else if (num >= 10) // 10 - x
{
printf("x");
num -= 10;
}
else if (num >= 9) // 9 - ix
{
printf("ix");
num -= 9;
}
else if (num >= 5) // 5 - v
{
printf("v");
num -= 5;
}
else if (num >= 4) // 4 - iv
{
printf("iv");
num -= 4;
}
else if (num >= 1) // 1 - i
{
printf("i");
num -= 1;
}
}
}
return 0;}
它会在 5 次用户输入后停止,但我不希望这样。我希望它在出现 5 个错误后停止。任何帮助,将不胜感激。谢谢
i
在循环的每次迭代中递增,这就是它在 5 次迭代(5 次用户输入)后停止的原因。所以:
- 您应该使用
while(1)
循环,而不是 for
循环。
- 而不是
int i;
,在主循环之前创建一个 int error_counter = 0;
。
- 那么
if(i == 5)
你可以写成 ++error_counter; if (error_counter == 5) { ... }
.
- 此外,两个内部“while”循环实际上只是“if”条件。因此,您应该使用
if (num <= 0 || num > 3999) {...} else {...}
而不是 while(num == 0 || num < 0 || num > 3999 || i == 5)
和 while(num > 0 && num <4000)
我是一名编程初学者。我制作了一个将整数输入转换为罗马数字的程序。这是一个非终止程序,只有当用户输入 5 个错误时它才会终止。
int main(){
int i;
for(i = 1; i > 0; i++)
{
int num;
printf("\n\nEnter a positive integer: ");
scanf("%d", &num);
while(num == 0 || num < 0 || num > 3999 || i == 5)
{
if(i == 5)
{
int ch;
printf("\n\nYou have reached 5 retries. Do you wish to continue?\n");
printf("Press 1 to continue.\n");
printf("Press 2 to terminate the program.\n");
scanf("%d", &ch);
if(ch == 1)
{
break;
}
else if(ch == 2)
{
return 0;
}
else
{
printf("Invalid input. The program will now terminate.");
return 0;
}
}
else if(num == 0)
{
printf("Error: Zero is Nulla");
break;
}
else if(num < 0)
{
printf("Error: Input cannot be negative");
break;
}
else if(num > 3999)
{
printf("Error: Input cannot be greater than 3999");
break;
}
else
{
printf("Error: Input should be a positive integer");
break;
}
}
while(num > 0 && num <4000)
{
if (num >= 1000) // 1000 - m
{
printf("m");
num -= 1000;
}
else if (num >= 900) // 900 - cm
{
printf("cm");
num -= 900;
}
else if (num >= 500) // 500 - d
{
printf("d");
num -= 500;
}
else if (num >= 400) // 400 - cd
{
printf("cd");
num -= 400;
}
else if (num >= 100) // 100 - c
{
printf("c");
num -= 100;
}
else if (num >= 90) // 90 - xc
{
printf("xc");
num -= 90;
}
else if (num >= 50) // 50 - l
{
printf("l");
num -= 50;
}
else if (num >= 40) // 40 - xl
{
printf("xl");
num -= 40;
}
else if (num >= 10) // 10 - x
{
printf("x");
num -= 10;
}
else if (num >= 9) // 9 - ix
{
printf("ix");
num -= 9;
}
else if (num >= 5) // 5 - v
{
printf("v");
num -= 5;
}
else if (num >= 4) // 4 - iv
{
printf("iv");
num -= 4;
}
else if (num >= 1) // 1 - i
{
printf("i");
num -= 1;
}
}
}
return 0;}
它会在 5 次用户输入后停止,但我不希望这样。我希望它在出现 5 个错误后停止。任何帮助,将不胜感激。谢谢
i
在循环的每次迭代中递增,这就是它在 5 次迭代(5 次用户输入)后停止的原因。所以:
- 您应该使用
while(1)
循环,而不是for
循环。 - 而不是
int i;
,在主循环之前创建一个int error_counter = 0;
。 - 那么
if(i == 5)
你可以写成++error_counter; if (error_counter == 5) { ... }
. - 此外,两个内部“while”循环实际上只是“if”条件。因此,您应该使用
if (num <= 0 || num > 3999) {...} else {...}
而不是
while(num == 0 || num < 0 || num > 3999 || i == 5)
和 while(num > 0 && num <4000)