首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >逐个字符读取-总是到达无效字符

逐个字符读取-总是到达无效字符
EN

Stack Overflow用户
提问于 2020-11-09 17:30:11
回答 1查看 94关注 0票数 1

我是C语言和编程世界的新手。我被要求以20为基数得到一个反转的数字,并打印出他转换成十进制的结果。我知道这不是最有效的代码,但这是我目前所学到的最好的代码。当我运行这个程序并输入一个数字时,我总是得到默认的情况...我遗漏了什么?谢谢您:)

代码语言:javascript
复制
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main() {
    char x;
    int exponent= 1;
    int sum = 0;
    int flag = 0;
    printf("Enter a reversed number is base 20\n");
    scanf(" %c", &x);
    while (x != "\n") {
        switch (x) {
        case '0':sum += 0;
            break;
        case '1':sum += 1 * exponent;
            break;
        case '2':   sum += 2 * exponent;
            break;
        case '3':sum += 3 * exponent;
            break;
        case '4':sum += 4 * exponent;
            break;
        case '5':sum += 5 * exponent;
            break;
        case '6':sum += 6 * exponent;
            break;
        case '7':sum += 7 * exponent;
            break;
        case '8':sum += 8 * exponent;
            break;
        case '9':sum += 9 * exponent;
            break;
        case 'A':
        case 'a': sum += 10 * exponent;
            break;
        case 'B':
        case 'b':sum += 11 * exponent;
            break;
        case 'C':
        case 'c' : sum += 12 * exponent;
            break;
        case 'D':
        case 'd': sum += 13 * exponent;
            break;
        case 'E':
        case 'e': sum += 14 * exponent;
            break;
        case 'F':
        case 'f': sum += 15 * exponent;
            break;
        case 'G':
        case 'g': sum += 16 * exponent;
            break;
        case 'H':
        case 'h': sum += 17 * exponent;
            break;
        case 'I':
        case 'i': sum += 18 * exponent;
            break;
        case 'J':
        case 'j': sum += 19 * exponent;
            break;
        default:flag++;
            break;
        }
        if (flag == 1) {
            printf("Error! %c is not a valid digit in base 20", x);
            break;
        }
        else {
            exponent *= 20;;
            scanf("%c", &x);
        }
    }
    if (flag == 1)
        return 0;
    else
        printf(sum);
    return 0;
}
EN

回答 1

Stack Overflow用户

发布于 2020-11-09 17:35:00

你真的应该看看你的compiler's warnings...:

代码语言:javascript
复制
<source>: In function 'main':
<source>:10:14: warning: comparison between pointer and integer
   10 |     while (x != "\n") {
      |              ^~
<source>:10:14: warning: comparison with string literal results in unspecified behavior [-Waddress]
<source>:77:16: warning: passing argument 1 of 'printf' makes pointer from integer without a cast [-Wint-conversion]
   77 |         printf(sum);
      |                ^~~
      |                |
      |                int
In file included from <source>:2:
/usr/include/stdio.h:332:43: note: expected 'const char * restrict' but argument is of type 'int'
  332 | extern int printf (const char *__restrict __format, ...);
      |                    ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~

您正在将x与一个地址进行比较-字符串常量"\n“的地址(位于程序内存空间的某一部分)。您可能是想将x与'\n‘进行比较。

此外,printf()函数在参数之前接受一个格式字符串,因此它应该是printf("%d\n", sum); (为了打印换行符)。

PS 1:总是在编译代码时打开更多的警告,例如gcc -W -Wall (也许还有其他警告标志),以便更好地捕获这些类型的打字错误和小错误。

PS 2:正如@Jabberwocky指出的那样,你的程序可以变得更短(不会失去可读性);另一方面,你应该总是检查scanf()和类似函数的返回值,这些函数可能会因为用户的输入而失败:

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>

int main() {
    char x;
    int exponent = 1;
    int sum = 0;
    int num_scanned;
    printf("Enter a reversed number is base 20\n");
    num_scanned = scanf(" %c", &x);
    if (num_scanned != 1) {
        fprintf(stderr, "Invalid input\n");
        exit(EXIT_FAILURE);
    }
    while (x != '\n') {
        int digit;
        if      (x >= '0' && x <= '9') { digit = x-'0'; }
        else if (x >= 'a' && x <= 'j') { digit = x-'a'; }
        else if (x >= 'A' && x <= 'J') { digit = x-'A'; }
        else {
            fprintf(stderr, "%c is not a valid digit in base 20.", x);
            exit(EXIT_FAILURE);
        }
        sum += digit * exponent;
        exponent *= 20;
        num_scanned = scanf("%c", &x);
        if (num_scanned != 1) {
           fprintf(stderr, "Invalid input\n");
           exit(EXIT_FAILURE);
        }
    }
    printf("%d\n",sum);
    return EXIT_SUCCESS;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64749004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档