0

I did the "credit" assignment for pset1 and even though it passes all tests and works, I find it in terrible style and cumbersome and ugly. Can you make some suggestions to improve the style and make things more concise?

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

long long findn(long long num);

int main(void) {

    printf("Number: ");
    long long n = get_long_long();

    long long res = findn(n);

    switch(res) {
        case 13:
        break;

        case 15: 
        break; 

        case 16:
        break;

        default:
        printf("INVALID\n");
        return 0;
        break;
    }

    long long oprtr = pow(10, res-2);

    long long first_digit = (n / (oprtr*10)) % 10;

    long long second_digit = (n / oprtr) % 10;

    switch(first_digit) {
        case 4:
            printf("VISA\n");
            break;

        case 3:
            if (second_digit == 4) {
                printf("AMEX\n");
            }

            else if (second_digit == 7) {
                printf("AMEX\n");
            }

            else {
                printf("INVALID\n");
            }
            break;

        case 5:
            switch (second_digit) {
                case 1:
                printf("MASTERCARD\n");
                break;

                case 2:
                printf("MASTERCARD\n");
                break;

                case 3: 
                printf("MASTERCARD\n");
                break;

                case 4:
                printf("MASTERCARD\n");
                break;

                case 5:
                printf("MASTERCARD\n");
                break;

                default:
                printf("INVALID\n");
                break;
            }
            break;

        default:
            printf("INVALID\n");
            break;
    }
}

long long findn(long long num)
{
    char snum[100];    
    return sprintf(snum, "%lld", num);
}
1

You can condense your switch statements where the cases all return the same output, for example:

switch (second_digit) {
    case 1:
    case 2:
    case 3: 
    case 4:
    case 5:
        printf("MASTERCARD\n");
        break;

    default:
        printf("INVALID\n");
        break;
    }
1
  • Thanks, that's a useful tip!
    – alex1x
    Feb 17 '17 at 1:45
0

I am watching and learning right along with the 2016 CS50 and am now in pset2, so don't think that this answer comes from a super knowledgeable person. With that being said, I think your Amex case and your Mastercard case can be shortened into 1 simple "If statement" for each. For your Amex Case (Case 3) you can use ||(the character above the backslash) which means OR for your test, e.g. if (second_digit == 4 || second_digit == 7) which would be if that second digit equals 4 or 7 do this otherwise do something else. For the Mastercard case you can use && which means AND for your test, e.g. if (second_digit > 0 && second_digit < 6) which would be if the second digit is greater than 0 and less than 6 do this, otherwise do something else.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .