Hello all!
I am having trouble writing this code where I am supposed to take integers and output them as strings. When i try a number like 67 it outputs it correctly like sixty seven. However, when i try 467 it only outputs four hundred sixty. Suggestions appreciated!

``````#include <iostream>
#include <string>
using namespace std;
string n2w1to999(int digit);
string n2w1to9(int digit);

int main ()
{
int number;
int newnumber;
string words;

cout << "Input:" ;
cin >> number ;
cout << "Output:";
if (number == 0)
{
words = "zero" ;
cout << words << endl ;
}
if (number > 999999999)
{
words = "Error" ;
cout << words << endl;
}
else
{
if (number > 999999 && number <= 999999999 )
{
newnumber = number/1000000 ;
words = n2w1to999(newnumber);
cout << words << "million " ;
number = number - newnumber*1000000 ;
}
if (number > 999 && number <= 999999 )
{
newnumber = number/1000 ;
words = n2w1to999(newnumber);
cout << words << "thousand " ;
number = number - newnumber*1000 ;
}
if (number > 9 && number <= 999)
{
words = n2w1to999( number);
cout << words << endl ;

}

if (number > 0 && number <= 9)
{
words = n2w1to9(number) ;
cout << words << endl;
}

}
}
string n2w1to9(int digit)
{
string words2 ;
if (digit == 1)
{
words2 = "one " ;
}
if (digit == 2)
{
words2 = "two " ;
}
if (digit == 3)
{
words2 = "three " ;
}
if (digit == 4)
{
words2 = "four " ;
}
if (digit == 5)
{
words2 = "five " ;
}
if (digit == 6)
{
words2 = "six " ;
}
if (digit == 7)
{
words2 = "seven " ;
}
if (digit == 8)
{
words2 = "eight " ;
}
if (digit == 9)
{
words2 = "nine " ;
if (digit == 0)
{
words2 = "" ;
}
}
return words2;
}
string n2w1to999(int digit)
{
int residual;
int number;
string words1;
if (digit>100)
{
number = digit/100;
words1 = n2w1to9(number) + "hundred " ;
residual = digit - (number * 100);
}
else
{
residual = digit;
}

if (residual < 100 && residual > 19)
{
if (residual < 100 && residual > 89)
{
words1 += "ninety " ;
residual = digit - 90;

}
if (residual < 90 && residual > 79)
{
words1 += "eighty " ;
residual = digit - 80	;
}
if (residual < 80 && residual > 69)
{
words1 += "seventy " ;
residual = digit - 70;
}
if (residual < 70 && residual > 59)
{
words1 += "sixty " ;
residual = digit - 60;
}
if (residual < 60 && residual > 49)
{
words1 += "fifty " ;
residual = digit - 50;
}
if (residual < 50 && residual > 39)
{
words1 += "forty " ;
residual = digit - 40;
}
if (residual < 40 && residual > 29)
{
words1 += "thirty " ;
residual = digit - 30 ;
}
if (residual < 30 && residual > 19)
{
words1 += "twenty " ;
residual = digit - 20;
}

}

if (residual == 19)
{
words1 += "nineteen " ;
}
if (residual == 18)
{
words1 += "eighteen " ;
}
if (residual == 17)
{
words1 += "seventeen " ;
}
if (residual == 16)
{
words1 += "sixteen " ;
}
if (residual == 15)
{
words1 += "fifteen " ;
}
if (residual == 14)
{
words1 += "fourteen " ;
}
if (residual == 13)
{
words1 += "thirteen " ;
}
if (residual == 12)
{
words1 += "twelve " ;
}
if (residual == 11)
{
words1 += "eleven " ;
}
if (residual == 10)
{
words1 += "ten " ;
}
if (residual > 0 && residual <= 9)
{
words1 += n2w1to9(residual) ;
}
return words1;
}``````

Edited by uclaEE: n/a

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by ravenous

In 42 through 47, you never subtract off the part of the number that was just added to the string, so number is still = 467, which is greater than 9 so your last if statement gets skipped over.

Edited by jonsca: n/a

I think you're not subtracting the residual bits of number that are less than 100 from the number in the function `n2w1to999()` . You should replace the lines that look like:

