使用逗号作为多个数字输入的分隔符时出现 c 语言错误
c language error when using comma as separator for multiple number inputs
我有这个程序,它让用户输入一个数字列表,然后程序在输入中找到最大的数字,并计算输入最大数字的次数。
当我使用space作为分隔符时,程序运行良好。但是当我使用逗号作为分隔符时,似乎出现了逻辑错误。
这是我的源代码:
int i, numberOfIntegers, listOfIntegers, largest = 0, occurrence = 0;
printf("\n \t \t \t Finding the LARGEST integer \n");
printf("\n How many integers do you want to enter? ");
scanf("%i", &numberOfIntegers);
printf("\n Input %i list of integers: \n ", numberOfIntegers);
for (i = 1; i <= numberOfIntegers; i++)
{
printf("\t");
scanf("%i", &listOfIntegers);
if (listOfIntegers > largest)
{
largest = listOfIntegers;
occurrence = 1;
}
else if (listOfIntegers == largest)
{
occurrence++;
}
}
printf("\n The largest value is %i and the number of occurrence is %i \n ", largest, occurrence);
return 0;
这是我使用逗号作为分隔符的输出示例:
How many integers do you want to enter? 4
Input 4 list of integers:
5, 6, 6, 6
The largest value is 5 and the number of occurrence is 4
然而,正确的输出应该是:
How many integers do you want to enter? 4
Input 4 list of integers:
5, 6, 6, 6
The largest value is 6 and the number of occurrence is 3
谁能指出我到底哪里做错了?
基本问题是,当在 C 中读取输入时,您需要考虑输入中的 每个 个字符(可能)——每个 space 和每个换行符以及每个逗号或其他标点符号,以及您真正关心和想要阅读的所有值。
当使用scanf
读取输入时,whitespace比较特殊,容易被忽略。 %c
、%[
和 %%
的 scanf 字符串 除了 之外的每个 %
指令将自动忽略前导白色 space .使用 "%i"
就像您正在使用的那样,循环中的第一个 scanf 调用会自动忽略(跳过)输入中 4
之后的换行符,数字之间的 spaces 将被后来的电话跳过。但是,任何逗号(或其他标点符号)都不会。您需要明确地跳过(阅读)它们。实际上,当您的程序在循环的第二次迭代中到达 scanf
调用时,要读取的下一个字符是 ,
(不是数字也不是白色 space),因此转换失败并且没有任何内容存储到 listOfIntegers
中并且 0 被 returned(没有匹配的指令)。由于您忽略了 scanf 的 return 值,因此您没有注意到这一点,并且很高兴地继续使用第一次迭代遗留下来的相同值。
您可以尝试的一件事是在循环中 scanf("%i,", &listOfIntegers)
。如果 立即 出现在您的号码之后,这将读取单个 ,
。如果数字后面的字符不是 ,
它将什么都不做。虽然这适用于您的示例,但它不适用于
这样的输入
5 , 6, 6 , 6
由于逗号前多了一个space。更容易接受的可能性是
scanf("%i%*[ \t,;.]", &listOfIntegers)
这将跳过(并忽略)数字后的所有 space、制表符、逗号、分号和句号。
无论如何,检查 scanf 的 return 值也是一个好主意:
if (scanf("%i%*[ \t,;.]", &listOfIntegers) < 1) {
... something is wrong -- the next input is not a number
捕捉输入字母或其他非数字输入的人。
我有这个程序,它让用户输入一个数字列表,然后程序在输入中找到最大的数字,并计算输入最大数字的次数。
当我使用space作为分隔符时,程序运行良好。但是当我使用逗号作为分隔符时,似乎出现了逻辑错误。
这是我的源代码:
int i, numberOfIntegers, listOfIntegers, largest = 0, occurrence = 0;
printf("\n \t \t \t Finding the LARGEST integer \n");
printf("\n How many integers do you want to enter? ");
scanf("%i", &numberOfIntegers);
printf("\n Input %i list of integers: \n ", numberOfIntegers);
for (i = 1; i <= numberOfIntegers; i++)
{
printf("\t");
scanf("%i", &listOfIntegers);
if (listOfIntegers > largest)
{
largest = listOfIntegers;
occurrence = 1;
}
else if (listOfIntegers == largest)
{
occurrence++;
}
}
printf("\n The largest value is %i and the number of occurrence is %i \n ", largest, occurrence);
return 0;
这是我使用逗号作为分隔符的输出示例:
How many integers do you want to enter? 4
Input 4 list of integers:
5, 6, 6, 6
The largest value is 5 and the number of occurrence is 4
然而,正确的输出应该是:
How many integers do you want to enter? 4
Input 4 list of integers:
5, 6, 6, 6
The largest value is 6 and the number of occurrence is 3
谁能指出我到底哪里做错了?
基本问题是,当在 C 中读取输入时,您需要考虑输入中的 每个 个字符(可能)——每个 space 和每个换行符以及每个逗号或其他标点符号,以及您真正关心和想要阅读的所有值。
当使用scanf
读取输入时,whitespace比较特殊,容易被忽略。 %c
、%[
和 %%
的 scanf 字符串 除了 之外的每个 %
指令将自动忽略前导白色 space .使用 "%i"
就像您正在使用的那样,循环中的第一个 scanf 调用会自动忽略(跳过)输入中 4
之后的换行符,数字之间的 spaces 将被后来的电话跳过。但是,任何逗号(或其他标点符号)都不会。您需要明确地跳过(阅读)它们。实际上,当您的程序在循环的第二次迭代中到达 scanf
调用时,要读取的下一个字符是 ,
(不是数字也不是白色 space),因此转换失败并且没有任何内容存储到 listOfIntegers
中并且 0 被 returned(没有匹配的指令)。由于您忽略了 scanf 的 return 值,因此您没有注意到这一点,并且很高兴地继续使用第一次迭代遗留下来的相同值。
您可以尝试的一件事是在循环中 scanf("%i,", &listOfIntegers)
。如果 立即 出现在您的号码之后,这将读取单个 ,
。如果数字后面的字符不是 ,
它将什么都不做。虽然这适用于您的示例,但它不适用于
5 , 6, 6 , 6
由于逗号前多了一个space。更容易接受的可能性是
scanf("%i%*[ \t,;.]", &listOfIntegers)
这将跳过(并忽略)数字后的所有 space、制表符、逗号、分号和句号。
无论如何,检查 scanf 的 return 值也是一个好主意:
if (scanf("%i%*[ \t,;.]", &listOfIntegers) < 1) {
... something is wrong -- the next input is not a number
捕捉输入字母或其他非数字输入的人。