Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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
Smile | :)
Posted 8-Mar-11 11:15am
Rate this: bad
good
Please Sign up or sign in to vote.

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. Smile | :)
  Permalink  
Comments
William Winner at 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: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

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).
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

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[^].
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

thanks to all who replied to my questions all of your answers really helps . Smile | :)
  Permalink  

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

  Print Answers RSS
0 Afzaal Ahmad Zeeshan 256
1 OriginalGriff 251
2 BillWoodruff 240
3 Maciej Los 230
4 Sergey Alexandrovich Kryukov 195
0 OriginalGriff 6,419
1 Sergey Alexandrovich Kryukov 6,008
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,070


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 9 Mar 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100