``````if (residual < 80 && residual > 69){
words1 += "seventy " ;
residual = digit - 70;
}``````

with something like

``````if (residual < 80 && residual > 69){
words1 += "seventy " ;
residual = digit - 70 - 100*number;
}``````

For example, if `digit = 477` then `digit - 70 = 407` , not 7 like you want :)

Also, your code is quite poorly formatted on the site. You can't always just copy and paste from your editor. I have reformatted it so that it is easier to read below:

``````#include <iostream>
#include <string>

using namespace std;
string n2w1to999(int digit);
string n2w1to9(int digit);

int main ()
{
int number;
int newnumber;
string words;

cout << "Input:" ;
cin >> number ;
cout << "Output:";
if (number == 0){
words = "zero" ;
cout << words << endl ;
}

if (number > 999999999){
words = "Error" ;
cout << words << endl;
}
else{
if (number > 999999 && number <= 999999999 ){
newnumber = number/1000000 ;
words = n2w1to999(newnumber);
cout << words << "million " ;
number = number - newnumber*1000000 ;
}
if (number > 999 && number <= 999999 ){
newnumber = number/1000 ;
words = n2w1to999(newnumber);
cout << words << "thousand " ;
number = number - newnumber*1000 ;
}
if (number > 9 && number <= 999){
words = n2w1to999( number);
cout << words << endl ;
}

if (number > 0 && number <= 9){
words = n2w1to9(number) ;
cout << words << endl;
}
}

return 0;
}

string n2w1to9(int digit)
{
string words2 ;
if (digit == 1){    words2 = "one " ;   }
if (digit == 2){    words2 = "two " ;   }
if (digit == 3){    words2 = "three " ; }
if (digit == 4){    words2 = "four " ;  }
if (digit == 5){    words2 = "five " ;  }
if (digit == 6){    words2 = "six " ;   }
if (digit == 7){    words2 = "seven " ; }
if (digit == 8){    words2 = "eight " ; }
if (digit == 9){    words2 = "nine " ;  }
if (digit == 0){    words2 = "" ;       }

return words2;
}

string n2w1to999(int digit)
{
int residual;
int number = 0;
string words1;
if (digit > 100){
number = digit/100;
words1 = n2w1to9(number) + "hundred " ;
residual = digit - (number * 100);
}
else{   residual = digit;   }

cout << "number = " << number << endl;

if (residual < 100 && residual > 19){
if (residual < 100 && residual > 89){
words1 += "ninety " ;
residual = digit - 90 - 100*number;
}
if (residual < 90 && residual > 79){
words1 += "eighty " ;
residual = digit - 80 - 100*number	;
}
if (residual < 80 && residual > 69){
words1 += "seventy " ;
residual = digit - 70 - 100*number;
}
if (residual < 70 && residual > 59){
words1 += "sixty " ;
residual = digit - 60 - 100*number;
cout << digit << " " << residual << endl;
}
if (residual < 60 && residual > 49){
words1 += "fifty " ;
residual = digit - 50 - 100*number;
}
if (residual < 50 && residual > 39){
words1 += "forty " ;
residual = digit - 40 - 100*number;
}
if (residual < 40 && residual > 29){
words1 += "thirty " ;
residual = digit - 30  - 100*number;
}
if (residual < 30 && residual > 19){
words1 += "twenty " ;
residual = digit - 20 - 100*number;
cout << digit << " " << residual << endl;
}
}

if (residual == 19){    words1 += "nineteen " ; }
if (residual == 18){    words1 += "eighteen " ;     }
if (residual == 17){    words1 += "seventeen "; }
if (residual == 16){    words1 += "sixteen " ;  }
if (residual == 15){    words1 += "fifteen " ;  }
if (residual == 14){    words1 += "fourteen " ; }
if (residual == 13){    words1 += "thirteen " ; }
if (residual == 12){    words1 += "twelve " ;   }
if (residual == 11){    words1 += "eleven " ;   }
if (residual == 10){    words1 += "ten " ;      }
if (residual > 0 && residual <= 9){
cout << "test" << endl;
words1 += n2w1to9(residual) ;
}

return words1;
}``````

Edited by ravenous: n/a