Marcello Scattolini Marcello Scattolini - 3 months ago 16
C Question

Rough times trying to convert string to integers array

Basically the program will ask for user credit card number, capture it inside a string, convert a string to an integer array (so I can validate credit card number, for later digit sums and multiplying).

I need a variable with integers array and the code below stores ASCII values.

Tried reading lots of posts, but didn't get it.

I'd appreciate any help, so I could sleep again =)

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

int main (void)

string cc_string;

printf("Please enter a credit card number:\n");

//Captures credit card string
cc_string = GetString();

// Array of credit card digits integers
int cc_digits[16];

for (int i = 0; i<= 15; i++)
cc_digits[i] = (int) cc_string[i];
//Just checking what value has been stored
printf("position %d with %d \n", i, cc_digits[i]);

//to be continued



Instead of cc_digits[i] = (int) cc_string[i] try cc_digits[i] = cc_string[i] - '0' If you just cast to int you'll get the ASCII code for the character, but if you subtract the code of character 0 then you should get the actual digit.