【问题标题】:I cannot read in a number using getchar()我无法使用 getchar() 读取数字
【发布时间】:2019-05-04 01:11:54
【问题描述】:

这里是编程菜鸟。我有一个作业,我必须使用getchar() 读取一个数字并对其执行一些操作。该数字可以是正数或负数。这是我的代码中接受输入的部分。我找不到问题,但它返回了其他内容。

我正在使用将字符转换为整数的一般方法,首先将累加器乘以10,然后使用(ch -'0') 将字符转换为其等效数字并将其添加到累加器中,

我已尝试删除对负数的验证,但仍然没有任何区别。

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

int main() {
    int accumulator = 0;
    char ch;
    int negative = 0;

    ch = getchar();
    if (ch == '-') {
        negative = -1;
        ch = getchar();
    }

    if (isdigit(ch) == 0) {
        puts("\nError: Not valid number.\nPlease try again");
        EXIT_FAILURE;
    }

    while ((ch = getchar()) != '\n') {
        ch = getchar();
        if ((ch = getchar()) == EOF) {
            break;
        }
        if (!isdigit(ch)) {
            puts("\nError: Not valid number.\nPlease try again");
            break;
            EXIT_FAILURE;
        }

        accumulator = accumulator * 10;
        accumulator = (ch = '0') + accumulator;
    }

    if (negative == -1)
        accumulator = accumulator * negative;

    printf("%d", accumulator);
}

例子

输入:2351 输出:2351(使用getchar

输入:-5689 输出:-5689(使用getchar

【问题讨论】:

  • 您的代码在您的两个示例中似乎都成功了。你能举个失败的例子吗?
  • while 循环多次调用getchar()。它在while() 条件中调用它一次,然后在第一行再次调用它。所以它在while() 中测试的字符被忽略了。然后它在if ((ch == getchar()) == EOF) 中再次调用它,因此它在上一行读取的字符被忽略。
  • @Beta 我认为底部的示例是它应该返回的,而不是它实际返回的。
  • @AlistairAlva:您可以通过单击其分数下方的灰色复选标记来接受其中一个答案

标签: c int stdio getchar


【解决方案1】:

您在while 循环中调用getchar() 的次数过多。他们每个人都丢弃了前一次调用的结果。您应该只在while() 条件中使用getchar() 调用的结果,而不是再次调用它。

您也没有将循环之前读取的字符保存到累加器中。

您在ch = '0' 中有错字,应该是ch - '0'

您需要将ch 声明为int,而不是char,因为EOF 不是字符。

EXIT_FAILURE 应该是 exit(EXIT_FAILURE)。而且你不应该把break;放在它前面,因为这会终止循环,跳过exit()调用。

您可以将negative 初始化为1,因此最后您只需乘以它,而无需先测试该值。在这种情况下,变量的更好名称是sign

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

int main() {
    int accumulator = 0;
    int ch;
    int sign = 1;

    ch = getchar();
    if (ch == '-') {
        sign = -1;
        ch = getchar();
    }

    if (!isdigit(ch)) {
        puts("\nError: Not valid number.\nPlease try again");
        exit(EXIT_FAILURE);
    }

    accumulator = ch - '0';

    while ((ch = getchar()) != '\n' && ch != EOF) {
        if (!isdigit(ch)) {
            puts("\nError: Not valid number.\nPlease try again");
            exit(EXIT_FAILURE);
        }

        accumulator *= 10;
        accumulator += (ch - '0');
    }

    accumulator *= sign;

    printf("%d", accumulator);
}

【讨论】:

    【解决方案2】:

    您的代码中存在多个问题:

    • ch 必须使用 int 类型定义,以适应 getchar() 返回的所有值。
    • 您应该在while 循环的顶部测试EOF
    • EXIT_FAILUREexit() 函数的参数值。将其用作exit(EXIT_FAILURE)
    • 您在 while 循环体中重新读取了一个新字符,而不是处理 ch

    这是一个更正的版本:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
        int accumulator = 0;
        int ch;
        int negative = 0;
    
        ch = getchar();
        if (ch == '-') {
            negative = -1;
            ch = getchar();
        } else (if (ch == '+') {
            ch = getchar();
        }   
    
        if (isdigit(ch) == 0) {
            puts("\nError: Not valid number.\nPlease try again");
            exit(EXIT_FAILURE);
        }
    
        while ((ch = getchar()) != EOF && ch != '\n') {
            if (!isdigit(ch)) {
                puts("\nError: Not valid number.\nPlease try again");
                exit(EXIT_FAILURE);
            }
            accumulator = accumulator * 10 + (ch = '0');
        }
        if (negative < 0)
            accumulator = -accumulator;
    
        printf("%d\n", accumulator);
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多