Need help with code changing numbers to string

Hello everyone,
I'm trying to write a code where the user input a number (1-100) and the program returns the number but in a string.
Example:
1 = one
53 = fifty three
I have an algorithm in mind, but I'm stuck because there is something wrong with the code. I'm using Bloodshed C++.
Okay. Here is the problem: Whenever I input a 58 (and other numbers). I get "fifty seven."
The way my algorithm works is that it reads "50" first and returns fifty. Then I have "8" but this 8 somehow keeps entering in my "if" statement from if(7 <= number && number < 8), and returns 7 which shouldn't happen.
I wrote some cout to keep track of my number 8 and make sure it does not change into another number and this in fact enters in that range if(7 <= number && number < 8), I don't know why.

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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <iostream>
#include <string>


using namespace std;

int main() //string firstNumberToString(double& number)
{
    double number;
       int counter = 0;
       double numberSubstraction = 0;
       double numberMultiplication;
       int greaterThan;
       string firstDigit;
       string numberString;
       string numberToString;
       
       cout << "Enter number " << endl;
       cin >> number;
while(counter != 2)
{
greaterThan = 0;

       if(30 <= number && number <= 99)
       {
             number /= 10;
             numberString = "ty ";
             numberMultiplication = 1;
             greaterThan = 1;
       }   
       
       if(13 <= number && number <= 19)
       {
             number -= 10;
             numberString = "teen ";           
             numberMultiplication = 0;
             greaterThan = 1;
       }
       

       // Output string of the number
       if(1 <= number && number < 2)
       {
            firstDigit = "one ";
            numberSubstraction = 1;
       }
       
       if(2 <= number && number < 3)
       {
            firstDigit = "two ";
            numberSubstraction = 2;
       }
       
       // greaterThan identifies if the number is below 3 then it is "three"
       if(3 <= number && number < 4 && greaterThan == 0)
       {
            firstDigit = "three ";
            numberSubstraction = 3;
       }
       
       // greaterThan identifies if the number is aove 12 then 3 is "thir" 
       if(3 <= number && number < 4 && greaterThan == 1)
       {
            firstDigit = "thir";
            numberSubstraction = 3;
       }
       
       if(4 <= number && number < 5)
       {
            firstDigit = "four ";
            numberSubstraction = 4;
       }
       
       // greaterThan identifies if the number is below 3 then it is "three"
       if(5 <= number && number < 6 && greaterThan == 0)
       {
            firstDigit = "five ";
            numberSubstraction = 5;
       }
       
       // greaterThan identifies if the number is aove 12 then 5 is "fif" 
       if(5 <= number && number < 6 && greaterThan == 1)
       {
            firstDigit = "fif";
            numberSubstraction = 5;
       }
       
       if(6 <= number && number < 7)
       {
            firstDigit = "six ";
            numberSubstraction = 6;
       }
              
       cout << "lalala " << firstDigit << endl;
       cout << "middle of second loop before SEVEN number = " << number << endl;

       if(7 <= number && number < 8)
       {    
            firstDigit = "seven ";
            numberSubstraction = 7;
            cout << "what am I doing here? Why is an 8 entering to this range?"  << endl;
       cout << "middle of second loop inside SEVEN number = " << number << endl;
       }
       cout << "middle of second loop after SEVEN number = " << number << endl;
       
       cout << firstDigit << endl;
       cout << numberToString << endl;
       
       if(8 <= number && number < 9)
       {
            firstDigit = "eight ";
            numberSubstraction = 8;
       }
       
       if(9 <= number && number < 10)
       {
            firstDigit = "nine ";
            numberSubstraction = 9;
       }
       
       if(10 <= number && number < 11)
       {
             firstDigit = "ten ";
             numberSubstraction = 10;
       }
       
       if(11 <= number && number < 12)
       {
             firstDigit = "eleven ";
             numberSubstraction = 11;
       }
       
       if(12 <= number && number < 13)
       {
             firstDigit = "twelve ";            
             numberSubstraction = 12;
       }
       

       greaterThan = 0;
       //Change the initial number back to original and eliminate the first digit
       if(numberMultiplication == 4)
       {
       number = (number*1000000) - (numberSubstraction*1000000);
       }
       
       if(numberMultiplication == 3)
       {
       number = (number*1000) - (numberSubstraction*1000);
       }
       
       if(numberMultiplication == 2)
       {
       number = (number*100) - (numberSubstraction*100);
       }
       
       if(numberMultiplication == 1)
       {
       number = (number-numberSubstraction)*10;
       }
      
cout << "End of first loop number = " << number << endl;
numberToString = numberToString + firstDigit + numberString;
numberString = "";

cout << numberToString << endl;
counter++;


} 

cout << number << endl;

system("pause");
return 0;
}


This is not the final code I still need some work to do but I'm stuck in this part and I need help with this (only this part because I want to do the rest).

Thank you.
...why are you going through so much work to determine if the number is only between 1 and 100? Will users be able to input decimal values? If not, you can make "number" a standard int, replace the entire list of if-else statements, and just replace it with a switch. Otherwise, use the cmath library, still use a switch, but rather than passing the standard "number" to the function, instead use:
1
2
3
4
5
6
7
switch(floor(number))
{
case 1:
    cout << "one";
    break;
//...
/* so on and so forth*/

That should work much more effectively.
Thanks for your reply. The program is supposed to read numbers between 1 and 1 billion. But the code that I'm showing is only 1 to 100. I have the algorithm in mind but I'm stuck here.
Well, for 1 billion, you can simply have the number put in be type double, divide by 1 billion, floor the result, and use the same switch that you use for every case. Then divide by 100 million, and use the same switch. Then, before you divide by 10 million, divide by 1 million (and floor) and check whether that result is greater than 15. If it is, just divide the inputted number by 10 million, floor, switch; then 1 million, floor, switch. If it is not, and it is greater than 10, have a second switch for the 11, 12, 13, 14, and 15 case. Just repeat that same process each time. If it's zero, display nothing for that place.
Okay. Thank you. I really appreciate the time that you took to reply.

Although that was not the answer that I was looking for. My solution is not really that complex. I think is similar to yours but uses if statements and a loop but I just got stuck. I only wanted to know why a number 8 is run in my "if" statement where the condition is a number between 7-8 (not including 8). That seems really weird. I followed my code step by step and still cannot see what is causing it.

The rest I wanted to figure it out by myself since I'm learning.
Topic archived. No new replies allowed.