下面代码中的
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/