Search code examples
cconditional-statementscs50return-typefunction-call

Cannot get if statement to evaluate variable that is equivalent to the adding of the results of two functions


I've found myself in a circle that I can't get out of where the compiler won't evaluate if totalSum % 10 == 0

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

int countingMachine(long n);
int oddAdd(long cNum2)
{
    int n = 0;
    long tempCred = cNum2;
    int add = 0;
    long double tempData = 0;
    while (tempCred != 0)
    {
        if (n % 2 != 0)
        {
            tempData = (tempCred % 10);
            if (tempData <= 0)
            {
                tempData = 0;
                add += (int) tempData;
            }
            add += tempData;
        }
        tempCred /= 10;
        n++;
    }
    return add;
}
int multAdd(long cNum)
{
    int n = 0;
    long tempCred = cNum;
    int evenAdd = 0;
    int tempData = 0;
    while (tempCred != 0)
    {
        tempCred /= 10;
        if(n % 2 == 0)
        {
            tempData = (tempCred % 10)*2;
            if (tempData >= 10)
            {
                evenAdd += tempData % 10;
                evenAdd += tempData / 10;
            }
            else
            {
                evenAdd += tempData;
            }
        }
        n++;
    }
    return evenAdd;
}

long divNum(int count)
{
    long long int divisor;
    int i;
    for(divisor = 10, i = 0; i <= count - 1; i++)
    {
        divisor = divisor * 10;
    }
    return divisor;
}

int mathCheck(long cardNum, long neoDiv)
{
    int primeTwo = cardNum / neoDiv;
    return primeTwo;
}


int main(void)
{
    int am1 = 34;
    int am2 = 37;
    int mc1 = 51;
    int mc2 = 52;
    int mc3 = 53;
    int mc4 = 54;
    int mc5 = 55;
    int vZA = 4;
    long n = 0;
    int tempCount = 0;
    int totalSum;
    long ccNum = 0;
    while (ccNum <= 0)
    {
        ccNum = get_long("Enter Credit Card Number\n");
    }
    tempCount = ccNum;
    totalSum = oddAdd(ccNum); + multAdd(ccNum) % 10;
    tempCount = countingMachine(tempCount);
    printf("%i\n", tempCount);
    long long int divi = divNum(tempCount);
    printf("%lld\n", divi);
    long firstTwo = ccNum / divi;
    printf("%li\n", firstTwo);
    while (firstTwo >= 40 && firstTwo <= 50)
    {
        firstTwo /= 10;
    }

    if (firstTwo == am1 || firstTwo = am2 (&& totalSum % 10 == 0))
    {
        printf("Number: %li\n", ccNum);
        printf("BANK OF AMERICA")
    }
    if (firstTwo == mc1 || mc2 || mc3 || mc4 || mc5 (&& totalSum % 10 == 0))
    {
        printf("Number: %li\n", ccNum);
        printf("MASTERCARD");
    }

}

int countingMachine(long n)
{

    int count = 0;
    while(n != 0)
    {
        count++;
        n /= 10;
    }
    return count;

}

I have tried defining the functions with both void and int as return types, and neither seems to work, as it gives me the error && within '||' place parenthesis around the && statement to silence this warning for

if (firstTwo == am1 || am2 && totalSum == 0)

And when that's done, I get

called object type 'int' is not a function or a function pointer or invalid operand to binary expression ('void *' and 'int')

and if I attempt to call the functions with a return type of int instead of void, like in the code, I get much of the same errors, just without void *.


Solution

  • Unfortunately you misunderstood the diagnostic message of the warning. Such messages are more for the experienced user, who remembers how to apply the suggestion properly.

    Starting with the conditional you wrote:

    if (firstTwo == am1 || am2 && totalSum == 0)
    

    The compiler told you to "place parenthesis around the && statement to silence this warning".

    And you tried, as the complete source currently shows:

    if (firstTwo == am1 || am2 (&& totalSum == 0))
    

    Both of these tries syntactically tell the compiler to use am2 as a function object and call it. But since am2 is just an int object, it cannot do this. Therefore, it produced the next error message.

    What the warning message really means is to put the complete expression (statement) with the binary operator && in parentheses:

    if (firstTwo == am1 || (am2 && totalSum == 0))
    

    This makes visible that && is evaluated before ||, which can be overseen easily.

    This will actually silent the warning, but it is not what you apparently want. This conditional will compare am2 with zero as the left operand of the && operator.

    C needs explicit expressions, when it comes to AND/OR-combined comparisons. Most probably this is what you want:

    if ((firstTwo == am1 || firstTwo == am2) && totalSum == 0)
    

    Note: There are more serious issues with your code, but for now this answers the asked and hardest problem. Reduce your code to a simple first working stage, and only if this works, add some small next feature. Do not try to write a "big bang" solution.