Click here to Skip to main content
15,886,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C++
#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

in addition to George's comments

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


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

C#
cbt=cost*quan;
               tc=cbt+tax;
 
Share this answer
 
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.
 
Share this answer
 
I suppose you mean that you get a compilation error.

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

and also the declaration of the constructor
C++
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.)
 
Share this answer
 
v3

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