CS50:credit.c 中的致命错误
CS50: fatal errors in credit.c
我是一个真正的新程序员,之前写过3个代码! (包括你好世界!)
我真的试图找出一种算法来帮助我找出 credit.c 程序,但我的程序一直出现 运行 错误。现在的代码非常混乱,但如果我先了解它的功能,我会尝试更好地设计它。这是代码,
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void);
long long CCno;//since int cant hold this long number
{
do
{
x = get_long_long("Number: ") //asks for user to input cc number
}
while (x < 0 || x > 9999999999999999); //returns a non zero number
}
int numcount = 0;
int numcount = (x == 0) ? 1 : (log(x) + 1);
//calculate checksum
//Finds alternate numbers and multiplies with 2
int digit1 = (((x % 100) / 10) * 2);
int digit2 = (((x % 10000) / 1000) * 2);
int digit3 = (((x % 1000000) / 100000) * 2);
int digit4 = (((x % 100000000) / 10000000) * 2);
int digit5 = (((x % 10000000000) / 1000000000) * 2);
int digit6 = (((x % 1000000000000) / 100000000000) * 2);
int digit7 = (((x % 100000000000000) / 10000000000000) * 2);
int digit8 = (((x % 10000000000000000) / 1000000000000000) * 2);
//Find the other remaining numbers
int digit9 = ((x % 10) / 1);
int digit10 = ((x % 100) / 10);
int digit11 = ((x % 1000) / 100);
int digit12 = ((x % 10000) / 1000);
int digit13 = ((x % 100000) / 10000);
int digit14 = ((x % 1000000) / 100000);
int digit15 = ((x % 10000000) / 1000000);
int digit16 = ((x % 100000000) / 10000000);
int sum1 = ((digit1 % 10) + (digit1 / 10));
sum1 = (sum1 + (digit2 % 10)) + (digit2 / 10);
sum1 = (sum1 + (digit3 % 10)) + (digit3 / 10);
sum1 = (sum1 + (digit4 % 10)) + (digit4 / 10);
sum1 = (sum1 + (digit5 % 10)) + (digit5 / 10);
sum1 = (sum1 + (digit6 % 10)) + (digit6 / 10);
sum1 = (sum1 + (digit7 % 10)) + (digit7 / 10);
sum1 = (sum1 + (digit8 % 10)) + (digit8 / 10);
int checksum = (sum1 + digit9 + digit10 + digit11 + digit12 + digit13 + digit14 + digit15 + digit16)
//check first two digits
if
{
(numcount == 15)
(digit1 == 3 && digit2 == 4 || 7)
printf("AMEX");
}
if
{
(numcount == 13 || 16)
(digit1 == 4)
printf("VISA");
}
if
{
(numcount == 16)
(digit1 == 5 && digit2 == 1 || 2 || 3 || 4 || 5)
printf("MASTERCARD");
}
else
{
printf("INVALID");
}
请帮我找出错误!!
edit- 现在所有其他错误都已更正,还有一个错误。
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main( void );
long long x;//since int cant hold this long number
{
do
{
x = get_long_long("Number: ") //asks for user to input cc number
}
while (x < 0 || x > 9999999999999999); //returns a non zero number
int numcount = 0;
int numcount = (x == 0) ? 1 : (log(x) + 1);
//calculate checksum
//Finds alternate numbers and multiplies with 2
int digit1 = (((x % 100) / 10) * 2);
int digit2 = (((x % 10000) / 1000) * 2);
int digit3 = (((x % 1000000) / 100000) * 2);
int digit4 = (((x % 100000000) / 10000000) * 2);
int digit5 = (((x % 10000000000) / 1000000000) * 2);
int digit6 = (((x % 1000000000000) / 100000000000) * 2);
int digit7 = (((x % 100000000000000) / 10000000000000) * 2);
int digit8 = (((x % 10000000000000000) / 1000000000000000) * 2);
//Find the other remaining numbers
int digit9 = ((x % 10) / 1);
int digit10 = ((x % 100) / 10);
int digit11 = ((x % 1000) / 100);
int digit12 = ((x % 10000) / 1000);
int digit13 = ((x % 100000) / 10000);
int digit14 = ((x % 1000000) / 100000);
int digit15 = ((x % 10000000) / 1000000);
int digit16 = ((x % 100000000) / 10000000);
int sum1 = ((digit1 % 10) + (digit1 / 10));
sum2 = (sum1 + (digit2 % 10)) + (digit2 / 10);
sum3 = (sum1 + (digit3 % 10)) + (digit3 / 10);
sum4 = (sum1 + (digit4 % 10)) + (digit4 / 10);
sum5 = (sum1 + (digit5 % 10)) + (digit5 / 10);
sum6 = (sum1 + (digit6 % 10)) + (digit6 / 10);
sum7 = (sum1 + (digit7 % 10)) + (digit7 / 10);
sum8 = (sum1 + (digit8 % 10)) + (digit8 / 10);
int checksum = (sum1 + sum2 + sum3 + sum4 + sum5 + sum6 + sum7 + sum8 + digit9 + digit10 + digit11 + digit12 + digit13 + digit14 + digit15 + digit16)
//check first two digits
if
{
(numcount == 15)
(digit1 == 3 && digit2 == 4 || 7)
printf("AMEX");
}
if
{
(numcount == 13 || 16)
(digit1 == 4)
printf("VISA");
}
if
{
(numcount == 16)
(digit1 == 5 && digit2 == 1 || 2 || 3 || 4 || 5)
printf("MASTERCARD");
}
else
{
printf("INVALID");
}
}
这是错误信息-
clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow
credit.c -lcrypt -lcs50 -lm -o credit
credit.c:8:1: error: expected identifier or '('
{
^
1 error generated.
<builtin>: recipe for target 'credit' failed
make: *** [credit] Error 1
这是一个问题:
int main(void); // <-- get rid of this semicolon
long long CCno; // <-- this either needs to be declared above main, or between
{ // the {} of the function body
do
{
x = get_long_long("Number: ") //asks for user to input cc number
}
while (x < 0 || x > 9999999999999999); //returns a non zero number
}
第一步,重写如下:
int main( void )
{
long long CCNo;
// do loop as written above
// everything involving numcount, digit, etc.
}
还有很多问题,但这至少应该让您克服一些错误。
我是一个真正的新程序员,之前写过3个代码! (包括你好世界!) 我真的试图找出一种算法来帮助我找出 credit.c 程序,但我的程序一直出现 运行 错误。现在的代码非常混乱,但如果我先了解它的功能,我会尝试更好地设计它。这是代码,
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void);
long long CCno;//since int cant hold this long number
{
do
{
x = get_long_long("Number: ") //asks for user to input cc number
}
while (x < 0 || x > 9999999999999999); //returns a non zero number
}
int numcount = 0;
int numcount = (x == 0) ? 1 : (log(x) + 1);
//calculate checksum
//Finds alternate numbers and multiplies with 2
int digit1 = (((x % 100) / 10) * 2);
int digit2 = (((x % 10000) / 1000) * 2);
int digit3 = (((x % 1000000) / 100000) * 2);
int digit4 = (((x % 100000000) / 10000000) * 2);
int digit5 = (((x % 10000000000) / 1000000000) * 2);
int digit6 = (((x % 1000000000000) / 100000000000) * 2);
int digit7 = (((x % 100000000000000) / 10000000000000) * 2);
int digit8 = (((x % 10000000000000000) / 1000000000000000) * 2);
//Find the other remaining numbers
int digit9 = ((x % 10) / 1);
int digit10 = ((x % 100) / 10);
int digit11 = ((x % 1000) / 100);
int digit12 = ((x % 10000) / 1000);
int digit13 = ((x % 100000) / 10000);
int digit14 = ((x % 1000000) / 100000);
int digit15 = ((x % 10000000) / 1000000);
int digit16 = ((x % 100000000) / 10000000);
int sum1 = ((digit1 % 10) + (digit1 / 10));
sum1 = (sum1 + (digit2 % 10)) + (digit2 / 10);
sum1 = (sum1 + (digit3 % 10)) + (digit3 / 10);
sum1 = (sum1 + (digit4 % 10)) + (digit4 / 10);
sum1 = (sum1 + (digit5 % 10)) + (digit5 / 10);
sum1 = (sum1 + (digit6 % 10)) + (digit6 / 10);
sum1 = (sum1 + (digit7 % 10)) + (digit7 / 10);
sum1 = (sum1 + (digit8 % 10)) + (digit8 / 10);
int checksum = (sum1 + digit9 + digit10 + digit11 + digit12 + digit13 + digit14 + digit15 + digit16)
//check first two digits
if
{
(numcount == 15)
(digit1 == 3 && digit2 == 4 || 7)
printf("AMEX");
}
if
{
(numcount == 13 || 16)
(digit1 == 4)
printf("VISA");
}
if
{
(numcount == 16)
(digit1 == 5 && digit2 == 1 || 2 || 3 || 4 || 5)
printf("MASTERCARD");
}
else
{
printf("INVALID");
}
请帮我找出错误!! edit- 现在所有其他错误都已更正,还有一个错误。
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main( void );
long long x;//since int cant hold this long number
{
do
{
x = get_long_long("Number: ") //asks for user to input cc number
}
while (x < 0 || x > 9999999999999999); //returns a non zero number
int numcount = 0;
int numcount = (x == 0) ? 1 : (log(x) + 1);
//calculate checksum
//Finds alternate numbers and multiplies with 2
int digit1 = (((x % 100) / 10) * 2);
int digit2 = (((x % 10000) / 1000) * 2);
int digit3 = (((x % 1000000) / 100000) * 2);
int digit4 = (((x % 100000000) / 10000000) * 2);
int digit5 = (((x % 10000000000) / 1000000000) * 2);
int digit6 = (((x % 1000000000000) / 100000000000) * 2);
int digit7 = (((x % 100000000000000) / 10000000000000) * 2);
int digit8 = (((x % 10000000000000000) / 1000000000000000) * 2);
//Find the other remaining numbers
int digit9 = ((x % 10) / 1);
int digit10 = ((x % 100) / 10);
int digit11 = ((x % 1000) / 100);
int digit12 = ((x % 10000) / 1000);
int digit13 = ((x % 100000) / 10000);
int digit14 = ((x % 1000000) / 100000);
int digit15 = ((x % 10000000) / 1000000);
int digit16 = ((x % 100000000) / 10000000);
int sum1 = ((digit1 % 10) + (digit1 / 10));
sum2 = (sum1 + (digit2 % 10)) + (digit2 / 10);
sum3 = (sum1 + (digit3 % 10)) + (digit3 / 10);
sum4 = (sum1 + (digit4 % 10)) + (digit4 / 10);
sum5 = (sum1 + (digit5 % 10)) + (digit5 / 10);
sum6 = (sum1 + (digit6 % 10)) + (digit6 / 10);
sum7 = (sum1 + (digit7 % 10)) + (digit7 / 10);
sum8 = (sum1 + (digit8 % 10)) + (digit8 / 10);
int checksum = (sum1 + sum2 + sum3 + sum4 + sum5 + sum6 + sum7 + sum8 + digit9 + digit10 + digit11 + digit12 + digit13 + digit14 + digit15 + digit16)
//check first two digits
if
{
(numcount == 15)
(digit1 == 3 && digit2 == 4 || 7)
printf("AMEX");
}
if
{
(numcount == 13 || 16)
(digit1 == 4)
printf("VISA");
}
if
{
(numcount == 16)
(digit1 == 5 && digit2 == 1 || 2 || 3 || 4 || 5)
printf("MASTERCARD");
}
else
{
printf("INVALID");
}
}
这是错误信息-
clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow
credit.c -lcrypt -lcs50 -lm -o credit
credit.c:8:1: error: expected identifier or '('
{
^
1 error generated.
<builtin>: recipe for target 'credit' failed
make: *** [credit] Error 1
这是一个问题:
int main(void); // <-- get rid of this semicolon
long long CCno; // <-- this either needs to be declared above main, or between
{ // the {} of the function body
do
{
x = get_long_long("Number: ") //asks for user to input cc number
}
while (x < 0 || x > 9999999999999999); //returns a non zero number
}
第一步,重写如下:
int main( void )
{
long long CCNo;
// do loop as written above
// everything involving numcount, digit, etc.
}
还有很多问题,但这至少应该让您克服一些错误。