Cleaning my code

Pages: 12
Hello everyone! I'm new to this forum as well as c++.

I'm creating a program that takes an integer input by the user and outputs the equivalent in words (i.e. input = 201, output = two hundred one)

So far the program works up to an input of 999. But I feel that it's becoming unorganized and inefficient. I was hoping that I could have some advice about a more efficient way to code this program (any type of advice would be appreciated). Otherwise, thank you for your time.

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

using namespace std;

string ones[10] =
{
    "zero ",       "one ",       "two ",       "three ",        "four ",
    "five ",       "six ",       "seven ",     "eight ",        "nine "
};

string teens[10] =
{
    "ten",         "eleven",     "twelve",     "thirteen",      "fourteen",
    "fifteen",     "sixteen",    "seventeen",  "eighteen",      "nineteen"
};

string tens[8] =
{
    "twenty ",     "thirty ",    "fourty ",    "fifty ",        "sixty ",
    "seventy ",    "eighty ",    "ninety "
};

string magnitude[3] =
{
    "hundred ", "thousand"
};

int main()
{
    while ( true )
    {
        int number;
        cout << "Enter number: ";
        cin >> number;

        if ( number < 10 )
        {
            cout << ones[number];
            cout << endl;
        }
        else if ( number > 9 && number < 20 )
        {
            cout << teens[number % 10];
            cout << endl;
        }
        else if ( number > 20 && number < 100)
        {
            cout << tens[(number / 10) - 2];
            if ( (number % 10) != 0 )
            {
                cout << ones[number % 10];
            }
            cout << endl;
        }
        else if ( number > 99 && number < 1000 )
        {
            int reverseNumber[3];

            for ( int i = 0; i < 3; i++ )
            {
                reverseNumber[i] = (number % 10);
                number = (number / 10);
            }
            cout << ones[reverseNumber[2]];
            cout << magnitude[0];
            if ( reverseNumber[1] != 0 )
            {
                cout << tens[reverseNumber[1] - 2];
            }
            if ( reverseNumber[0] != 0 )
            {
                cout << ones[reverseNumber[0]];
            }
            cout << endl;
        }
        else
        {
            cout << "Derp" << endl;
        }
    }
}
The spaces in the arrays above are kinda "overspaced".

I'd recommend storing the arrays in a struct to make it look more organized.
closed account (Dy7SLyTq)
or in a two dimensional array
Okay. I'm just now coming up on the concept of structs. I'll have to apply what I learn to this.

Thanks for the advice.

Ryan
Last edited on
After that be sure to learn about classes. They're structs they just can have functions basically.
closed account (Dy7SLyTq)
well they are actually much more than that. they are the core feature that diffrentiate c from c++
You can break the number into pieces:
1
2
3
4
5
6
int number = 123456789;

int millions = number / 1000000;

// millions equals the string "one hundred twenty three"
// which only needs " million" tacked to the end 


Don't listen to Fredbill et. al. they're wasting your time.

The issue is how you are thinking about the number. Which is what LowestOne is talking about.

Notice that the only difference between

123 ("one hundred twenty three") and 123,000 ("one hundred twenty three thousand") is the word "thousand" at the end.

This is true of any power of 103.


What you have so far
You've got your basic formula for every group of three digits on lines 37 through 55. Put that into a function.

Now all you have to do is break your number up by groups of three (powers of 1000), and call the function for each group. Remember, you'll have to work backwards, because you want to write the millions before the thousands before the zeros.

123,456,789
one hundred twenty three million
four hundred fifty six thousand
seven hundred eighty nine

2,000,013
two thousand

thirteen


[edit] Working backwards really means that you have to grab the higher groups of three first.

1
2
3
4
5
6
7
8
9
10
11
12
int power_index = 1;
int power_value = 1000;
while ((number / power_value) != 0)
  {
  power_value *= 1000;
  power_index += 1;
  }
power_value /= 1000;
power_index -= 1;

// now power_value is one of 0, 1000, 1000000, 1000000000, ...
// and power_index is one of 0, 1, 2, 3, ... 


Now you can use power_value to divide number for the first three digits to pass to your function. Use power_index as an index into an array of the names to tack onto the end ("thousand", "million", etc).

Don't forget to remove those three digits from number before the next time through the loop.

