asking for help for my code

i make a source code on converting numbers to words. it convert but all. when i try to input: 1000000, 100000, 10000, 1000, 100, i got an error. how to solve this? And how to make it display like negative one million?

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

int main()
{
    string ones[21]= {" ", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
    string tens[10]= {"ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};
    int num, ret, left, right, hundredth;
    int a,b,c,d,e,f,g;

    while(num!=0)
    {
        cout<<endl<< "Enter a number between -9999999 and 9999999, o to exit: \n"<<endl;
        cin>>num;
        system("CLS");
        cout<<endl;

        while(num<-9999999 || num>9999999)
        {
            cout<< "The number you enter is too high! Please enter the exact number: \n";
            cout<<endl;
            cin>>num;
            system("CLS");
        }

            a= (num%10000000)-(num%1000000);        //million
            b= (num%1000000)-(num%100000);          //hundred thousand
            c= (num%100000)-(num%10000);            //ten thousand
            d= (num%10000)-(num%1000);              //thousand
            e= (num%1000)-(num%100);                //hundred
            f= (num%100)-(num%10);                  //ten
            g= num%10;

            if(num<0)
            {
                cout<< "negative";
            }
            if(num>=1000000)
            {
                ret= a/1000000;
                cout<<ones[ret]<< " "<<" million ";
            }
            if(num>=100000)
            {
                ret= b/100000-1;
                right= c/10000;
                hundredth= b/100000;
                cout<<ones[hundredth]<<" hundred ";
            }
            if(num>=10000 && num<20000)
            {
                ret= (c+d)/1000;
                cout<<ones[ret]<< " ";
            }
            else if(num>=100000)
            {
                right= c/10000-1;
                left= d/1000;
                cout<<tens[right]<< " "<<ones[left]<<" ";
            }
            if(num>=1000 && num<10000)
            {
                ret= d/1000;
                cout<<ones[ret]<< " ";
            }
            if(num>=1000)
            {
                cout<< " thousand ";
            }
            if(num>=100)
            {
                ret= e/100;
                cout<<ones[ret]<< " "<< " hundred ";
            }
            if(num>=20)
            {
                left= f/10-1;
                right= g;
                cout<<tens[left]<<" "<<ones[right];
            }
            else if(num<20 || num>0)
            {
                ret= f+g;
                cout<<ones[ret];
            }
            cout<<endl;
    }
    return 0;
}
Hello Vinz24,

To start with you should be using the C++ header file "string" not the C header file "string.h".

Lines 10 and 11 you should initialize your variables. The easiest way is int num{}; which will set the variable to zero.

Your next problem is the outer while loop. This may seem to work, but the garbage value for an uninitialized "num" falls within the range for your inputted value. What you need to do is either change the while condition on line 13 or enter a value for "num" before you reach the while loop.

Also the while loop on line 13 is an endless loop because "num" never reaches zero.

I have not fully tested the program yet and I now see the need to ask for a number outside and inside the while loop. This will eliminate the endless loop.

Hope that helps,

Andy
Be careful you appear to be accessing your arrays out of bounds. Look closely at this snippet:

1
2
3
4
5
6
            else if(num>=100000)
            {
                right= c/10000-1;
                left= d/1000;
                cout<<tens[right]<< " "<<ones[left]<<" ";
            }


If num is equal to 100000 right will be equal to -1 and since you use right as the index to an array you have a buffer underflow error.

Also your code seems a bit convoluted and could probably be simplified.

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

int main()
{
    string ones[21]= {" ", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
    string tens[10]= {"ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};
    int num= 1, ret, left, right, hundredth;
    int a,b,c,d,e,f,g;

    while(num!=0)
    {
        cout<<endl<< "Enter a number between -9999999 and 9999999, o to exit: \n"<<endl;
        cin>>num;
        system("CLS");
        cout<<endl;

        while(num<-9999999 || num>9999999)
        {
            cout<< "The number you enter is too high! Please enter the exact number: \n";
            cout<<endl;
            cin>>num;
            system("CLS");
        }

            a= (num%10000000)-(num%1000000);        //million
            b= (num%1000000)-(num%100000);          //hundred thousand
            c= (num%100000)-(num%10000);            //ten thousand
            d= (num%10000)-(num%1000);              //thousand
            e= (num%1000)-(num%100);                //hundred
            f= (num%100)-(num%10);                  //ten
            g= num%10;

            if(num<0)
            {
                cout<< "negative ";
            }
            if(num>=1000000)
            {
                ret= a/1000000;
                cout<<ones[ret]<< " "<<" million ";
            }
            if(num>=100000)
            {
                hundredth= b/100000;
                cout<<ones[hundredth]<<" hundred ";
            }
            if(num>=10000 && num<20000)
            {
                ret= (c+d)/1000;
                cout<<ones[ret]<< " ";
            }
            else if(num>=100000)
            {
                left= c/10000-1;
                right= d/1000;
                cout<<tens[left]<< " "<<ones[right]<<" ";
            }
            if(num>=1000 && num<10000)
            {
                ret= d/1000;
                cout<<ones[ret]<< " ";
            }
            if(num>=1000)
            {
                cout<< " thousand ";
            }
            if(num>=100)
            {
                ret= e/100;
                cout<<ones[ret]<< " "<< " hundred ";
            }
            if(num>=20)
            {
                left= f/10-1;
                right= g;
                cout<<tens[left]<<" "<<ones[right];
            }
            else if(num<20 || num>0)
            {
                ret= f+g;
                cout<<ones[ret];
            }
            cout<<endl;
    }
    return 0;
}


@jlb thanks for reminding to that but the problem is it cannot convert numbers ends with 00 like 100, 1000, 10000... 1000000, the console will have an error, even i enter 1100100 and negative numbers.


one  million two hundred thirty four  thousand five  hundred sixty seven

Enter a number between -9999999 and 9999999, o to exit:
Last edited on
You still appear to have the same basic type of problems, you need to verify all of your math to insure you don't access your arrays out of bounds.

This is where running the program with your debugger will probably help, set a breakpoint early in main() single step through the program watching the variables as you step.


i have change the conditions of my source code and it can convert 0 to 1 million. But another problem is i cannot convert negative numbers. how to make my program convert negative numbers?

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

int main()
{
    string ones[20]={" One"," Two"," Three"," Four"," Five"," Six"," Seven"," Eight"," Nine", "Eleven"," Twelve"," Thirteen"," Fourteen"," Fifteen"," Sixteen"," Seventeen"," Eighteen"," Nineteen"};
    string tens[10]={" Ten"," Twenty"," Thirty"," Forty"," Fifty"," Sixty"," Seventy"," Eighty"," Ninety"};
    int inum=1,res, left, right;
    int a,b,c,d,e,f,g;

    while(inum!=0)
    {
        cout <<endl<< "Enter the number between -9999999 and 9999999, 0 to exit: \n"<<endl;
        cin >> inum;
        system("CLS");
        while(inum<-9999999 || inum >10000000)
        {
            cout << endl << "Error! Please enter a number within the range!: \n"<<endl;
            cin >> inum;
            system("CLS");
        }

        a = (inum%10000000) - (inum%1000000);
        b = (inum%1000000) - (inum%100000);
        c = (inum%100000) - (inum%10000);
        d = (inum%10000) - (inum%1000);
        e = (inum%1000) - (inum%100);
        f = (inum%100) - (inum%10);
        g = inum%10;

        if(inum<0)
        {
            cout<<"Negative ";
        }
        if(a >= 1000000)
        {
            res= a/1000000-1;
            cout<<ones[res]<< +" Million";
        }
        if(b >= 100000 && c== 0 && b>=100000)
        {
            res= b/100000-1;
            cout<<ones[res]<< +" Hundred"<< +" Thousand";
        }
        if(b >= 100000 && c!= 0 && b>=100000)
        {
            res= b/100000-1;
            cout<<ones[res]<< +" Hundred";
        }
        if(c >= 20000 && d >= 1000 && (c+d)>=20000 && (c+d)<1000000)
        {
            left = c/10000-1;
            right = d/1000-1;
            cout<<tens[left]<<" "<<ones[right]<< +" Thousand";
        }
        if(c <= 10000 && d >= 1000 && (c+d)>=10000)
        {
            res= (c+d)/1000-2;
            cout<<ones[res]<<" "<< " Thousand";
        }
        if(c>=10000 && d==0)
        {
            res = c/10000 -1;
            cout << tens[res] << " " << " Thousand";
        }
        if((c+d)%10000!=0 && (c+d)<10000 && (c+d)>=1000)
        {
            res = d/1000 -1;
            cout << ones[res] << " " << " Thousand";
        }
        if((c+d+e)%1000 != 0)
        {
            res = e/100 -1;
            cout << ones[res] << " " << " Hundred";
        }
        if(f == 0 && g != 0)
        {
            res = g-1;
            cout << ones[res];
        }
        if(f == 10 && g != 0)
        {
            res = f+g-2;
            cout << ones[res];
        }
        if((f+g)%10 == 0 && (f+g) != 0)
        {
            res = f/10 - 1;
            cout << tens[res];
        }
        if((f+g) > 20 && (f+g)%10 != 0)
        {
            res = f/10 - 1;
            cout << tens[res] << " " << ones[g-1];
        }
        cout<<endl;
    }
    return 0;
}


 One Million Two Hundred Thirty  Four Thousand Five  Hundred Sixty  Seven

Enter the number between -9999999 and 9999999, 0 to exit:


when I enter -1

-ninety

Enter the number between -9999999 and 9999999, 0 to exit:[
Last edited on
modulus is weird for negative numbers. Simple way is to take the absolute value before doing any math:
1
2
#include <cmath>
inum = std::abs(inum);


Do this before line 25, where you begin assigning your a-g.
Last edited on
Hello Vinz24,

I solved the problem by putting this:
1
2
3
4
5
if (num < 0)
{
	cout << "negative ";
	num = abs(num);  // <--- include header file "cmath".
}

above the lines for "a" - "g". This allows the word "negative" to be printed before converting the number to a positive and finishing the calculations.

Hope that helps,

Andy
Topic archived. No new replies allowed.