HELp me

Pages: 12
FINALLY DONE!

Ill fix the limit
Pls try to run it and find some error

as of now I fix the problem about teen and limittt

THANK YOU EVERYONE



[code]

#include <iostream>


using namespace std;

main(){
int num, ones, tens, hundreds, thousands;


a:
cout<<"\t\tConvert Number to Word!\n\n";
cout<<"Input a number from 1-3000: ";
cin>>num;


thousands=(num/1000)%10;
hundreds=((num/100)%100)%10;
tens=(num/10)%10;
ones=num%10;




if (1<=num&&num<=3000)
{

switch(thousands){

case 1:
cout<<"One Thousand ";
break;
case 2:
cout<<"Two Thousand ";
break;
case 3:
cout<<"Three Thousand ";
break;
}

switch (hundreds){
case 1:
cout<<"One Hundred ";
break;
case 2:
cout<<"Two Hundred ";
break;
case 3:
cout<<"Three Hundred ";
break;
case 4:
cout<<"Four Hundred ";
break;
case 5:
cout<<"Five Hundred ";
break;
case 6:
cout<<"Six Hundred ";
break;
case 7:
cout<<"Seven Hundred ";
break;
case 8:
cout<<"Eight Hundred ";
break;
case 9:
cout<<"Nine Hundred ";
break;
}



switch (tens){

case 1:
switch (ones)
{
case 0:
cout<<"ten";
goto b;
break;



case 1:
cout<<"eleven ";

goto b;
break;
case 2:
cout<<"twelve ";
goto b;
break;


case 3:
cout<<"thirteen ";
goto b;
break;
case 4:
cout<<"Fourteen ";
goto b;
break;
case 5:
cout<<"Fifteen ";
goto b;
break;
case 6:
cout<<"Sixteen ";
goto b;
break;
case 7:
cout<<"Seventeen ";
goto b;
break;
case 8:
cout<<"Eighteen ";
goto b;
break;
case 9:
cout<<"Nineteen ";
goto b;
break;
}
case 2:

cout<<"Twenty ";
break;
case 3:
cout<<"Thirty ";
break;
case 4:
cout<<"Fourty ";
break;
case 5:
cout<<"Fifty ";
break;
case 6:
cout<<"Sixty ";
break;
case 7:
cout<<"Seventy ";
break;
case 8:
cout<<"Eighty ";
break;
case 9:
cout<<"Ninety ";
break;
}

switch (ones){
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;
}

b:

return 0;


}

else
{
cout<<"Invalid/Over the Limit"<<endl;
cout<<"Pls. try again\n"<<endl;
goto a;

}
}




Last edited on
It works for me. Nice job.

Have you learned about arrays? You can shorten the code quite a lot if you store and array of cstrings with the names of the digits. Save your file under a different name so you have a working version, and then consider modifying it to do something like this:

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
#include <iostream>

using namespace std;

main()
{
    int num, ones, tens, hundreds, thousands;
    const char *onesNames[10] = {
	 "", "One", "Two", "Three", "Four",
	 "Five", "Six", "Seven", "Eight", "Nine"
    };
    const char *teensNames[10] = {
	"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen",
	"Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"
    };
    const char *tensNames[10] = {
	 "", "Ten", "Twenty", "Thirty", "Forty", "Fifty",
	"Sixty", "Seventy", "Eighty", "Ninety"
    };
 a:
    cout << "\t\tConvert Number to Word!\n\n";
    cout << "Input a number from 1-3000: ";
    cin >> num;

    thousands = (num / 1000) % 10;
    hundreds = ((num / 100) % 100) % 10;
    tens = (num / 10) % 10;
    ones = num % 10;

    if (1 <= num && num <= 3000) {

	if (thousands) {
	    cout << onesNames[thousands] << " Thousand ";
	}
	if (hundreds) {
	    cout << onesNames[hundreds] << " Hundred ";
	}
	if (tens == 1) {
	    cout << teensNames[ones];
	    return 0;
	} else if (tens) {
	    cout << tensNames[tens] << ' ';
	}

	if (ones) {
	    cout << onesNames[ones];
	}
    }

    else {
	cout << "Invalid/Over the Limit" << endl;
	cout << "Pls. try again\n" << endl;
	goto a;

    }
}

closed account (E0p9LyTq)
Feedest, PLEASE learn to use code tags, it makes it much easier to read and comment on your code.

You can edit your posts and add code tags, please do it.
http://www.cplusplus.com/articles/jEywvCM9/

You've been asked, POLITELY, by several others to use code tags.
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
#include <iostream>
#include <string>
using namespace std;

const string UNITS[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
                         "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
const string TENS[] = { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

//======================================================================

string expandThousand( int n )
{
   string result;
   int hundreds, tens, units;

   hundreds = n / 100;
   n %= 100;
   if ( n < 20 ) 
   {
      tens = 0;
      units = n;
   }
   else
   {
      tens = n / 10;
      units = n % 10;
   }

   if ( hundreds > 0 )
   {
      result += UNITS[hundreds] + " hundred";
      if ( n > 0 ) result += " and ";
   }

   if ( tens > 0 )
   {
      result += TENS[tens];
      if ( units > 0 ) result += "-";
   }

   if ( units > 0 )
   {
      result += UNITS[units];
   }

   return result;
}

//======================================================================

string expand( int n )
{
   string result;
   int millions, thousands, hundreds;

   millions = n / 1000000;
   n %= 1000000;
   thousands = n / 1000;
   n %= 1000;
   hundreds = n / 100;

   if ( millions > 0 )
   {
      result += expandThousand( millions ) + " million";
      if ( thousands > 0 || n > 0 ) result += ( thousands + hundreds > 0 ? ", " : " and " );
   }

   if ( thousands > 0 )
   {
      result += expandThousand( thousands ) + " thousand";
      if ( n > 0 ) result += ( hundreds > 0 ? ", " : " and " );
   }

   if ( n > 0 )
   {
      result += expandThousand( n );
   }

   return result;
}

//======================================================================

int main()
{
   int n;

   while ( true )
   {
      cout << "Enter a number between 1 and 999999999 (or 0 to finish): ";   cin >> n;
      if ( n <= 0 ) break;
      cout << expand( n ) << '\n';
   }
}

//====================================================================== 


Enter a number between 1 and 999999999 (or 0 to finish): 29
twenty-nine
Enter a number between 1 and 999999999 (or 0 to finish): 101
one hundred and one
Enter a number between 1 and 999999999 (or 0 to finish): 2019
two thousand and nineteen
Enter a number between 1 and 999999999 (or 0 to finish): 1999
one thousand, nine hundred and ninety-nine
Enter a number between 1 and 999999999 (or 0 to finish): 999999999
nine hundred and ninety-nine million, nine hundred and ninety-nine thousand, nine hundred and ninety-nine
Enter a number between 1 and 999999999 (or 0 to finish): 0
Topic archived. No new replies allowed.
Pages: 12