12,073,837 members (67,401 online)
Rate this:
See more:
I want to convert the number from long double to string . I will enter a number in long double and will return the same number back in string this is my code and I'm getting an error due to the array . I even don't know if my code is right or wrong
```string lditoms(long double money[8])
{
string amnt;
for(int i=0;i<8;i++)
{
amnt=(amnt[i]*10)+money[i]-0;
}
return amnt;

}
int main()
{
long double money[8];
cout<<"enter value not more than 8 digits";
for(int i=0;i<8;i++)
{
cin>>money[i];

}
cout<<lditoms(money);

}
```
thank you
Posted 8-Mar-11 11:15am

Rate this:

## Solution 1

You have several problems going on here.

1. You say "I want to convert the number from long double to string." OK, but the function `lditoms` takes an array of ld's as input, but only has one output. I'm guessing you want to call `lditoms` once for each ld, and thus get 1 string for each ld.

2. Assuming (1) is correct, the input parameter of function `lditoms` should be written as `long double money`; there's no need for an array.

3. Inside `lditoms`, there's no need for a `for` loop, since now you only have one input parameter, not an array.

4. In `main()`, stick `lditoms` in the `for` loop; input an ld, call `lditoms`. And there's no need to make `money` an array.

I'm not at all sure this is what you really want, but it conforms to what you said you wanted.
William Winner 8-Mar-11 18:21pm

Did you even look at his code? He's storing a single digit inside each position in the array.

So, if the user typed
75639234, it would be an array with 8 values equaling the individual components. Then, he takes that array and tries to convert it to a string equivalent by looping through the numbers. His function logic is correct. he just doesn't understand that a char is actually an ASCII conversion of an int.
Rate this:

## Solution 5

Hi,

I will enter a number in long double and will return the same number back in string is not enough to specify your function, as you will have to round to some number of digits, or use scientific notation for very small or very large numbers.

So your question is not at all trivial.

You should use the built-in facilities of the Standard C++ Library, for instance this function converts a `float`, or `double`, or `long double` to a `std::string`. The output is rounded to `decimal_digits` digits:
```#include <iomanip>
#include <sstream>
template <class Val>
std::string ToString(Val val, size_t decimal_digits = 2)
{
std::ostringstream oss;
oss << std::fixed << std::setprecision(decimal_digits) << val << std::ends;
return oss.str();
}```
You may test it with:
```#include <iostream>
int main()
{
std::cout << ToString(123.456) << std::endl; // "123.46"
std::cout << ToString(12.3456) << std::endl; // "12.35"
std::cout << ToString(12.3456e10) << std::endl; // "123456000000.00"
std::cout << ToString(0.0123456) << std::endl; // "0.01"
}```
cheer,
AR
Rate this:

## Solution 3

First of all, I have to ask...why in the world are you using a `long double` to store values between 0 and 9? int would have sufficed!

Secondly, you have to understand what `amnt=(amnt[i]*10)+money[i]` is actually doing in the code.

A string is really just an array of char's, so you have to start there. Understand that a char representation of an integer uses an ascii table. So, if money[0] = 4, then a converting that to a char will give you a char of an EOT. But, money[0] + 48 converted to a char gives a char of 4 because the ascii value for 0 is 48.

The other thing to realize is that a string plus a string just concatenates is. So, if amnt = "45" and you add "8", it will become "458", not "57".

So, the way that you've done it, you would really want

amnt = amnt + (money[i] + 48)

But, what if the person only wants to enter 45 as their number? With your solution, they would have to enter 00000045 to get the program to run.

Hans is correct in that you should take a single integer as an input.

Then, you could modify your code to look like:

```string convertInt(int number)
{
if (number == 0)
return "0";
string temp="";
string returnvalue="";
while (number>0)
{
temp+=number%10+48;
number/=10;
}
for (int i=0;i<temp.length();i++)
returnvalue+=temp[temp.length()-i-1];
return returnvalue;
}```

or you could just use a stringstream (though if this is for a class check with your professor if you can use objects you haven't learned yet).
Rate this:

## Solution 2

```string lditoms(long double money[8])
{
string amnt;
for(int i=0; i < 8; i++)
{
amnt=(amnt[i]*10)+money[i]-0;
}
return amnt;

}
```
You have defined `amnt` as a string in this function, but then tried to access an array element with the same name; I am not sure exactly what you mean by that statement. You are also trying to set a string variable to the result of some mathematical expression, which will not work. You need to use one of the print formatting routines to convert a number to a string, as described here[^].
Rate this:

## Solution 4

There are some severe fundamental problems in your code snippet. I'm gonna try to explain the question by ignoring them.

But first, you may want to know that there is no long double (80 bit) support in Win32 and Visual Studio (in case you use) although it's base data type of 80x87. long double maps to double (64 bit) in Win32 and VS.

There are lots of way to convert a double to a string.
You can use "%f" format specifier in printf family functions.
You can use "<<" operator with streams like below
```double dblVar = 10.0;
cout << dblVar << endl;```
You can use _ecvt(), _fcvt(), _gcvt() (stdlib.h) functions. All take double as parameter. If you are working on a different platform, there may be long double versions like _ecvtl(), _fcvtl(), _gcvtl() in your compiler.
Rate this:

## Solution 6

thanks to all who replied to my questions all of your answers really helps .

Top Experts
Last 24hrsThis month
 OriginalGriff 605 Dave Kreskowiak 305 CPallini 295 ProgramFOX 230 CHill60 160
 Dave Kreskowiak 2,711 OriginalGriff 2,354 Richard MacCutchan 1,695 CPallini 1,507 CHill60 1,379