Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
( num > 10 && num < 20 ) ? (cout << ten[num-10]) :( cout << tens[num/10-1] && ((num%10)?one[num%10]:1));


it has this error :

1>c:\users\public\documents\c++ data\help num2word\help num2word\help num2word.cpp(47): error C2446: ':' : no conversion from 'int' to 'char *'
Posted
Comments
Sandeep Mewara 27-Mar-11 2:48am    
Rest all looks ok. So, Error lies here: ((num%10)?one[num%10]:1))

Check again.

1 solution

((num%10)?one[num%10]:1)
I would assume that the "one" array returns a char pointer?
It is complaining because "one[num%10]" and "1" are not the same type...


Just like yesterday, I won't give you the code - not in a form you can use, anyway :laugh:
I have to leave you something to do!

So: Here is what I would do (but remember that you will need to re-work this into C++, this is psuedo code in C# style syntax and won't compile)

1) Set up three arrays of strings:
C#
string[] digitsWords = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
string[] teensWords = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
string[] tensWords = { "", "", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" };

These cover the three case we talked about.
2) Set up a function to use them. It takes an integer and a buffer, and returns a char pointer. It will put the words into the buffer, so make it 100 characters or so it won't run off the end! Call it NumberToWords:
char* NumberToWords(int value, char* buffer)

3) If the function, check the value is between 0 and 99 - if it isn't, put an error message in the buffer!
4) Break the value into the tens part (value / 10) and the digits part (value % 10)
5)
C#
if (value <= 9)
    {
    copy digitsWords[value] to buffer
    }
else if (value <= 19)
    {
    copy teensWords[digits] to buffer
    }
else
    {
    copy tensWords[tens] to buffer
    if (digit != 0)
        {
        append "-" to buffer
        append digitsWords[digits] to buffer
        }
    }

6) return buffer.

That should be pretty much what you are doing already!
The reason I would put this in a function is twofold: It is easier to read, and it is easier to test - you don't have to type in each number, you can set up a loop which tests all the numbers from -1 to 100 and makes sure you get the right result for each!
 
Share this answer
 
v2
Comments
yay hello 27-Mar-11 2:51am    
how can i correct it?
OriginalGriff 27-Mar-11 2:56am    
You could change the "1" to a char pointer?
Perhaps return "one[0]"?
But then, you could get rid of the conditional altogether, and just use "one[num % 10]" instead...Just a thought.

I take it this is your "convert a number to word" program that we talked about yesterday? How's it going?
yay hello 27-Mar-11 3:21am    
it is not good :(
OriginalGriff 27-Mar-11 3:23am    
:laugh: I have days like that! What's the problem?
Niklas L 27-Mar-11 8:46am    
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900