Hi guys the other day i posted a question regarding inputing data into a string. After much research my code has changed lots and i have got to this point
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int input;
int q =0;
int p;
int d=0;
int main(){
char sortingarray[5];
printf("Please enter a 4 digit number\n");
fgets ( sortingarray, 5, stdin );
d = strlen(sortingarray);
printf("%d",d);
if (d == 4){
printf("\n There are %c thousands in your number\n", sortingarray[0]);
printf("\n There are %c hundreds in your number\n", sortingarray[1]);
printf("\n There are %c tens in your number\n", sortingarray[2]);
printf("\n There are %c ones in your number\n\n", sortingarray[3]);
}
else if (d==3){
printf("\n There are 0 thousands in your number\n");
printf("\n There are %c hundreds in your number\n", sortingarray[0]);
printf("\n There are %c tens in your number\n", sortingarray[1]);
printf("\n There are %c ones in your number\n\n", sortingarray[2]);
}
else if (d==2){
printf("\n There are 0 thousands in your number\n");
printf("\n There are 0 hundreds in your number\n");
printf("\n There are %c tens in your number\n", sortingarray[0]);
printf("\n There are %c ones in your number\n\n", sortingarray[1]);
}
else if(d==1){
printf("\n There are 0 thousands in your number\n");
printf("\n There are 0 hundreds in your number\n");
printf("\n There are 0 tens in your number\n");
printf("\n There are %c ones in your number\n\n", sortingarray[0]);
}
system("PAUSE");
return 0;
}
I am having problems with the strlen part of it. What the program needs to do is tell the user how many 1000's, 100's, 10's and 1's that have been entered with a maximum number size of 9999. If 123 is entered the program must say that the is 0 1000's, 1 100, 2, 10's and 3 1's. To be able to do this i have created a series of if and else if statements. What i am having with is when you enter a 4 digit number the program works as it sees that there have been 4 numbers entered. Where it stops working properly is when there have been 3 2 and 1 numbers entered. They give answers but they are 1 number out. So if 3 is entered strlen says that 4 has been entered. When 2 have been entered strlen says there has been 3 entered and so on. What is causing this. It is really confusing me.
Thanks in advance
Sam