-1

这是我的代码:

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

int main(void)
{
    int answer_compared;

    do  {
        printf("What is your credit card number?\n");
        long long credit_card = GetLongLong ();
        printf("Is your credit card number is %lld?\n", credit_card);
        printf("Please respond Yes or No.\n");
        string answer = GetString ();
        answer_compared = strncasecmp(answer, "y", 1);
    } while (answer_compared != 0);
    int long long cc = credit_card;
    printf("Thank you.\n"); 
    if (cc == 4){
        printf("Visa\n");
    }else if (cc == 34 || cc == 37){
        printf("AMEX\n");
    }else if (cc >= 51 && cc <= 55){
        printf("MC\n");
    }else printf("You did not enter an appropriate credit card.\n");
        printf("I think this is what I need.\n");

}              

我的错误是:

credit.c:17:32: error: use of undeclared identifier 'credit_card'
        int long long cc = credit_card;
                           ^

最初我将所有 cc 变量设置为 credit_card 但更改了它,因为它增加了 5 个错误,都是出于相同的原因。我试图找到讨论在 do-while 循环中创建变量的地方,但无济于事。

你能解释一下这里出了什么问题吗?

4

3 回答 3

1

C 有一个块作用域。如果你想在那里访问它,你需要long long credit_card在外部范围内声明你的变量 ( ) (例如,函数的范围,就像你所做的那样)。answer_compared

除此之外,就数据类型而言,信用卡号不是“数字”。您不应该将其存储为一个 - 使用 achar[20]代替(16 位数字,3 个分隔符,nul 终止符)!不过,您可能希望对其进行规范化(删除 4 个块之间的分隔符),以便您可以正确访问单个数字。

于 2014-02-09T13:48:06.833 回答
0

你必须定义

long long credit_card

在 do while 循环之外

于 2014-02-09T13:47:49.423 回答
0

啊,是的,正如其他人所提到的,这归结为范围。你可以移动

printf("What is your credit card number?\n");
long long credit_card = GetLongLong ();

在您的 do 声明之前,这应该可以解决问题。一般来说,最好在函数开始时初始化变量,然后再设置它们的值。

于 2017-07-30T21:23:55.130 回答