Hope this helps.
Last edited on
Here is my code. I dont know if it can help:

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
#include <iostream>
#include <string>
using namespace std;

void   birler(long long x){
   if(x==0){cout<<"zero";}
   if(x==1){cout<<"one";}
   if(x==2){cout<<"two";}
   if(x==3){cout<<"three";}
   if(x==4){cout<<"four";}
   if(x==5){cout<<"five";} 
   if(x==6){cout<<"six";}
   if(x==7){cout<<"seven";}
   if(x==8){cout<<"eight";}
   if(x==9){cout<<"nine";}   
   }
void   onlar(long long x) {
   if(x<10) {birler(x);}
   if(x==10){cout<<"ten";}
   if(x==11){cout<<"eleven";}
   if(x==12){cout<<"twelve";}
   if(x==13){cout<<"thirteen";}
   if(x==14){cout<<"fourteen";} 
   if(x==15){cout<<"fifteen";}
   if(x==16){cout<<"sixteen";}
   if(x==17){cout<<"seventeen";}
   if(x==18){cout<<"eighteen";}   
   if(x==19){cout<<"nineteen";}
   if(x==20){cout<<"twenty";}
   if(x>20&&x<30) {cout<<"twenty-"; x=x-20; birler(x);}
   if(x==30){cout<<"thirty";}
   if(x>30&&x<40) {cout<<"thirty-"; x=x-30; birler(x);}       
   if(x==40){cout<<"fourty";}
   if(x>40&&x<50) {cout<<"fourty-"; x=x-40; birler(x);}         
   if(x==50){cout<<"fifty";}
   if(x>50&&x<60) {cout<<"fifty-"; x=x-50; birler(x);}         
   if(x==60){cout<<"sixty";}
   if(x>60&&x<70) {cout<<"sixty-"; x=x-60; birler(x);}         
   if(x==70){cout<<"seventy";}
   if(x>70&&x<80) {cout<<"seventy-"; x=x-70; birler(x);}
   if(x==80){cout<<"eighty";}
   if(x>80&&x<90) {cout<<"eighty-"; x=x-90; birler(x);}         
   if(x==90){cout<<"ninety";}
   if(x>90&&x<100) {cout<<"ninety-"; x=x-90; birler(x);}            
            }
            
void   yuzler(long long x){
   if(x<100) {onlar(x);}
   if(x==100)         {cout<<"one hundret ";}
   if(x>100&&x<200)   {cout<<"one hundret "; x=x-100; onlar(x);}
   if(x==200)         {cout<<"two hundret ";}
   if(x>200&&x<300)   {cout<<"two hundret "; x=x-200; onlar(x);}          
   if(x==300)         {cout<<"three hundret ";}
   if(x>300&&x<400)   {cout<<"three hundret "; x=x-300; onlar(x);}          
   if(x==400)         {cout<<"four hundret ";}
   if(x>400&&x<500)   {cout<<"four hundret "; x=x-400; onlar(x);}          
   if(x==500)         {cout<<"five hundret ";}
   if(x>500&&x<600)   {cout<<"five hundret "; x=x-500; onlar(x);}          
   if(x==600)         {cout<<"six hundret ";}
   if(x>600&&x<700)   {cout<<"six hundret "; x=x-600; onlar(x);}          
   if(x==700)         {cout<<"seven hundret ";}
   if(x>700&&x<800)   {cout<<"seven hundret "; x=x-700; onlar(x);}          
   if(x==800)         {cout<<"eight hundret ";}
   if(x>800&&x<900)   {cout<<"eight hundret "; x=x-800; onlar(x);}
   if(x==900)         {cout<<"nine hundret ";}
   if(x>900&&x<1000)  {cout<<"nine hundret "; x=x-900; onlar(x);}
   }
   
