简体   繁体   English

CS50 Credit,如果不是卡号则循环

[英]CS50 Credit, loop if not card number

I have nearly finished the Credit task of PSET1 (CS50) although I have run into a problem trying to get the program to not allow an entry that isn't a card number (Eg "Foo" or "t" or anything other than a number) and then loop.我几乎完成了 PSET1 (CS50) 的 Credit 任务,尽管我遇到了一个问题,试图让程序不允许输入不是卡号的条目(例如“Foo”或“t”或除卡号以外的任何内容)号),然后循环。

I have attempted various while statements and goto statements although none of these attempts have been sucessful我尝试了各种 while 语句和 goto 语句,尽管这些尝试都没有成功

Also any other suggestions would be appreciated.任何其他建议也将不胜感激。

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

int main(void)
{
        //gets cardnumber & counts digits entered
        long long cardnumber;
        int count = 0;
            {
                //prompts user for a cardnumber
                printf("Enter a cardnumber: ");
                scanf("%lld", &cardnumber);
            }
        //Prints count of digits entered (will likely not need this part)
        //else printf("Number of digits: %d", count); printf("\n");
        //Finds first 8 digits for checksum)

        int digit1 = (((cardnumber % 100) / 10) * 2);
        int digit2 = (((cardnumber % 10000) / 1000) * 2);
        int digit3 = (((cardnumber % 1000000) / 100000) * 2);
        int digit4 = (((cardnumber % 100000000) / 10000000) * 2);
        int digit5 = (((cardnumber % 10000000000) / 1000000000) * 2);
        int digit6 = (((cardnumber % 1000000000000) / 100000000000) * 2);
        int digit7 = (((cardnumber % 100000000000000) / 10000000000000) * 2);
        int digit8 = (((cardnumber % 10000000000000000) / 1000000000000000) * 2);

        //Sum first 8 digits of checksum

        int check1 = ((digit1 % 10) + (digit1 / 10));
        check1 = (check1 + (digit2 % 10)) + (digit2 / 10);
        check1 = (check1 + (digit3 % 10)) + (digit3 / 10);
        check1 = (check1 + (digit4 % 10)) + (digit4 / 10);
        check1 = (check1 + (digit5 % 10)) + (digit5 / 10);
        check1 = (check1 + (digit6 % 10)) + (digit6 / 10);
        check1 = (check1 + (digit7 % 10)) + (digit7 / 10);
        check1 = (check1 + (digit8 % 10)) + (digit8 / 10);

        //Sum last 8 digits of checksum & Sum of first 8 (check1)

        int digit9 = ((cardnumber % 10) / 1);
        int digit10 = ((cardnumber % 1000) / 100);
        int digit11 = ((cardnumber % 100000) / 10000);
        int digit12 = ((cardnumber % 10000000) / 1000000);
        int digit13 = ((cardnumber % 1000000000) / 100000000);
        int digit14 = ((cardnumber % 100000000000) / 10000000000);
        int digit15 = ((cardnumber % 10000000000000) / 1000000000000);
        int digit16 = ((cardnumber % 1000000000000000) / 100000000000000);

        //add to remaining digits to those that were multiplied by 2

        int checksum = (check1 + digit9 + digit10 + digit11 + digit12 + digit13 + digit14 + digit15 + digit16);

        // Check first 2 digits

        if (cardnumber / 10000000000000 == 34 || cardnumber / 10000000000000 == 37 )
        goto Amex;

        if (cardnumber / 100000000000000 == 51 || cardnumber / 100000000000000 == 52 || cardnumber / 100000000000000 == 53 || cardnumber / 100000000000000 == 54 || cardnumber / 100000000000000 == 55 )
        goto Master;

        if (cardnumber / 1000000000000000 == 4 )
        goto Visa;
        else
            {
                printf("INVALID\n");
                (I = 0);
            }
        goto End;
        // Amex start with 34 or 37
        Amex:;
        char A;
        if (cardnumber / 10000000000000 == 34 || cardnumber / 10000000000000 == 37 )
            {
                A = 1;
            }
        else
            {
                A = 0;
            }
        goto End;
        // Mastercard start with 51, 52, 53, 54, 55
        Master:;
        char M;
        if (cardnumber / 100000000000000 == 51 || cardnumber / 100000000000000 == 52 || cardnumber / 100000000000000 == 53 || cardnumber / 100000000000000 == 54 || cardnumber / 100000000000000 == 55 )
            {
                M = 1;
            }
        else
            {
                M = 0;
            }
        goto End;
        //Visa start with 4
        Visa:;
        char V;
        if (cardnumber / 1000000000000000 == 4 )
            {
                V = 1;
            }
        else
            {
                V = 0;
            }
        goto End;
        End:;
        while(cardnumber != 0)
            {
                // cardnumber = cardnumber/10
                cardnumber /= 10;
                ++count;
            }
        // if cardnumber <13 or >16 digits error "invalid card" given
        if (count > 16 || count < 13)
            {
                printf("INVALID\n");
                (I = 0);
            }
            // Keep track of first 2 digits & validate checksum
        char T;
        if (checksum % 10 == 0)
            {
                T = 1;
            }
        else
            {
                T = 0;
            }
        if (T == 0) printf("INVALID\n");
            {
                (I = 0);
            }
        // - Amex (15 digits, start with 34 or 37)
        if (count == 15 && T == 1 && A == 1)
            {
                printf("AMEX\n");
            }
        // - Mastercard (16 digits, start with 51, 52, 53, 54, 55)
        if (count == 16 && T == 1 && M == 1)
            {
                printf("MASTERCARD\n");
            }
        // - Visa (13 & 16 digits, start with 4)
        if ((count == 13 && T == 1 && V == 1) || (count == 16 && T == 1 && V == 1))
        {
            printf("VISA\n");
        }
}

