### 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?

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192`` ``````#include #include #include 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<>num; system("CLS"); cout<9999999) { cout<< "The number you enter is too high! Please enter the exact number: \n"; cout<>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<=100000) { ret= b/100000-1; right= c/10000; hundredth= b/100000; cout<=10000 && num<20000) { ret= (c+d)/1000; cout<=100000) { right= c/10000-1; left= d/1000; cout<=1000 && num<10000) { ret= d/1000; cout<=1000) { cout<< " thousand "; } if(num>=100) { ret= e/100; cout<=20) { left= f/10-1; right= g; cout<0) { ret= f+g; cout<
Hello Vinz24,

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:

 ``123456`` `````` else if(num>=100000) { right= c/10000-1; left= d/1000; cout<

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.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889`` ``````#include #include #include 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<>num; system("CLS"); cout<9999999) { cout<< "The number you enter is too high! Please enter the exact number: \n"; cout<>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<=100000) { hundredth= b/100000; cout<=10000 && num<20000) { ret= (c+d)/1000; cout<=100000) { left= c/10000-1; right= d/1000; cout<=1000 && num<10000) { ret= d/1000; cout<=1000) { cout<< " thousand "; } if(num>=100) { ret= e/100; cout<=20) { left= f/10-1; right= g; cout<0) { ret= f+g; cout<

@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?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102`` ``````#include #include #include 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 <> inum; system("CLS"); while(inum<-9999999 || inum >10000000) { cout << endl << "Error! Please enter a number within the range!: \n"<> 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<= 100000 && c== 0 && b>=100000) { res= b/100000-1; cout<= 100000 && c!= 0 && b>=100000) { res= b/100000-1; cout<= 20000 && d >= 1000 && (c+d)>=20000 && (c+d)<1000000) { left = c/10000-1; right = d/1000-1; cout<= 1000 && (c+d)>=10000) { res= (c+d)/1000-2; cout<=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<

 ``` 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:
 ``12`` ``````#include 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:
 ``12345`` ``````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.