void   binler(long long x){
   if(x<1000){yuzler(x);}
   if(x==1000)         {cout<<"one thousand ";}
   if(x>1000&&x<2000)  {cout<<"one thousand "; x=x-1000; yuzler(x);}
   if(x==2000)         {cout<<"two thousand ";}
   if(x>2000&&x<3000)  {cout<<"two thousand "; x=x-2000; yuzler(x);}          
   if(x==3000)         {cout<<"three thousand ";}
   if(x>3000&&x<4000)  {cout<<"three thousand "; x=x-3000; yuzler(x);}          
   if(x==4000)         {cout<<"four thousand ";}
   if(x>4000&&x<5000)  {cout<<"four thousand "; x=x-4000; yuzler(x);}          
   if(x==5000)         {cout<<"five thousand ";}
   if(x>5000&&x<6000)  {cout<<"five thousand "; x=x-5000; yuzler(x);}          
   if(x==6000)         {cout<<"six thousand ";}
   if(x>6000&&x<7000)  {cout<<"six thousand "; x=x-6000; yuzler(x);}          
   if(x==7000)         {cout<<"seven thousand ";}
   if(x>7000&&x<8000)  {cout<<"seven thousand "; x=x-7000; yuzler(x);}          
   if(x==8000)         {cout<<"eight thousand ";}
   if(x>8000&&x<9000)  {cout<<"eight thousand "; x=x-8000; yuzler(x);}
   if(x==9000)         {cout<<"nine thousand ";}
   if(x>9000&&x<10000) {cout<<"nine thousand "; x=x-9000; yuzler(x);}
     
   }
   
void   onbinler(long long x){
   if(x<10000) {binler(x);}
   if(x==10000)         {cout<<"ten thousand ";}
    if(x>10000&&x<11000) {cout<<"ten thousand "; x=x-10000; yuzler(x);} 
   if(x==11000)         {cout<<"eleven thousand ";}
    if(x>11000&&x<12000) {cout<<"eleven thousand "; x=x-11000; yuzler(x);}
   if(x==12000)         {cout<<"twelve thousand ";}
    if(x>12000&&x<13000) {cout<<"twelve thousand "; x=x-12000; yuzler(x);}
   if(x==13000)         {cout<<"thirteen thousand ";}
    if(x>13000&&x<14000) {cout<<"thirteen thousand "; x=x-13000; yuzler(x);}
   if(x==14000)         {cout<<"fourteen thousand ";}
    if(x>14000&&x<15000) {cout<<"fourteen thousand "; x=x-14000; yuzler(x);}
   if(x==15000)         {cout<<"fifteen thousand ";}
    if(x>15000&&x<16000) {cout<<"fifteen thousand "; x=x-15000; yuzler(x);}
   if(x==16000)         {cout<<"sixteen thousand ";}
    if(x>16000&&x<17000) {cout<<"sixteen thousand "; x=x-16000; yuzler(x);}
   if(x==17000)         {cout<<"seventeen thousand ";}
    if(x>17000&&x<18000) {cout<<"seventeen thousand "; x=x-17000; yuzler(x);}
   if(x==18000)         {cout<<"eighteen thousand ";}
    if(x>18000&&x<19000) {cout<<"eighteen thousand "; x=x-18000; yuzler(x);}
   if(x==19000)         {cout<<"nineteen thousand ";}
    if(x>19000&&x<20000) {cout<<"nineteen thousand "; x=x-19000; yuzler(x);}
   if(x==20000)         {cout<<"twenty thousand ";} 
    if(x>20000&&x<30000) {cout<<"twenty-"; x=x-20000; binler(x);}        
   if(x==30000)         {cout<<"thirty thousand ";}
    if(x>30000&&x<40000) {cout<<"thirty-"; x=x-30000; binler(x);}        
   if(x==40000)         {cout<<"fourty thousand ";}
    if(x>40000&&x<50000) {cout<<"fourty-"; x=x-40000; binler(x);}        
   if(x==50000)         {cout<<"fifty thousand ";}
    if(x>50000&&x<60000) {cout<<"fifty-"; x=x-50000; binler(x);}        
   if(x==60000)         {cout<<"sixty thousand ";}
    if(x>60000&&x<70000) {cout<<"sixty-"; x=x-60000; binler(x);}        
   if(x==70000)         {cout<<"seventy thousand ";}
    if(x>70000&&x<80000) {cout<<"seventy-"; x=x-70000; binler(x);}                
   if(x==80000)         {cout<<"eighty thousand ";}
    if(x>80000&&x<90000) {cout<<"eighty-"; x=x-80000; binler(x);}        
   if(x==90000)         {cout<<"ninety thousand ";}
    if(x>90000&&x<100000){cout<<"ninety-"; x=x-90000; binler(x);}         
            }         
