Hello,
I have a simple program I wrote, that takes a 5 digit number and finds the sum of its individual digits. The program reads the user input using scanf as a string. It then iterates through that null terminated char array [string], while converting each digit to an int and summing the result. I get a "general protection fault" at line 14 sum += atoi(input[i]);
Any idea why this is happening? My program looks perfectly valid to me. Do you think it has to do with protections in CVI built around strings?
#include <stdio.h>
#include <stdlib.h>
#define DIGITS 5
int main(void){
char input[10];
int sum = 0;
printf("Enter a number 10000 <= n <= 99999\n");
scanf("%s", input);
for(int i = 0; i < DIGITS; ++i){ //populate the digits array with the value of each digit
sum += atoi(input[i]);
}
printf("the sum is %d", sum);
getchar();
return 0;
}
ENV:
CVI 2017
Windows 10 64-bit