尝试将字符串转换为整数数组的艰难时期

Rough times trying to convert string to integers array

基本上程序会询问用户信用卡号,将其捕获到字符串中,将字符串转换为整数数组(这样我就可以验证信用卡号,用于以后的数字总和和乘法)。

我需要一个带有整数数组的变量,下面的代码存储 ASCII 值。

尝试阅读了很多帖子,但没有读懂。

如果有任何帮助,我将不胜感激,这样我就可以再次入睡了 =) 亲切地,

#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

}

不要使用 (int) 转换为 int,而是使用 atoi()

而不是 cc_digits[i] = (int) cc_string[i] 尝试 cc_digits[i] = cc_string[i] - '0' 如果你只是转换为 int 你会得到字符的 ASCII 代码,但是如果你减去字符代码 0 那么你应该得到实际的数字。

找到解决方案,想与大家分享guys.Thank大家的帮助,时间和学习。

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

int main (void)

{
    long long cc_num;
    int array_dig[15];
    int static sum_evens, sum_odds, sum_tot, valid = 0;
    printf("Enter your credit card number for validation: \n");
    cc_num = GetLongLong();

    for (int i=15; i >= 0; i--)
        {
            array_dig[i] = cc_num % 10;
            cc_num = cc_num / 10;

            //Sanity check
            printf("pos %d = %d\n", i, array_dig[i]);
        }

    //Summing digits
    for (int j=0; j < 16; j++)
        {
            if (j % 2 == 0)
            {
            sum_evens += array_dig[j];

            //Sanity check
            printf("sum_evens = %d/n", sum_evens);
            }

            else if (j % 2 == 1)
                {
                    if (array_dig[j] < 5)
                    {
                        sum_odds += 2 * array_dig[j];

                        //Sanity check
                        printf("sum_odds for char < 5 = %d\n", sum_odds);
                    }
                    else
                    {
                        sum_odds += (2 * array_dig[j]) % 10 + 1;

                        //Sanity check
                        printf("sum_odds for char >= 5 = %d\n", sum_odds);
                    }
                }
        }

         sum_tot = sum_evens + sum_odds;

         //Validation (if sum % 10 = 0 it is valid)
         valid = sum_tot % 10;
         printf("sum_tot = %d, valid = %d \n", sum_tot, valid);
            if (valid % 10 == 0)
                {
                    printf("Valid credit card \n");
                }
            else
                {
                    printf("Invalid credit card\n");
                }

}

不会这么快。 =( 将值(数字)传递给 long long 变量 cc_num.

时程序会丢失精度
#include <cs50.h>
#include <stdio.h>
#include <string.h>

int main (void)

{
    long long cc_num;
    char cc_num_str[15];

    int static sum_evens, sum_odds, sum_tot, valid = 0;
    int static size;
    do
        {
            printf("Enter your credit card number for validation: \n");
            cc_num = GetLongLong();
            sprintf(cc_num_str, "%lld", cc_num);
            size = strlen (cc_num_str);
        }
    while (cc_num < 1 || cc_num > 9999999999999999 || size < 13 || size > 16);

    int array_dig[size];

    for (int i = size - 1; i >= 0; i--)
        {
            array_dig[i] = cc_num % 10;
            cc_num = cc_num / 10;

            //Sanity check
            printf("pos %d = %d\n", i, array_dig[i]);
        }

    //Summing digits
    for (int j=0; j < size; j++)
        {
            if (j % 2 == 0)
            {
            sum_evens += array_dig[j];

            //Sanity check
            printf("sum_evens = %d/n", sum_evens);
            }

            else if (j % 2 == 1)
                {
                    if (array_dig[j] < 5)
                    {
                        sum_odds += 2 * array_dig[j];

                        //Sanity check
                        printf("sum_odds for char < 5 = %d\n", sum_odds);
                    }
                    else
                    {
                        sum_odds += (2 * array_dig[j]) % 10 + 1;

                        //Sanity check
                        printf("sum_odds for char >= 5 = %d\n", sum_odds);
                    }
                }
        }

         sum_tot = sum_evens + sum_odds;

         //Validation (if sum % 10 = 0 it is valid)
         valid = sum_tot % 10;
         printf("sum_tot = %d, valid = %d \n", sum_tot, valid);
            if (valid % 10 == 0)
                {
                    //code that will verifies card type (to be written)

                    printf("Valid credit card \n");
                }

            else
                {
                    printf("Invalid. \n");
                }
    return 0;    
}

这是输出:

 Enter your credit card number for validation: 
    1234567809874366
    pos 15 = 8
    pos 14 = 3
    pos 13 = 6
    pos 12 = 9
    pos 11 = 5
    pos 10 = 8
    pos 9 = 9
    pos 8 = 0
    pos 7 = 8
    pos 6 = 7
    pos 5 = 6
    pos 4 = 5
    pos 3 = 4
    pos 2 = 3
    pos 1 = 2
    pos 0 = 1
    sum_evens = 1/nsum_odds for char < 5 = 4
    sum_evens = 4/nsum_odds for char < 5 = 12
    sum_evens = 9/nsum_odds for char >= 5 = 15
    sum_evens = 16/nsum_odds for char >= 5 = 22
    sum_evens = 16/nsum_odds for char >= 5 = 31
    sum_evens = 24/nsum_odds for char >= 5 = 32
    sum_evens = 33/nsum_odds for char >= 5 = 35
    sum_evens = 36/nsum_odds for char >= 5 = 42
    sum_tot = 78, valid = 8 
    Invalid.