Where is this coming from?

I'm trying to write a program to convert a user's integer into English text (Jumping into C++ Chapter 7 task).

While testing out a piece of code I've already written I noticed that there are extra characters at the end of the output. I've tried looking through the code, but can't see where that is coming from. I've managed to narrow it down my hundredsCounter() function, but still have no idea where that extra bit is coming from.

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

using namespace std;

int zeroCounterFunction (int XXX);
int hundredsCounter (int hundreds);

int main (){
    int input;

    cout << "Please enter a number: " << endl;
    cin >> input;

    cout << "The number you entered is";
    cout << hundredsCounter (input) << ".";
}

int zeroCounterFunction (int XXX) {
    int i = 0;
    while (XXX / 1000 > 0) {
        XXX /= 1000;
        i++;
    }
    return i;
}

int hundredsCounter (int hundreds){
    int i = hundreds / 100;
    if (i == 1) {
        cout << " one hundred ";
    }
    else if (i == 2) {
        cout << " two hundred ";
    }
    else if (i == 3) {
        cout << " three hundred ";
    }
    else if (i == 4) {
        cout << " four hundred ";
    }
    else if (i == 5) {
        cout << " five hundred ";
    }
    else if (i == 6) {
        cout << " six hundred ";
    }
    else if (i == 7) {
        cout << " seven hundred ";
    }
    else if (i == 8) {
        cout << " eight hundred ";
    }
    else if (i == 9) {
        cout << " nine hundred ";
    }
    else {
        cout << " ";
    }
}


Just in case, console out put:
1
2
3
4
5
Please enter a number:
649
The number you entered is six hundred4683872.
Process returned 0 (0x0)   execution time : 2.758 s
Press any key to continue.


PS. I know that 'using namespace std;' in the header is bad practice.
Last edited on
it's the return from hundredsCounter.

This line

cout << hundredsCounter (input) << ".";

is calling the function hundredsCounter, which outputs the words, and then outputting the return from hundredsCounter.

Try

1
2
hundredsCounter (input);
cout << ".";


Also, your hundredsCounter function should really use a void return as it stands. Or you should fix it so it returns something.

(I don't know what compiler you're using, but Visual C++ doesn't build your code:

error C4716: 'hundredsCounter' : must return a value.

Fixing the return to void leads to this error on the cout line

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' ...

Which forces me to make the correction I suggested above.)

Andy
Last edited on
Ah, I get it.
hundredsCounter() has its own 'cout'. I should just call it normally outside any output I may have around it since it doesn't actually just return a value.
What a dumb mistake to make.

