number to word code help

Hello, the purpose of this program is to have a user input a number, and the program will then read out the number in words. Example: User inputs 10004, you get one zero zero zero four.

Also, any leading zero should be ignored. user inputs 00001, you get one.

Looking at the final for loop with the switch statement, part of my problem from setting up how the loop and the switch statement interact.

Any hints or ideas?

---------------

#include <stdio.h>

int main(){

int n,m,i,j,k,temp,temp2,numdigits; //add all the variables!

printf("Enter an integer no bigger than 10 digits and not starting with 0: ");//The only easy part of this program
scanf("%d",&n);

numdigits=0; //begin the count here
m=n;
for(j=0;m>0;j++)//counting the number of digits
{
m=m/10;//Divide m, initilized as n, by 10. Keep going until m<=0
numdigits=j+1;//numdigits: adds one to the count each time the loop runs.
}
HERE IS WHERE THE PROBLEM BEGINS
m=n;//re-initialize m as n
for (i=numdigits-1;i>=0;i--)
{
temp=0;
for (k=numdigits-1;k>=0;k--)
{
temp=temp*10;
temp2=n%10;
temp=temp+temp2;
m=temp2%10;
}


switch (m){
case 1:
printf("one ");
break;
case 2:
printf("two ");
break;
case 3:
printf("three ");
break;
case 4:
printf("four ");
break;
case 5:
printf("five ");
break;
case 6:
printf("six ");
break;
case 7:
printf("seven ");
break;
case 8:
printf("eight ");
break;
case 9:
printf("nine ");
break;
case 0:
printf("zero ");
default:
printf("error!");
}
}
return 0;
}
Last edited on
closed account (zybCM4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * main.cpp
 *
 *  Created on: 19 Feb 2014
 *    
 */

#include <iostream>
#include <string>

using namespace std;

string drop_zeros(const int &length, const string &storage )
{
	int position_count = 0;
	int leading_zero_count = 0;
	string return_string = storage;

	while( position_count <= length )
	{
		char number = storage[position_count];

		if( number == '0' )
		{
			//increase count of leading zero
			leading_zero_count++;
			//cout << number_store << endl;
		}
		else if( number != '0')
		{
			break;
		}
		position_count++;
	}
	if( leading_zero_count != 0 )
	{
		return_string.erase( 0, leading_zero_count);
	}
	return return_string;
}

int num_to_words( const string &storage, const short int &length)
{
	short int position_count = 0;

	//Loop will cycle until there are no more numbers in the array.
	while( position_count < length )
	{
		//Store the number as a char because C++ doesn't allow comparison
		//for strings.
		//Char is used because otherwise it gets the Ascii number instead
		char number = storage[position_count];

		//Check out which char this number is and then output the word
		//associated with it.
		switch( number )
		{
			case('0'):
			{
				cout << "Zero";
				break;
			}
			case('1'):
			{
				cout << "One";
				break;
			}
			case('2'):
			{
				cout << "Two";
				break;
			}
			case('3'):
			{
				cout << "Three";
				break;
			}
			case('4'):
			{
				cout << "Four";
				break;
			}
			case('5'):
			{
				cout << "Five";
				break;
			}
			case('6'):
			{
				cout << "Six";
				break;
			}
			case('7'):
			{
				cout << "Seven";
				break;
			}
			case('8'):
			{
				cout << "Eight";
				break;
			}
			case('9'):
			{
				cout << "Nine";
				break;
			}
		}
		//Space the numbers horizontally
		cout << ", ";

		//Move to the next number.
		position_count++;
	}

	return 0;
}


int main()
{
	//Use string to get input and also using the array-like qualities.
	string number_store;
	//Not expecting a number over 30,000.
	short int length_of_number = 0;
	//Since above is short....
	short int count = 0;

	//Using getline since it's preferred over cin
	getline( cin, number_store);
	length_of_number = number_store.size();

	//Checking that we have the right details.
	cout << "Number: " << number_store << endl;
	cout << "Count: " << count << endl;
	cout << "Length of String: " << length_of_number << endl;

	number_store = drop_zeros(length_of_number, number_store);
	length_of_number = number_store.size();
	cout << "Number storage: " << number_store << endl;
	num_to_words(number_store, length_of_number);

	//Reset!


//	//Even though we don't need it, lets use it for completeness!
	return 0;
}






That should be good enough, it makes use of the things you should have learned and a few extras that you should have covered in your own time. (Who does homework anyway).

For loops are not recommended with variable lengths.

Switch statements are recommended if you're not doing comparisons. If anyone wants to chip in with some tidying up ideas I'd welcome them.

Edit:

Functions have been added to clean up the thing, passing by the safer const refs of course (although this program is not `that` large, it's good practice.

The program successfully strips leading zeros and converts numbers to words from the end users perspective anyway.

My advice to you in the future is to make your variable names far more descriptive. Instead of i or j, try counting_position_in_array.

Yeah. It's much longer, but it's also much clearer as it's not always obvious what you're counting.

Last edited on
Topic archived. No new replies allowed.