Click here to Skip to main content
14,667,169 members
Rate this:
Please Sign up or sign in to vote.
See more:
Im not quite sure where in my code is causing the problem that causes wrong calculations. When I run the program there is a warning of : C4305: 'argument' : truncation from 'double' to 'float'. There seems to be something wrong with Tax amount(ta) and Total cost(tc), Thanks.


Current Output:
Cost before Tax: $30.20
Tax Amount: $30.20
Total Cost: $-107374144.00
ground beef is ex-sponged
Press any key to continue . .


What it **should** be:
Your item name:ground beef
Cost before Tax: $30.20
Tax Amount: $2.64
Total Cost: $32.84
ground beef is ex-sponged
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<iomanip>
#include<string>

using namespace std;

class item
{
public:
    item(char* = " " ,float=0.0,int=0,float=0.0);
    ~item();
    void print();
    void calc(int);
private:
    char name[20];
    int quan;
    float cost, tp, cbt, tax, tc;
};
item::~item()
{
    cout << name << " is ex-sponged"<<endl;
    system("pause");
    }
item::item(char *w,float x, int y, float z)
{
    strcpy(name, w);
    cost = x;
    quan=y;
    tp = z;
    tax=cost*quan;
    tc=cbt+tax;
    cbt = cost*quan;
}
void item::print()
{
    cout << "Your item name:" << name << endl;
    cout << "Cost before Tax: $" << cbt << endl;
    cout << "Tax Amount: $" << tax << endl;
    cout << "Total Cost: $" << tc << endl;
}

void item::calc(int n)
{
    quan += n;
    cbt = cost*quan;
     tax = cbt*tp/100;
     tc = cbt + tax;
}

int main()
{
    item i("ground beef", 7.55, 4, 8.75);
    cout << setprecision(2) << showpoint << fixed;
    i.print();
}
Posted
Updated 29-Oct-14 23:31pm
v2

Rate this:
Please Sign up or sign in to vote.

Solution 2

in addition to George's comments

tc=cbt+tax;
        cbt = cost*quan;


how can tc = cbt + tax, when you havnt calculated cbt yet ? - so reverse those two lines so ..

cbt=cost*quan;
               tc=cbt+tax;
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

Without knowing what exactly is looking like is behaving badly and what's supposed to happen it's a bit hard to work out what's going wrong. However here's some suggestions to make things a bit easier to read:

- you don't need a fixed size character buffer and it's far easier to use std::string

- use member initialisers - it'll make your constructor less cluttered

- use descriptive names. x, y and z don't tell me as a reader what the bits and pieces are supposed to represent or do. I can't even guess what tc and tp are. item looks like a line_item (it has a quantity) and calc isn't calc it's add_items_to_line if I'm reading what it does properly.

- fix warnings. As you didn't say what was giving you a warning I can't provide a lot of guidance there - although I'd hazard a guess it's when an int is implicitly promoted to a double, used in a calculation and the result stashed back in a float.

Anyway, fix that lot and if the problem doesn't leap out you step through it all with a debugger and see what isn't being set the way you expect. Next time consider investigating test driven development which is a really great way to avoid calculation problems before they get too heavily ground into the carpet.
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

I suppose you mean that you get a compilation error.

Try to change your code in the main() function to this:
item i("ground beef", 7.55f, 4, 8.75f);

and also the declaration of the constructor
item(char* = " ", float = 0.0f, int = 0, float = 0.0f);

The f after a number is used to say that it is float.

(Spaces are not evil, in the right places they actually help to make the code readable.)
   
v3

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




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