为什么我在 if 语句中出现“预期标识符”错误?[关闭]

Why do I get "expected identifier" error in if statement? [closed]

提问人:rickityciket 提问时间:11/17/2023 更新时间:11/17/2023 访问量:35

问:


这个问题是由错别字或无法再现的问题引起的。虽然类似的问题可能在这里是主题,但这个问题的解决方式不太可能帮助未来的读者。

6天前关闭。

我正在处理 cs50 第 1 周的 pset1 中的信用问题。我在具有 2 个条件的 if 语句中在线收到“预期标识符”错误。

        if (credit_card == 34 || credit_card == 37 ) && (credit_length == 15)

我也会发布整个代码。

#include <cs50.h>
#include <stdio.h>
// Write a program that promts the user for a credit card number and prints if it is AMEX, MasterCard, Visa, or invalid
// get credit card number from user
// count length of credit card
// check if credit card is valid (13, 15, or 16 digits)
// Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products’ digits together. (sum 2)
// Add the sum to the sum of the digits that weren’t multiplied by 2 (sum 1)
// (maybe last step)check if starts with 34 or 37 for AMEX, 51/52/53/54/55 for MasterCardd, or 4 for Visa.
int main(void)
{
    long credit_card = get_long("Credit Card #: ");

    int i = 0;
    long cc_length = credit_card;
    while (cc_length > 0)
    {
        cc_length = cc_length / 10;
        i++;
    }
    if (i != 13 && i != 15 && i != 16)
    {
        printf("INVALID\n");
        return 0;
    }
    int sum1 = 0;
    int sum2 = 0;
    int mod1;
    int mod2;
    long n = credit_card;
    int total;
    int valid = 0;
    do
    {
        mod1 = (n % 10);
        sum1 = mod1 + sum1;
        n = n / 10;
        mod2 = (n % 10) * 2;
        if (mod2 >= 10)
        {
            mod2 = mod2 % 10;
            sum2 = mod2 + sum2 + 1;
        }
        else
        {
            sum2 = mod2 + sum2;
        }
        n = n / 10;
    }
    while (n > 0);
    total = sum1 + sum2;
    if (total % 10 != 0)
    {
        printf("INVALID\n");
    }
    else if (total % 10 == 0)
    {
        valid = 1;
    }
    do
    {
        credit_card = credit_card / 10;
    }
    while (credit_card >= 100);
    if (valid == 1)
    {
        if (credit_card == 34 || credit_card == 37 ) && (credit_length == 15)
        {
            printf("AMEX\n");
        }
        else if (credit_card == 51 || credit_card == 52 || credit_card == 53 || credit_card == 54 || credit_card == 55)
        {
            printf("MASTERCARD\n");
        }
        else if (credit_card / 10 == 4)
        {
            printf("VISA\n");
        }
        else
        {
            printf("INVALID\n");
        }
    }
    else
    {
        printf("INVALID\n");
    }
}

没有完成,可能设计得不好。我是新来的。错误

中C CS50

评论

1赞 Jabberwocky 11/17/2023
不要发布文字图片。将文本作为文本发布。错误日志是文本。
0赞 273K 11/17/2023
@tkausl 那些不是 s,那些是 s. OP,你缺少括号。whiledo
2赞 Oka 11/17/2023
if (credit_card == 34 || credit_card == 37 ) && (credit_length == 15)格式不正确。用括号将整个表达式括起来。if ((...) && (...))
0赞 Support Ukraine 11/17/2023
更改为 或if ((credit_card == 34 || credit_card == 37 ) && (credit_length == 15))if ((credit_card == 34 || credit_card == 37 ) && credit_length == 15)
0赞 rickityciket 11/17/2023
@Jabberwocky 我无法弄清楚如何在 CLI :( 中复制文本。谢谢大家

答: 暂无答案