Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
I'm writing a program that inputs the amount of money and outputs it preceded by dollar sign and divided by commas into groups of three digits
I'll enter the money as string and call function called mstold()and returns an equivalent number as long double so my problem is how can I get the a number long double equivalent to string and this is my code : :)
C#
long double mstold(string money)
{
    long double amnt;
    for(int i=0;i<money.length();i++)
    {
        amnt=money[i]*10+1;
    }
    return amnt;
}
int _tmain(int argc, _TCHAR* argv[])
{
    string money;
    cout<<"Enter the amount of money you want to convert to long double";
    cin>>money;
    cout<<"\n\namount in long double :  "<<mstold(money)<<endl;
return 0;
}

Thank You .

[update]
Thanks a lot to OriginalGriff and Alain Rist for repling to my question your answers really help.
[/update]
Posted
Updated 23-Feb-11 21:23pm
v2
Comments
Richard MacCutchan 23-Feb-11 13:20pm    
Do not ever use double or float for monetary values unless you are happy to get incorrect results in your calculations.
Sergey Alexandrovich Kryukov 23-Feb-11 14:19pm    
Richard, I strongly disagree. Right approach is not using integer values until the final result. This is very known effect of accumulation of rounding errors in some situations.
--SA
CPallini 23-Feb-11 15:25pm    
?
Sergey Alexandrovich Kryukov 23-Feb-11 19:29pm    
Yes, yes. What, you look at money from (some) accountant's stand point? If you looked from the economist stand point, you would understand that money is floating point by its nature. Surprise?
--SA
CPallini 24-Feb-11 3:24am    
I don't think so. :-)

You do realise that a string is a sequence of characters, don't you?
And that a string of characters such as "123" needs to be handled properly in order to convert it to a number?
Your code
C#
for(int i=0;i<money.length();i++)
{
    amnt=money[i]*10+1;
}
Looks at each character in the string: '1' then '2' then '3'. But these are not the same as the numbers 1, 2, and 3: they are characters. What that means is that they have a character value which is translated to a display device as a visible digit, not the numeric value that you see. Here[^] is a table showing a system of character values against display values. If you try to treat them as numbers they way you are doing, you will get huge errors in your numbers.

Instead, use the built in functions which convert strings to numbers: atoi, atol, atod etc.
amnt = atod(money);
instead of your for loop. (There are other ways, but I'll leave them for your tutor!)
 
Share this answer
 
Hi,
The Standard C++ Library provides the necessary toolset, not very sexy, but works:
C++
#include <locale>     // for locale, money_get, use_facet
#include <sstream>    // for istringstream, string
#include <iterator>   // for istreambuf_iterator
long double mstold(const std::string& amount)
{
    using std::ios;
    using std::use_facet;
    using std::money_get;
    using std::istreambuf_iterator;

    const std::locale loc("us");
    std::istringstream iss(amount);
    iss.imbue (loc);
    ios::iostate state = ios::goodbit;
    long double value = {0};

    use_facet<money_get<char> >(loc).get(
        iss, istreambuf_iterator<char>(), false, iss, state, value);

    if ((state & ios::failbit) == ios::failbit)
    {
        // Handle error
    }

    return value / 100;
}

[Edited for code style and error handling]
cheers,
AR
 
Share this answer
 
v3
Comments
goorley 24-Feb-11 3:18am    
I like this solution a lot.
Alain Rist 24-Feb-11 4:07am    
Thanks, I will try to make it look nicer :)
Alain Rist 24-Feb-11 5:08am    
Code should be more appealing now :)

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