I'm having a problem with taking the individual characters in the string entered by the user and converting them into integers. I use XCode, and the error that pops up when I use atoi function is:
Incompatible integer to pointer conversation passing 'char' to the parameter of type 'const char *'
Then I take the address with &, and then it asks me to fix the issue by adding &. Whenever I do this, however, the debugging screen appears and I have no idea what to do from there.
Can somebody show me how to properly format this?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
char cardnumber[17];
printf("Enter your card number: ");
scanf("\n%s",cardnumber);
int c;
for (c=0; cardnumber[c]!='\0'; c++)
{
if (isdigit(cardnumber[c]) == 0)
{
printf("Invalid Entry");
}
}
int i=0;
int odd1 = atoi(cardnumber[i]);
int odd2 = atoi(cardnumber[i+2]);
int odd3 = atoi(cardnumber[i+4]);
int odd4 = atoi(cardnumber[i+6]);
int odd5 = atoi(cardnumber[i+8]);
int odd6 = atoi(cardnumber[i+10]);
int odd7 = atoi(cardnumber[i+12]);
int odd8 = atoi(cardnumber[i+14]);
int s1 = odd1*2;
int s2 = odd2*2;
int s3 = odd3*2;
int s4 = odd4*2;
int s5 = odd5*2;
int s6 = odd6*2;
int s7 = odd7*2;
int s8 = odd8*2;
int t1 = ((s1/10)+(s1%10));
int t2 = ((s2/10)+(s2%10));
int t3 = ((s3/10)+(s3%10));
int t4 = ((s4/10)+(s4%10));
int t5 = ((s5/10)+(s5%10));
int t6 = ((s6/10)+(s6%10));
int t7 = ((s7/10)+(s7%10));
int t8 = ((s8/10)+(s8%10));
int even1 = atoi(cardnumber[i+1]);
int even2 = atoi(cardnumber[i+3]);
int even3 = atoi(cardnumber[i+5]);
int even4 = atoi(cardnumber[i+7]);
int even5 = atoi(cardnumber[i+9]);
int even6 = atoi(cardnumber[i+11]);
int even7 = atoi(cardnumber[i+13]);
int checkdigit = atoi(cardnumber[i+15]);
int check=((t1+t2+t3+t4+t5+t6+t7+t8+even1+even2+even3+even4+even5+even6+even7)%10);
if(check==0)
{
if(check==checkdigit)
{
printf("Your card is accepted.");
}
else
{
printf("Invalid entry.\n");
}
}
else
{
if(10-check==checkdigit)
{
printf("Your card is accepted.");
}
else
{
printf("Invalid entry.\n");
}
}
return 0;
}
atoiaccepts a null-terminated string (char *), butcardnumber[i]and friends are individual characterscardnumber[i] - '0'