int main(){
    long long x;
    int y;
    while(1)
    {
    cout<<"Ingilizcesini ogrenmek istediginiz sayiyi giriniz.\n";
    cin>>x;
    if(x<10){birler(x);} 
    if(x>9&&x<100){onlar(x);}
    if(x>99&&x<1000){yuzler(x);}
    if(x>999&&x<10000){binler(x);}
    if(x>9999&&x<100000){onbinler(x);}
    if(x>99999&&x<1000000){yuzbinler(x);}
    if(x>999999&&x<10000000){milyonlar(x);}
    if(x>9999999&&x<100000000){onmilyonlar(x);}
    if(x>99999999&&x<1000000000){yuzmilyonlar(x);}
    if(x>999999999){cout<<"Cok buyuk bir sayi girdiniz.\nTekrar deneyin."; continue;}
    
    do{
    cout<<"\nLutfen seciminizi yapin.\n";
    cout<<"1.Yeni sayi gir.\n2.Cikis";
    cin>>y;
    } while(y!=1&&y!=2);
    if(y==1){continue;}
    if(y==2){break;}
    
    }   
    
    }
Two small observations:
1. I can input -23. Check that your integer is positive
2. Take a look at your if ... else if statements for duplicate checks. For example, if your number is 23, you test on line 37 if it is less then 10. It is not, so you go to line 42, where the first thing you do is to check if it's greater than 9. That is a useless check, since you already know that it is 10 or more. All your conditions have similar problems
@senhor
It works, but it makes no effort to use simpler structures that make it more flexible. What happens if I enter the number 123456789019? It can't handle it.

Redeyery's code has the good sense to use a table lookup for stuff. It is inherently more compact and more flexible right at the start.
Thank you very much!

Your posts should keep me busy for awhile. I'm going to turn the blocks that convert three digit chunks(to English) into a function to be called per each power of 1,000. Later I will simplify the function itself making it less redundant.

Duoas
I was experimenting with your code in order to understand it better. When raising the variables to sizes beyond millions the program crashes or outputs something incorrectly. I attempted to use other variables but they did not seem to help.

[edit]
unsigned long long will do the trick! c:
Last edited on
My code?




Are you talking about this?
http://www.cplusplus.com/forum/lounge/74394/#msg402102
'Cause I don't actually use any integers in my algorithm...
@Duoas it is not all of my code
@op and @senhor: why not using switch?
@Rechard3
Who are you to tell OP to go away?
Reported.

@senhor
Don't be offended. You already got bonus help about how to improve your code. No one here is perfect. I've had my code picked apart, and sometimes when I consider posting, I look at something someone else posted and click cancel in shame.
No i am not offended. It is About my english. It is not well and cause misunderstanding.

@Duoas Sorry my friend I did not read the number you wrote :D it is really out of range. But I cant understand your recommend /:
@Duoas
hey man, looks like you misunderstood me, first of all, i never mean to offend anyone, and i don't remember telling anyone to go away.
2- i don't know who is OP you're talking about.
3- actually you happened to be a member in one of the discussions i mentioned.
4- i gave an honest advice to senhor to search for the answer before asking the question, i -myself- saw like two or three posts that discussed this problem and reached a convenient solution, so why waste your time waiting for another one to solve your question, the answer already exists in this forum.
i can't see why you reported my comment !!!
anyway, i'm gonna be patient with you cause this looks like a big misunderstanding.
i hope you realize that my comment doesn't deserve this action you've taken, and if you do realize so, i will happily forget all about this.
PS: i do respect you Duoas, because you look like a great programmer.

EDIT: can someone please tell me who is OP, i can't see anyone with this name, if you guys see his contribution, maybe i'm being hacked right now.
Last edited on
Original Poster.
Duoas
I was referring to the code you posted on this specific topic. You're actual algorithm for this problem is quite impressive though. I'm definitely not at that level yet for I only introduced myself to c++ about two weeks ago. I'm still coping with the fundamentals. If you don't mind me asking, how long have you been programming?

[edit]
This thread seems to be over. If you would like to respond in a message that would be greatly appreciated. Otherwise, thank you.
Last edited on
Pages: 12