Prompt the user to type in the credit card number, use a do while loop so that if the number is less than 0, you will keep asking the user for a card number.提示用户输入信用卡号,使用 do while 循环,这样如果数字小于 0,您将不断向用户询问卡号。

int main(void)
{
long long credit_card_number;

do
{
  credit_card_number = get_long_long("Enter Valid Credit Card Number: ");
}
while (credit_card_number < 0);
#include <cs50.h>
#include <math.h>
#include <stdio.h>

int main(void)
{
    long long cardnumber = 0;
    do
    {
        cardnumber = get_long("Number: ");
    }
    while (cardnumber < 0);
    int count = 0;
    long long digits = cardnumber;
    while (digits > 0)
    {
        digits = digits/10;
        count++;
    }
    if ((count != 13) && (count != 15) && (count != 16))
    {
        printf("INVALID\n");
    }
    int number[count];
    for (int i = 0; i < count; i++)
    {
        number[i] = cardnumber % 10;
        cardnumber = cardnumber / 10;
    }
    int originalnumber[count];
    for (int i = 1; i < count; i++)
    {
        originalnumber[i] = number[i];
    }
    for (int i = 1; i < count; i+=2)
    {
        number[i] = number[i] * 2;
    }
    int v = 0;
    int temp;
    if (count == 13)
    {
      for (int i = 0; i < count; i++)
      {
        temp = (number[i] % 10) + (number[i]/10 % 10);
        v = v + temp;
      }
      if (originalnumber[12] == 4 && v % 10 == 0)
      {
        printf("VISA\n");
      }
      else
      {
        printf("INVALID\n");
      }
    }
    if (count == 15)
    {
      for (int i = 0; i < count; i++)
      {
        temp = (number[i] % 10) + (number[i]/10 % 10);
        v = v + temp;
      }
      if (originalnumber[14] == 3 && v % 10 == 0 && (originalnumber[13] == 4 || originalnumber[13] == 7))
      {
        printf("AMEX\n");
      }
      else
      {
        printf("INVALID\n");
      }
    }
    if (count == 16)
    {
      for (int i = 0; i < count; i++)
      {
        temp = (number[i] % 10) + (number[i]/10 % 10);
        v = v + temp;
      }
      if (originalnumber[15] == 4 && v % 10 == 0)
      {
        printf("VISA\n");
      }
      else if (originalnumber[15] == 5 && v % 10 == 0 && (originalnumber[14] == 1 || originalnumber[14] == 2 || originalnumber[14] == 3 || originalnumber[14] == 4 || originalnumber[14] == 5))
        {
            printf("MASTERCARD\n");
        }
      else
      {
        printf("INVALID\n");
      }
    }
  
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM