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.
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.
...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
cout << "one";
/* so on and so forth*/
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.