c - 为什么 get_long_long ("") 打印两次?

标签 c cs50

下面代码中的

get_long_long("") 打印了 2 次,我不确定为什么。它打印,请输入您的信用卡号...”,2次。(注意:我不是在骗人,我们正在为 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);

最佳答案

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

关于c - 为什么 get_long_long ("") 打印两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53110881/

相关文章:

C Fread 将文本文件拆分为 block

c - 如何将二进制字符串转换为无符号整数?

c - 使用 C 程序压缩文件

c - 如何在 C 中调试此错误 : function definition is not allowed here?

创建一个函数; scanf 和 char 问题

c - 如何用c语言构建左半角金字塔

c - 分割数组中的数字

c - 指针数组和括号内的指针

c - 用1随机填充100X100矩阵的100格

CS50:recover.c ~ 什么数据类型可以保存 JPG 的字节(具体为 512 个字节)?