Hating myself now :(
Ok, so I finished the code and have another issue. When I input too big an integer (over 2 billion!) the programs ignores the condition for '2bil>input>-2bil' and proceeds to execute regardless with maximum possible integer.

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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int zeroCounter (int XXX);                      //function prototypes
int hundredsCounter (int hundreds);
int tensCounter (int tens);
int onesCounter (int ones);

int index;                                      // global variables
int hundreds;
int tens;
int ones;
int intermediate;

int main (){
    bool doRun = true;
    int input;

    cout << "Please enter a number: " << endl;  // user input
    cin >> input;
    if (input == 0) {                           // check if input 0
        cout << "The number you entered is zero.";
        doRun = false;
    }                                              // do-while start
    if (input > 2000000000 && input < -2000000000) {   // int limitations
        cout << "The number is too large to process.";
        doRun = false;
    }
    else if (doRun==true && input > 0 && input < 2000000000) {   // run if input valid
        cout << "You have entered '";
    }
    else if (input < 0 && input > -2000000000) { // if negative, flip
        cout << "You have entered ' minus";
        input = abs(input);
    }
    if (doRun==true) {
    index = zeroCounter(input);         // count billions, millions, thousands
    if (index == 3) {                           // output billions
        hundreds = input / pow (1000 , index);
        hundredsCounter (hundreds);
        tensCounter (tens);
        onesCounter(ones);
        cout << " billion";
        int dump = hundreds * pow (1000 , index);
        intermediate = input - dump;
        input = intermediate;
        index--;
    }
    if (index == 2) {
        hundreds = input / pow (1000 , index); // output millions
        hundredsCounter (hundreds);
        tensCounter (tens);
        onesCounter(ones);
        cout << " million";
        int dump = hundreds * pow (1000 , index);
        intermediate = input - dump;
        input = intermediate;
        index--;
    }
    if (index == 1) {
        hundreds = input / pow (1000 , index);      // output thousands
        hundredsCounter (hundreds);
        tensCounter (tens);
        onesCounter(ones);
        cout << " thousand";
        int dump = hundreds * pow (1000 , index);
        intermediate = input - dump;
        input = intermediate;
        index--;
    }
    if (index == 0) {
        hundreds = input / pow (1000 , index);      // output ones
        hundredsCounter (hundreds);
        tensCounter (tens);
        onesCounter(ones);
        cout << " ' .";
    }
  }
return 0;
}

int zeroCounter (int XXX) {                         // get a 1000 power index
    int i = 0;
    while (XXX / 1000 > 0) {
        XXX /= 1000;
        i++;
    }
    return i;
}

int onesCounter(int ones) {                         // output for ones
    if (ones == 1) {
        cout << " one";
    }
    else if (ones == 2) {
        cout << " two";
    }
    else if (ones == 3) {
        cout << " three";
    }
    else if (ones == 4) {
        cout << " four";
    }
    else if (ones == 5) {
        cout << " five";
    }
    else if (ones == 6) {
        cout << " six";
    }
    else if (ones == 7) {
        cout << " seven";
    }
    else if (ones == 8) {
        cout << " eight";
    }
    else if(ones == 9) {
        cout << " nine";
    }
    else {
        cout << "";
    }
    return 0;
}

int tensCounter (int tens) {                // outputs for tens, including up to 19
    int k;
    if (tens == 19){
        cout << " nineteen";
    }
    else if (tens == 18) {
        cout << " eighteen";
    }
    else if (tens == 17) {
        cout << " seventeen";
    }
    else if (tens == 16) {
        cout << " sixteen";
    }
    else if (tens == 15) {
        cout << " fifteen";
    }
    else if (tens == 14) {
        cout << " fourteen";
    }
    else if (tens == 13) {
        cout << " thirteen";
    }
    else if (tens == 12) {
        cout << " twelve";
    }
    else if (tens == 11) {
        cout << " eleven";
    }
    else if (tens == 10) {
        cout << " ten";
    }
    else if (tens == 9) {
        cout << " nine";
    }
    else if (tens == 8) {
        cout << " eight";
    }
    else if (tens == 7) {
        cout << " seven";
    }
    else if (tens == 6) {
        cout << " six";
    }
    else if (tens == 5) {
        cout << " five";
    }
    else if (tens == 4) {
        cout << " four";
    }
    else if (tens == 3) {
        cout << " three";
    }
    else if (tens == 2) {
        cout << " two";
    }
    else if (tens == 1) {
        cout << " one";
    }
    else {
        k = tens / 10;
    }
    if (k == 2) {                       // output for tens from 20 to 90
        cout << " twenty";
    }
    else if (k == 3) {
        cout << " thirty";
    }
    else if (k == 4) {
        cout << " fourty";
    }
    else if (k == 5) {
        cout << " fifty";
    }
    else if (k == 6) {
        cout << " sixty";
    }
    else if (k == 7) {
        cout << " seventy";
    }
    else if (k == 8) {
        cout << " eighty";
    }
    else if (k == 9) {
        cout << " ninety";
    }
    if (tens > 19){
        ones = tens - (k * 10);
       return ones;
    }
}


int hundredsCounter (int hundreds){   //output for hundreds
    int j = hundreds / 100;
    if (j == 1) {
        cout << " one hundred";
    }
    else if (j == 2) {
        cout << " two hundred";
    }
    else if (j == 3) {
        cout << " three hundred";
    }
    else if (j == 4) {
        cout << " four hundred";
    }
    else if (j == 5) {
        cout << " five hundred";
    }
    else if (j == 6) {
        cout << " six hundred";
    }
    else if (j == 7) {
        cout << " seven hundred";
    }
    else if (j == 8) {
        cout << " eight hundred";
    }
    else if (j == 9) {
        cout << " nine hundred";
    }
    else {
        cout << "";
    }
    tens = hundreds - (j*100);
    return tens;
}


dumped the whole lot of it, though I'm sure the functions themselves have to impact on the issue.
That probably has to with the fact that you're using an int. An int is usually 4 bytes, so the values it can take ranges from 0-2^31
closed account (Dy7SLyTq)
you can find the maximum with <limits>
use switch
That probably has to with the fact that you're using an int. An int is usually 4 bytes, so the values it can take ranges from 0-2^31
I know it is. Note that the code says if (input > 2000000000 && input < -2000000000), which is actually smaller than the maximum you can store as an int. However, when you enter anything over 2.2bil, int input stores 2147483648 (which is greater than the the limit defined by the above condition) and proceeds with the program ignoring the input limits in the if statement. Can't figure out why it does so.

you can find the maximum with <limits>
I know what int limit is, that's why I hardcoded the limits to +-2bil.

use switch
Really? The whole point of the exercise is to write that program using what you've learned so far ('switch' isn't on that list yet). I know I can shorten the code significantly by using 'switch' & arrays, but, again, that's not the point of the exercise.
Last edited on
This

if (input > 2000000000 && input < -2000000000)

is impossible.

How can a number be both greater than 2000000000 and less than -2000000000 at the same time?

Do you mean or?

if (input > 2000000000 || input < -2000000000)

Andy
oh, crap... got the hard part out, but missed the little thing... I should consider taking breaks when coding for a full day.

Thanks, Andy
Topic archived. No new replies allowed.