ILoveYeezis
ILoveYeezis

Reputation: 27

If statement conditions unmet and yet program gets stuck

Basically the conditions are unmet and yet the program will not proceed from the main if statement into the else statement. I'll give the gist of it, a tl;dr version upfront and the full version below that.

tl;dr version:

a = 1;
b = 2;
c = 3;

if(a == 1)
{
    if(b == 1 && c == 1)
    do x;

    if(b == 2 && c == 2)
    do y;

    if(b == 3 && c == 3)
    do z;
}
else
printf("Invalid\n");

Basically none of the smaller if statement conditions are met and the program should proceed to the else statement and print "Invalid", but it won't. It just does nothing. For the actual program, see:

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

int checksum = 10;
int j = 16;
long first = 5;
long second = 6;

int main(void)
{
    checksum %= 10;
    if(checksum == 0)
    {
        if(j == 15 && first == 3 && (second == 4 || second == 7))
        printf("AMEX\n");

        if(j == 16 && first == 5 && (second == 1 || second == 2 || second == 3 || second == 4 || second == 5))
        printf("MASTERCARD\n");

        if((j == 16 || j == 13) && first == 4)
        printf("VISA\n");
    }
    else
    printf("INVALID\n");
}

Upvotes: 0

Views: 134

Answers (2)

Edwin Buck
Edwin Buck

Reputation: 70939

If you wish to "make the code look a little cleaner" you can use a programming pattern called "exit early." The basic idea is that for each "correct" answer, you return the value. Since you are not using functions yet, your program will have to exit early; but, typically a function returns the "correct" value instead of the program returning if it succeeded / failed.

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

int checksum = 10;
int j = 16;
long first = 5;
long second = 6;

int main(void) {
    checksum %= 10;
    if(checksum == 0 && j == 15 && first = 3 && (second == 4 || second == 7) {
        printf("AMEX\n");
        return EXIT_SUCCESS;
    }
    if (checksum == 0 && j == 16 && first == 5 && (second == 1 || second == 2 || second == 3 || second == 4 || second == 5))
        printf("MASTERCARD\n");
        return EXIT_SUCCESS;
    }
    if (checksum == 0 && (j == 16 || j == 13) && first == 4)
        printf("VISA\n");
        return EXIT_SUCCESS;
    }
    printf("INVALID\n");
    return EXIT_FAILURE;
}

The int in int main(void) { is the program's return code. It is sent back to the terminal so a person can tell if the program ran correctly or encountered an error. EXIT_SUCCESS and EXIT_FAILURE are C constants, defined in (I think <stdlib.h>).

Also, consider getting into the habit of using int main(int argc, char* argv) over int main(void). Even if you don't use the argument parameters right away, it is the one way to type the main function that you'll never have to update.

Upvotes: 1

Giovanni Pachera
Giovanni Pachera

Reputation: 81

Your program prints "INVALID" only when the bigger if statement is unmet. You need to change your code by adding another else associated with the smaller if statement conditions, as follow:

#include <cs50.h>    
#include <stdio.h>
    
    int checksum = 10;
    int j = 16;
    long first = 5;
    long second = 6;
    
    int main(void)
    {
        checksum %= 10;
        if(checksum == 0)
        {
            if(j == 15 && first == 3 && (second == 4 || second == 7))
                printf("AMEX\n");
    
            else if(j == 16 && first == 5 && (second == 1 || second == 2 || second == 3 || second == 4 || second == 5))
                printf("MASTERCARD\n");
    
            else if((j == 16 || j == 13) && first == 4)
                printf("VISA\n");
    
            else printf("INVALID\n");
        }
        else
            printf("INVALID\n");
    }

Hope it could be helpful,

Giovanni Pachera

Upvotes: 0

Related Questions