c程序未打印出所需输出

np8igboo  于 2023-01-20  发布在  其他
关注(0)|答案(1)|浏览(149)

我写了这个代码在C作为一个问题集从CS50,需要解决的问题是写一个程序,可以检查如果信用卡是有效的,然后如果卡是有效的识别乙醚卡是VISA,美国运通卡或万事达卡。我写的算法来检查卡号是否有效,如果卡号是无效的,则打印无效。我的问题是,我的代码不I don“我不想把名字打印出来

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

int main(void)
{
long card;
//prompt the user for card number
do
{
    card = get_long("Card no: ");
}
while (card < 0); // card number must always be greater than zero

//count digit starting from second-last then multiply by two
int card1,card2,card3,card4,card5,card6,card7,card8;

card1 = ((card % 100)/10 * 2);
card2 = ((card % 10000)/1000 * 2);
card3 = ((card % 1000000)/100000 * 2);
card4 = ((card % 100000000)/10000000 * 2);
card5 = ((card % 10000000000)/1000000000 * 2);
card6 = ((card % 1000000000000)/100000000000 * 2);
card7 = ((card % 100000000000000)/10000000000000 * 2);
card8 = ((card % 10000000000000000)/1000000000000000 * 2);

card1 = (card1 % 100 / 10) + (card1 % 10);
card2 = (card2 % 100 / 10) + (card2 % 10);
card3 = (card3 % 100 / 10) + (card3 % 10);
card4 = (card4 % 100 / 10) + (card4 % 10);
card5 = (card5 % 100 / 10) + (card5 % 10);
card6 = (card6 % 100 / 10) + (card6 % 10);
card7 = (card7 % 100 / 10) + (card7 % 10);
card8 = (card8 % 100 / 10) + (card8 % 10);

int sum1 = card1 + card2 + card3 + card4 + card5 + card6 + card7 + card8;

//find digits that where not multiplyed by two
int card9,card10,card11,card12,card13,card14,card15,card16;

card9 = (card % 10);
card10 = ((card % 1000)/100);
card11 = ((card % 100000)/10000);
card12 = ((card % 10000000)/1000000);
card13 = ((card % 1000000000)/100000000);
card14 = ((card % 100000000000)/10000000000);
card15 = ((card % 10000000000000)/1000000000000);
card16 = ((card % 1000000000000000)/100000000000000);

int card9,card10,card11,card12,card13,card14,card15,card16;

card9 = (card % 10);
card10 = ((card % 1000)/100);
card11 = ((card % 100000)/10000);
card12 = ((card % 10000000)/1000000);
card13 = ((card % 1000000000)/100000000);
card14 = ((card % 100000000000)/10000000000);
card15 = ((card % 10000000000000)/1000000000000);
card16 = ((card % 1000000000000000)/100000000000000);

int sum2 = card9 + card10 + card11 + card12 + card13

c
18th Jan 2023, 12:39 PM
Khulyso Dev®
Khulyso Dev® - avatar
3 Answers

Sort by: Votes
Answer
0
you mean I should first write code on sololearn playground and then share a link, the one I posted above I wrote it using VS code
18th Jan 2023, 2:53 PM
Khulyso Dev®
Khulyso Dev® - avatar
0

int sum2 = card9 + card10 + card11 + card12 + card13 + card14 + card15 + card16;
int sum3 = sum1 + sum2;

int length = 0;
long visa = card;
long amex = card;
long master = card;

if((sum3 % 10)!= 0)
{
    printf("INVALID\n");
    return 0;
}

//Differetiate between visa,master and Amex
while (card >0)
{
    card = card / 10;
    length++;
}

//Identify if its VISA
while (visa >= 0)
{
    visa /= 10;
}
if (visa == 4 && (length == 13 || length == 16))
{
    printf("VISA\n");
    return 0;
}

//identify if its Amex
while (amex >= 10000000000000)
{
    amex /= 10000000000000;
}
if (length == 15 && (amex == 34 || amex == 37))
{
    printf("AMEX\n");
    return 0;
}

//identify if its mastercard
while (master >= 100000000000000)
{
    master /= 100000000000000;
}
if (length == 16 && (master == 51 || master == 52 || master == 53 || master == 54 || master == 55))
{
    printf("MASTERCARD\n");
    return 0;
}
else
printf("INVALID\n");
return 0;
}

我试图替换printf(“%s\n”,“VISA”);使用printf(“VISA\n”),但仍无法打印出所需的输出

djp7away

djp7away1#

是时候使用调试器了。或者仔细看看这个:

//Identify if its VISA
while (visa >= 0)
{
    visa /= 10;
}

只有当visa小于0时,这个循环才会结束,而这只有在visa初始为负时才会发生。
但是只有当visa为4时才输入下面的代码--这是从来不会发生的。

if (visa == 4 && (length == 13 || length == 16))
{
    printf("VISA\n");
    return 0;
}

相关问题