为什么get_long_long(“”)打印两次?

时间:2018-11-01 23:43:00

标签: c cs50

下面代码中的

get_long_long("")正在打印2次,但我不确定为什么。它将Please enter your credit card number..."打印两次。 (注意:我不是在骗人,我们正在为AP计算机科学课做这个项目,基本上是编写一个程序来检查卡是否合法。)

这不是完整的代码,只是开头,询问他们拥有的卡提供商,然后将使用该信息来确定卡是否合法。

    #include <cs50.h>
    #include <stdio.h>
    #include <string.h>


    char type1[100]; //array used to store and then compare which card is being used
    char visa[25] = "Visa";
    char amex[25] = "Amex";
    char master[25] = "Mastercard";
    long long card;

    int main(void)
    {
        printf("Is your card Visa, Mastercard, or Amex?\n");
        //read the card type then store it in type array
        scanf("%s", type1);


        if (strcmp(type1, master) == 0 || strcmp(type1, visa) == 0 || 
        strcmp(type1, amex) == 0)
        {
            card = get_long_long("Please enter your credit card 
            number\n");
        }
        do
        {
            printf("Is your card Visa, Mastercard, or Amex?\n");
            scanf("%s", type1);
        }
        while (strcmp(type1, master) == 0 || strcmp(type1, visa) == 0 
            || strcmp(type1, amex) != 0);

1 个答案:

答案 0 :(得分:1)

reduce转换scanf与一系列非空白字符匹配(跳过任何前导空白),并在遇到尾随空白时停止。这意味着任何尾随空白(例如行尾的换行符)都不会被读取。即使不知道非标准%s的内部结构,我几乎可以肯定,它首先遇到换行符并再次询问,因为这看起来与用户相同,只是按Enter而不输入其他任何内容。

相关问题