我目前正在写一个简单的代码来乘以两个数字,但我想添加一个选项,让程序在初始提示“按x离开”时保持正确。这是我到目前为止:
printf(" Please input two numbers between 0 and 4000 or press x to leave: \n\n");
scanf(" %d %d", &var1, &var2);
if ((var1 > 4000) || (var2 > 4000) || (var1 < 0) || (var2 < 0)) {
while (1) {
printf(
"\n Input out of range, please use only values between 0 and 4000 \n\n");
return main();
}
while (0) {
return (0);
}
}
printf("\n Your inputs are %d and %d. \n\n", var1, var2);
printf(" %d multiplied by %d is equal to %d \n", var1, var2, var1*var2);
我仍然是C的新手,我只想弄清楚是否有一种方法可以澄清scanf()
,如果它的输入是两个十进制值然后继续,但如果它是char
的奇异x
输入然后立即离开。
那可能吗?
如果没有,那么你可以提一下我可以研究的新方向吗?像另一个if()
声明来验证进入scanf()
的内容?
我还假设用户只能输入两个数字或字母x。到目前为止,我只用一个简单的if语句来澄清。感谢您提供的任何信息。
%d
的scanf
格式说明符需要读取整数值。如果遇到不是整数的东西,它会停止读取并在缓冲区中留下不匹配的字符。
此外,main
不应该递归调用。
处理此问题的最佳方法是首先使用fgets
读取一行文本,然后检查它是否为“x”。如果是这样,请跳出读取循环。如果没有,使用sscanf
读取两个整数并检查以确保返回值为2,这意味着读取了2个值。
int valid;
do {
char line[100];
printf(" Please input two numbers between 0 and 4000 or press x to leave: \n\n");
fgets(line, sizeof line, stdin);
if (strcmp(line, "x\n") == 0) {
break;
}
valid = 0;
int cnt = sscanf(line, " %d %d", &var1, &var2);
if (cnt != 2) {
printf("Please enter two integers");
} else if ( (var1 > 4000) || (var2 > 4000) || (var1 < 0) || (var2 < 0)) {
printf( "\n Input out of range, please use only values between 0 and 4000 \n\n");
} else {
valid = 1;
}
} while (!valid);
printf( "\n Your inputs are %d and %d. \n\n", var1, var2);
printf(" %d multiplied by %d is equal to %d \n", var1, var2, var1*var2);
它不可能以这种方式使用scanf。您可以做的最接近的是下面给出的代码。希望这可以帮助!
int var1, var2;
char ch;
while (1)
{
printf("Please input two numbers between 0 and 4000\n\n");
scanf(" %d %d", &var1, &var2);
if ((var1 > 4000) || (var2 > 4000) || (var1 < 0) || (var2 < 0))
printf("\n Input out of range, please use only values between 0 and 4000 \n\n");
else
{
printf("\n Your inputs are %d and %d.\n", var1, var2);
printf(" %d multiplied by %d is equal to %d \n", var1, var2, var1*var2);
printf("Press x to leave or any other key to continue...\n\n");
scanf(" %c", &ch);
if (ch == 'x') break;
}
}
这是可能的,但有很多限制。 @dbush提供的代码实际上是更优雅的选项,而Gautam提供的代码更简单但不会按照您需要的方式执行。
虽然可以通过以下代码以更简单的方式实现所需:
在进入代码之前,稍微解释一下:
x
,则搜索字符串。这可以使用strcspn
方法完成。如果是,退出,否则继续。int
s。为此使用atoi
方法。在原始字符串上使用它可以获得第一个数字作为int
(即,直到遇到第一个space
)。通过提取字符串的第二部分,即在空格之后,将其转换为int
以获得第二个数字。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main() {
int var1, var2, index, length, isInvalid = 0;
char s[10], s2[10];
//do {
isInvalid = 0;
printf(" Please input two numbers between 0 and 4000 or press x to leave: \n\n");
scanf("%[^\n]s", s); //accept the 2 numbers or the 'x' as a string
length = strlen(s); //calculate the length of the string
index = strcspn(s, "x"); //check if user has entered 'x'
//if 'x' is present its position will be stored in 'index', if not present value of index will be length of string.
if (index < length) { //if 'x' is present
isInvalid = 0;
printf("\nAborted");
return 0;
} else { //if 'x' isn't entered
var1 = atoi(s); //converts the first part of string(before space) to its int equivalent
index = strcspn(s, " "); //get the postion of space
strncpy(s2, s + index, length); //get the part of string after space
var2 = atoi(s2); //convert the second part of string into int
//rest is your usual code.. without your return main() part which isn't really right
if ((var1 > 4000) || (var2 > 4000) || (var1 < 0) || (var2 < 0)) {
printf("\n Input out of range, please use only values between 0 and 4000 \n\n");
isInvalid = 1;
}
printf("\n Your inputs are %d and %d. \n\n", var1, var2);
printf(" %d multiplied by %d is equal to %d \n", var1, var2, var1 * var2);
}
//} while (isInvalid);
return 0;
}
但请记住一个非常重要的缺点:在输入时,必须输入2个数字作为空格分隔数字。只需单击2次“输入”键即可使用此功能。