Using a long double, it seems to cut out a decimal when it increases another digit. The first scan won't work properly, as it only shows 3 decimals, but should have 4, which doesn't concern me, but i'd like it to always have three.

Price: 5.99
Scanned Price (Over and Over) : 000J90120071000002291
Of course it will have to work with different Numbers.

#include <iostream>   
 #include <sstream>     
 #include <string>            

using namespace std;
int main() { 
    long double t, p;    
    string number21;
    cout << "Please enter the price."; 
    cout << endl;   
    cin>>p;   
    t = 0;   
    cout << "Please scan the the weight.";  
    cout << endl;
    cout << "Scan 1";
    cout << endl;
    cin >> number21;
    string number7 = number21.substr (14);
    long double w = atoi (number7.c_str ()); 
    t = w * p * 0.01 + t; 
    cout <<"Total: $"<<t;
    cout << endl;
    cout << endl;
    int i = 1; while (i++ < 1000) 
    { 
        cout << "Scan " <<i; 
        cout << endl; 
        cin >> number21; 
        string number7 = number21.substr (14); 
        long double w = atoi (number7.c_str ()); 
        t = w * p * 0.01 + t;  
        cout <<" Total: $"<<t; 
        cout << endl; 
        cout << endl;
    };
    cin.get();
    cin.get();
}

use strtod instead of atoi...

Now I'm just confused, I read about Strtod, so i changed atoi to Strtod, and i changed w from long double to double, and it's not working

Code:

#include <iostream>   
 #include <sstream>     
 #include <string>            

using namespace std;
int main() { 
    long double t, p;    
    string number21;
    cout << "Please enter the price."; 
    cout << endl;   
    cin>>p;   
    t = 0;   
    cout << "Please scan the the weight.";  
    cout << endl;
    cout << "Scan 1";
    cout << endl;
    cin >> number21;
    string number7 = number21.substr (14);
    double w = strtod (number7.c_str ()); 
    t = w * p * 0.01 + t; 
    cout <<"Total: $"<<t;
    cout << endl;
    cout << endl;
    int i = 1; while (i++ < 1000) 
    { 
        cout << "Scan " <<i; 
        cout << endl; 
        cin >> number21; 
        string number7 = number21.substr (14); 
        double w = strtod (number7.c_str ()); 
        t = w * p * 0.01 + t;  
        cout <<" Total: $"<<t; 
        cout << endl; 
        cout << endl;
    };
    cin.get();
    cin.get();
}

Here's the error message i get:

too few arguments to function `double strtod(const char*, char**)'


Probably something really easy i missed.

you need to tell cout how many decimals you want it to print: example

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    double x = 12345.7890;

    cout << fixed << setprecision(2) << x << "\n";
    cout << fixed << setprecision(3) << x << "\n";
    cout << fixed << setprecision(4) << x << "\n";
}

output

12345.79
12345.789
12345.7890
Press any key to continue . . .

Well that's certainly quite a bit more simple thanks for the reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.