【发布时间】:2022-12-27 06:37:57
【问题描述】:
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 *.
【问题讨论】:
-
You have to use
firstTwo == am1 || firstTwo == am2notfirstTwo == am1 || am2 -
totalSum = oddAdd(ccNum); + multAdd(ccNum) % 10;has an extra semicolon which causes themultAdd(ccNum) % 10to be ignored. -
@Spikatrix: Good spot, and there are several other instances of the same thing. It's tempting to translate
||into words as "or", and assume it works in all the same contexts where you would use the word "or" in English ("if x equals 5 or 6"), but that's a mistake. C is not English, it follows its own rules, and you won't get very far if you try to guess them instead of looking them up. -
@Agatha Fordyce Should
add += tempData;beelse {add += tempData;}?
标签: c conditional-statements cs50 return-type function-call