mardi 24 août 2021

Check if int_long variable first character is equal to a certain number in c

I'm currently doing the cs50 computer science Harvard online course and my current challenge is to check if the first character inside a variable is equal to a number here is my code

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

int main(void) {
    long card;
    char n = card[0];
    do {
    card = get_long("Enter Credit Card Number: ");
    } while (card < 15);

    if (n == 3) {
        printf("AMEX\n");
    }



    /*
    AMEX\n 34 or 37;
    MASTERCARD\n 51, 52, 53, 54, or 55
    VISA\n  4
    INVALID\n
    */
}

the pseudo-code would go as follow

    if (input first number is equal to 3) {
        print "AMEX" //new line
    } else if (input first number is equal to 5) {
        print "MASTERCARD"8 //new line
    }

Here is the Challenge link

I tried to do

if (card[0] == 3) {
    printf("AMEX\n");
}

But since it's not an array it didn't work.

i also try to search online for answers but since I'm not familiar with c at all (I learn HTML, css, javascript and react) i don't really understand the layout of c

Sorry if my post is kinda hard to read I'm new to this whole StackOverflow thing if you need anything else just ask please

Aucun commentaire:

Enregistrer un commentaire