Visual Studio中的Scanf不接受多个字符的情况

时间:2019-05-08 15:02:28

标签: c string scanf ascii

我正在为字母/数字ASCII表创建一个转换项目。我的代码应该是“交互式的”,因此用户可以在屏幕上输入“ y”或“ n”来回答问题。但是,它不想重复两次...

我尝试过:

  1. 只尝试数字而不是字符,但这不是我想要的
  2. %[\n]*c%[\n]c%[\n]*s ...技术,但无济于事;-;
  3. 在不同的项目中进行测试,但我唯一能做的是将多个scanf()连续放置。

代码如下:

printf("Would you like to convert a number today? \n");
printf("Please press Y or N \n");
scanf("%c", &input);

if (input == 'y' || input == 'Y') { //compare input if they said 'yes'
    printf("\nThank you! \nWhat number?\n");
    scanf("%d", &number);
    flag = test(number);
    if (flag == 0) { //if there is an equivalent letter
        letter = conversion(number); //find the equivalent letter
        printf("\nYour Number \t ASCII letter\n");
        printf("%d\t %c\n", number, letter);
    }
}
else if (input == 'n' || input == 'N') {
    printf("\nWould you like to convert a letter instead? This time enter 0 or 1\!\n\n"); //problem here!!
    printf("I wish I could say it was to \' Spice things up \' ...but it\'s not ;-; \n\n");
    scanf("%d", &input2);

    if (input2 == 0) { //this needs to be checking whether the user input Y/y
        printf("Great choice adventurer!\n");
        printf("What letter will it be today?\n\n");

        //..I would go to a different funtion here ie: test2(letter)...

        scanf("%d", &number); //I showed that it worked with multiple numbers, but I can't get this to work with multiple letters
        printf("%d", number);
    }

    if (input2 == 1) { //this needs to be checking whether the user input N/n
        printf("Difficult to please, I see...\n\n");
        printf("I suggest you move on with that attitude!\n\n");
        printf("Bye bye then\n");
    }
}
else { //if they tried to break the code
    printf("Sorry I did not recognise your command...please retry\n");
    printf("Press Y or N next time!\n");
}

第一张支票可以完美工作,我只希望第二张支票像第一张支票一样! 一些“解决方案”导致了溢出,如果可能的话,我不希望这样做 即使有人可以解释为什么这没有按照我的意图工作也会很有帮助!

1 个答案:

答案 0 :(得分:3)

我不确定是什么让您感到困惑。

使用

char foo;
scanf(" %c", &foo);

用于单个字符,例如。字母和

int bar;
scanf("%d", &bar);

用于数字,整数。如果您键入字母,则scanf()将失败。

%[...]用于字符串。

scanf()返回成功的转换次数(或EOF),因此

int height;
int width;
scanf("%d %d", &height, &width);

如果成功,它将返回2。如果只能读取1,则可能返回height

因此,要检查用户输入是否有错误,您应该执行以下操作:

int height;
int width;
if (scanf("%d %d", &height, &width) != 2) {
    // handle the error, maybe exit the program.
}

您的代码可能看起来像这样(没有错误处理):

#define _CRT_SECURE_NO_WARNINGS  // you said Visual Studio? Without it you should get
                                 // warnings about some functions being insecure.

#include <ctype.h>   // isalpha()  returns true if the value is a letter
#include <stdlib.h>  // EXIT_SUCCESS
#include <stdio.h>   // puts(), printf(), scanf()

int main(void)
{
    for(;;) {  // for-ever ... endless loop since the user exits by answering
               // 'n' or 'N' two times

        puts("Would you like to convert a number today?\nPlease press Y or N:");
        char input;
        if (scanf(" %c", &input) != 1)  // We reached EOF ... end of file
            break;                      // that's improbable for stdin,
                                        // but input could be redirected to
                                        // read from a file instead.

        if (input == 'y' || input == 'Y') {
            puts("\nThank you!\nWhat number?");
            int number;
            scanf("%d", &number);

            if (isalpha((char unsigned)number))  // *)
                printf("\nYour Number \t ASCII letter\n%d\t %c\n\n", number, number);
            else
                puts("Sorry, but that's not the ASCII code of a letter :(\n");
        }
        else if (input == 'n' || input == 'N') {
            puts("\nWould you like to convert a letter instead?\nPlease press Y or N:");
            scanf(" %c", &input);

            if (input == 'y' || input == 'Y') {
                puts("\nGreat choice adventurer!\nWhat letter will it be today?");
                char letter;
                scanf(" %c", &letter);

                if (isalpha(letter))
                    printf("\nYour letter \t ASCII code\n%d\t %c\n\n", letter, letter);
                else
                    puts("Sorry, but that's not a letter :(\n");
            }
            else if (input == 'n' || input == 'N') {
                puts("\nDifficult to please, I see...\n\nI suggest you move on with that attitude!\n");
                puts("Bye bye then.");
                return EXIT_SUCCESS;
            }
        }
        else {
            puts("Sorry I did not recognize your command... Please retry.");
            puts("Press Y or N next time!\n");
        }
    }
}

*)isalpha()(以及<ctype.h>中的其他函数)期望的值适合unsigned char或值EOF。对于其他值,它具有未定义的行为。由于我们将用户输入读入int中,因此我们无法确定是否是这种情况,因此我们必须在将值传递给unsigned char(和朋友)之前将其转换为isalpha()

下次您提出问题时,请提供完整的代码,包括变量声明,函数test()conversion()#include。但是,请发布一个示例,以解决您手头的问题。您包括的所有对话框都是不必要的。