Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

input double but returning a character/string

Hi everyone,
I have tried to input a double number from the main and return a character/string using a function but i have not been able to do that as yet. can anyone help me on this?
Thank you
[211 byte] By [neo22] at [2007-11-11 8:38:21]
# 1 Re: input double but returning a character/string
You need to convert the double to a string. The precise implementation details depend on yoru design. You can use stringstream objects to automate the conversion. Otherwise, use sprintf() to convert double to a char array buffer.
Danny at 2007-11-11 21:01:06 >
# 2 Re: input double but returning a character/string
I am familiar with casting in java but not in C++ ... is it possible to show me a code to convert? i mean if it is some what done like it is done in java.
neo22 at 2007-11-11 21:02:17 >
# 3 Re: input double but returning a character/string
you cant cast it, strings are arrays of chars in c/c++.

so either study sprintf as danny sais or create a function to convert it.

Depending on what compiler and libraries you use you can look into itoa or IntToStr.

If you wish to create your own function to convert it, I can give you a hint, to make a single digit a char you can do this: char DigitAsChar = (char)MySingleDigit+'0';
MySingleDigit beeing a int.

Then it will add the value of the char '0' to the value of the digit so if the digit = 5 it will be '5' (the char) instaid and the it cast's to a char so it will accept setting a char to the number representation of the character.

I'm sorry if I over- or under explain.

GL Thomas.
ungamed at 2007-11-11 21:03:11 >
# 4 Re: input double but returning a character/string
ahhh ok thanks for letting me know about casting.

i kept trying and before i checked for messages here i made the solution :)

it was very simple...since i am new i did not know the proper syntax for coding a return function.

what i have done is i have made a string function. I am sending an integer value and returning a string according to the if statement.
neo22 at 2007-11-11 21:04:10 >