Creating a string for decimals
To Anyone Who Can Help-
I'm in the midst of typing a code for a program where the user enters a numerical dollar amount as a string and has it convert to word form.
Ex) $67,218.21 becomes Sixty-seven thousand, two hundred eighteen and twenty one hundredth dollars.
For debugging purposes, I've had it print out the string length count with all characters, which it shows. It also gives out an error for things such as sign and magnitude.
The main thing that has been hampering me in my process is the decimal place. I'm trying to get the program to, upon seeing the decimal, take the string values past the right decimal and put them into a separate string, and put NULL values where the old values were. My problem is that it isn't modifying these values; the user entry string remains unchanged, and the decimal string has garbage.
I especially know it doesn't work because I'm trying to have it subtract from the previous length count and add to a decimal length count, and then print out both strings, neither of which the program does. What exactly is it that I'm doing wrong?
Code:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
typedef char* string;
int dec_con(char decimal[]);
int main(){
char amount[21]; //variable for data entry
char decimal[2]; //variable for modified decimal places
int length = 0; //length counter
int length_2 = 0; //decimal length counter
int i, j; //variables for for loop
puts("Please enter the amount you wish to convert into words.");
scanf("%20[^\n]", amount);
for (i = 0; i < amount[i]; i++) //count string length
length++;
printf("%i\n", length); //DEBUG
if ((unsigned)strlen(amount) > 19){ //error for length, then program break
printf("Error. Magnitude must be than one trillion.\n");
return 0;
}
if (amount[0] == '-'){ //error for sign, then program break
printf("Error. Magnitude must be positive.\n");
return 0;
}
for (i = 0; i < amount[i]; i++){ //grouping by columns
if (amount[i] == ','){
}
}
for (i = 0; i < amount[i]; i++){ //check for decimals
if (amount[i] == '.'){
for (j = 0; j < decimal[j]; j++){ //
decimal[j] = amount[j+1]; //put variables in the new string
amount[i+j] = NULL; //replace its original location with NULL
length--; //subtract from original count
length_2++; //add to decimal count
}
}
}
printf("%i %i\n", length, length_2); //DEBUG
for (i = 0; i < amount[i]; i++)
printf("%c", amount[i]);
puts("");
for (i = 0; i < decimal[i]; i++)
printf("%c", decimal[i]);
puts("");
dec_con(decimal); //calls the decimal conversion function
}
int dec_con(char decimal[]){
if (decimal[0] == '0'){
if (decimal[1] == '0')
printf("dollars.");
if (decimal[1] == '1')
printf("and one hundredth dollars.");
if (decimal[1] == '2')
printf("and two hundredths dollars.");
if (decimal[1] == '3')
printf("and three hundredths dollars.");
if (decimal[1] == '4')
printf("and four hundredths dollars.");
if (decimal[1] == '5')
printf("and five hundredths dollars.");
if (decimal[1] == '6')
printf("and six hundredths dollars.");
if (decimal[1] == '7')
printf("and seven hundredths dollars.");
if (decimal[1] == '8')
printf("and eight hundredths dollars.");
if (decimal[1] == '9')
printf("and nine hundredths dollars.");
}
else if (decimal[0] == '1'){
if (decimal[1] == '0')
printf("and one tenth dollars.");
if (decimal[1] == '1')
printf("and eleven hundredths dollars.");
if (decimal[1] == '2')
printf("and twelve hundredths dollars.");
if (decimal[1] == '3')
printf("and thirteen hundredths dollars.");
if (decimal[1] == '4')
printf("and fourteen hundredths dollars.");
if (decimal[1] == '5')
printf("and fifteen hundredths dollars.");
if (decimal[1] == '6')
printf("and sixteen hundredths dollars.");
if (decimal[1] == '7')
printf("and seventeen hundredths dollars.");
if (decimal[1] == '8')
printf("and eighteen hundredths dollars.");
if (decimal[1] == '9')
printf("and nineteen hundredths dollars.");
}
else{
if (decimal[0] == '2')
printf("and twenty ");
if (decimal[0] == '3')
printf("and thirty ");
if (decimal[0] == '4')
printf("and forty ");
if (decimal[0] == '5')
printf("and fifty ");
if (decimal[0] == '6')
printf("and sixty ");
if (decimal[0] == '7')
printf("and seventy ");
if (decimal[0] == '8')
printf("and eightty ");
if (decimal[0] == '9')
printf("and ninety ");
if (decimal[1] == '1')
printf("one hundredth dollars.");
if (decimal[1] == '2')
printf("two hundredths dollars.");
if (decimal[1] == '3')
printf("three hundredths dollars.");
if (decimal[1] == '4')
printf("four hundredths dollars.");
if (decimal[1] == '5')
printf("five hundredths dollars.");
if (decimal[1] == '6')
printf("six hundredths dollars.");
if (decimal[1] == '7')
printf("seven hundredths dollars.");
if (decimal[1] == '8')
printf("eight hundredths dollars.");
if (decimal[1] == '9')
printf("nine hundredths dollars.");
}
return 0;
}