【问题标题】:Input several numbers from array and each one number check for integer or not从数组中输入多个数字,每个数字检查是否为整数
【发布时间】:2014-04-27 17:03:54
【问题描述】:

大家! 我希望有人可以帮助我找出 C 语言中的一些东西。 这是我在 IT 方面的第一个认真的家庭作业,我没有经验,而且我正在学习电子学习,所以老师的帮助不是很好。

我需要用 C 语言开发控制台应用程序。用户需要输入10个整数,如果插入的数字不是整数,需要输出错误并重新输入新的数字,直到10个整数都被插入。

如果我说这 10 个数字不能为 0,一切正常(我这样做是为了确保我的 if-else 语句正常工作),但当我希望检查每个输入数字时就不起作用是否为整数。

我怎样才能做到正确。

请帮忙

到目前为止,我的代码如下所示:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i;
    float f;
    int numbers[10];

    for (i = 0; i < 10; i++)
    {
        scanf ("%d", &numbers[i]);
        if (numbers[i] != 0)
        {
            scanf ("*%d", &numbers[i]);
        }
        else
        {
            printf ("\nError!Entered number is't integer \n");
            printf ("\nPlease insert number again \n");
            scanf("%*d", &numbers[i]);
        }

    }

}

【问题讨论】:

标签: c arrays integer


【解决方案1】:
#include <stdio.h>

int main(void) {
    int i = 0;
    int val;
    char ch;
    int numbers[10];

    while(i < 10) {
        val = scanf("%d", numbers + i);  // read the integer into a[i]
        if(val != 1) {
            while((ch = getchar()) != '\n')  // discard the invalid input
                ;  // the null statement
            printf("Error! Entered number is not an integer.\n");
            printf("Please enter an integer again.\n");
            val = scanf("%d", numbers + i);
            continue;
        }
        ++i;
    }
    // process the numbers array
    return 0;
}

我再写这行

val = scanf("%d", numbers + i);

现在它按我的需要工作。太好了 - 非常感谢

【讨论】:

  • 看来应该删除第二个val = scanf("%d", numbers + i);
【解决方案2】:

您可以使用几种技术:

  1. 将数字作为字符串读取,如果其中包含不适合整数的字符,则拒绝。使用sscanf()将字符串转换为整数。

  2. 将数字读取为浮点数,如果超出整数范围或具有非整数值,则拒绝。

  3. 逐个字符读取输入并建立一个整数值。如果出现无效字符,则拒绝该值。

【讨论】:

  • 感谢您这么快的回复。您能否给我写一个示例,它在每个选项的代码中看起来如何?今天我已经在尝试 sscanf thig 了,但是有些地方是错误的
【解决方案3】:

scanf 返回成功匹配和分配的输入项数。您可以在每次调用scanf 时检查1 的此值。如果值为0,则应丢弃输入以清除stdin 缓冲区并再次读取输入。

#include <stdio.h>
#include <ctype.h>

int main(void) {
    int i = 0; 
    int val;
    char ch;
    int numbers[10];

    while(i < 10) {
        // read an integer and the first non-numeric character

        val = scanf("%d%c", numbers + i, &ch);  

        // if the number of items assigned by scanf is not 2 or if 
        // the first non-numeric character is not a whitespace, then
        // discard the input and call read input again.
        // for example input of type 32ws are completely discarded

        if(val != 2 || !isspace(ch)) {
            while((ch = getchar()) != '\n')  // discard the invalid input
                ;  // the null statement
            printf("Error! Entered number is not an integer.\n");
            printf("Please enter an integer again.\n");
            continue;
        }
        ++i;
    }
    // process the numbers array
    return 0;
}

【讨论】:

  • 谢谢。这是可行的,但是有没有一种方法可以使由于不是整数而被程序消除的数字不会在数组中计数。例如 - 用户输入 1 2 3 0,5(错误)4(而不是 0,5)5 6 7 8 9 10 = 总输入数字(整数)为 10,因为使用您的代码,他不会让输入最后一个数字,因为他计算了不是整数的数字。
  • @efeja 那么最好的办法是将整数读取为字符串,然后检查字符串是否包含任何非数字字符。
  • @efeja 请更新答案。现在它可以满足您的需求了。
【解决方案4】:

虽然我对你的问题的细节并不完全清楚,但这里有一个类似于你想要的代码大纲:

int main(void)
{
    int i;
    int numbers[10];
    int sum = 0;

    for(i=0; i<10; ++i)
    {
        printf("Enter #%d:\n", i+1);
        scanf("%d", numbers+i);

        if (numbers[i] % 2 == 0) // Then Number is even
        {
            sum += numbers[i];
        }
    }

    printf("The sum of only the even numbers is %d\n", sum);

    getch();
    return 0;
}

【讨论】:

    【解决方案5】:

    要阅读int,建议fgets() 然后sscanf()strtol()

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
      int i;
      int numbers[10];
    
      for (i = 0; i < 10; ) {
        char buffer[50];
        if (fgets(buffer, sizeof buffer, stdin) == NULL) break; 
        int n;  // number of `char` parsed
        if (sscanf(buffer, "%d %n", &numbers[i], &n) != 1 || buffer[n] != '\0') {
          printf("Error! Entered number is not an integer.\n");
          printf("Please enter an integer again.\n");
          continue;
        }
        i++;
      }
      return 0;
    }
    

    strtol() 方法。这会检测溢出问题:

        if (fgets(buffer, sizeof buffer, stdin) == NULL) break; 
        char *endptr;
        errno = 0;  
        long num = strtol(buffer, &endptr, 10);
        if (errno || num < INT_MIN || num > INT_MAX) Handle_RangeError();
        if (buffer == endptr || *endptr != '\n') Handle_SyntaxError();
        numbers[i] = (int) num;        
    

    推荐制作一个可以重复使用的int GetInt(const char *prompt)函数。
    用户输入是邪恶的。在经过严格审查之前不要相信它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-23
      • 2011-07-13
      • 2016-05-19
      相关资源
      最近更新 更多