我希望有人可以帮助我解决这个问题。我是一个完整而彻底的 C 新手。
这是针对 C 类的学校作业(只是普通的旧 C,而不是 C# 或 C++),教授坚持认为我们被允许使用的唯一编译器是 Borland 5.5。
一般任务是运行一个可以检查信用卡号码有效性的算法。我已经成功地让程序获取用户输入的 CC 号码,然后将该号码分成一个数组。它主要打印出我想要的东西。
但是,当我输入最后一个函数(我这样评论的那个)然后编译时,程序刚刚开始挂起。我不知道是什么原因造成的。
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
//global variables declared.
//in an earlier version, I was going to use multiple functions, but I couldn't make them work
float array[16];
double num, ten;
int i, a, b, x, y, check;
int main()
{
ten = 10;
//pick up user-input number
printf("Enter your credit card number\n>");
scanf("%lf", &num);
//generate the array
for (i = 15; i >= 0; i--)
{
array[i] = fmod(num, ten);
num /= 10;
printf("Array is %1.1lf\n", array[i]);
}
//double every other number. If the number is greater than ten, test for that, then parse and re-add.
//this is where the program starts to hang (I think).
{for (i = 2; i <= 16; i + 2)
{
array[i] = array[i] * 2;
if (array[i] >= 10)
{
a = (int)array[i] % 10;
b = (int)array[i] / 10;
array[i] = a + b;
}
}
printf("%f", array[i]);
}
//add the numbers together
x = array[2] + array[4] + array[6] + array[8] + array[10] + array[12] + array[14] + array[16];
y = array[1] + array[3] + array[5] + array[7] + array[9] + array[11] + array[13] + array[15];
check = x + y;
//print out a test number to make sure the program is doing everything correctly.
//Right now, this isn't happening
printf("%d", check);
return 0;
}