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.
#include <iostream>
#include <string>
usingnamespace 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).
...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*/
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.