Hey guys, question. I can't get the output to correctly display the cost of shipping based off of user input data. It's just displaying as zero. What am I doing wrong?
//This program formats a shipping label with a pseudo barcode for printing and placing on a package.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string name, streetAddress, city, state; //To hold user location input information.
int zipcode, weight, postage, number; //To hold the user number input information.
const int cost1=0.49, cost2=0.98, cost3=2.54, addCharge1=0.22, addCharge2=0.20; //To hold the given constants.
//Gather user input data.
cout<<"Please enter your name:";
getline(cin, name);
cout<<"Please enter your street address:";
getline(cin, streetAddress);
cout<<"Please enter your city:";
getline(cin, city);
cout<<"Please enter your state:";
getline(cin, state);
cout<<"Please enter your zipcode:";
cin>>zipcode;
cout<<"Please enter 1 for letter, 2 for envelope or 3 for parcel:";
cin>>number;
cout<<"Please enter item's weight in ounces:";
cin>>weight;
//Calculate the cost of the item based off of type and weight.
if (number==1&&weight<=1)
cout<<"******************************"<<cost1<<"\n\n";
else if (number==1||weight>1)
cout<<"******************************"<<cost1+(weight*addCharge1)<<"\n\n";
else if (number==2&&weight<=1)
cout<<"******************************"<<cost2<<"\n\n";
else if (number==2||weight>1)
cout<<"******************************"<<cost2+(weight*addCharge1)<<"\n\n";
else if (number==3&&weight<=3)
cout<<"******************************"<<cost3<<"\n\n";
else if (number==3||weight>3)
cout<<"******************************"<<cost3+(weight*addCharge2)<<"\n\n";
else
cout<<"Invalid input, restart program."<<endl;
base everything on pennies. format your output with floats at the tail end if you want, but don't use floats to track currency in the program itself. ever.
using floats is more hassle than its worth, unless you relish the idea of seeing something priced at $7.5
...
THE DECIMAL SHOULDNT FLOAT IN CURRENCY! ITS FIXED!
Ok awesome, thanks guys, what's funny is I just figured it out on my own through a lucky accident that it wasn't displaying right bc it was a decimal number.
I switched the constants to "const double" and put the weight and postage variables as "double" and now the correct numbers are displaying in the output.
Again, thanks so much for all the input, (haha get it)
base everything on pennies. format your output with floats at the tail end if you want, but don't use floats to track currency in the program itself. ever.
using floats is more hassle than its worth, unless you relish the idea of seeing something priced at $7.5
...
THE DECIMAL SHOULDNT FLOAT IN CURRENCY! ITS FIXED!
You ok there, zaphraud...? Random bias against floats...
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
float f = 7.5;
cout << fixed << setprecision(2);
cout << "$" << f << endl;
}
$7.50
Floats have plenty enough precision to deal with add/subtract math involving two decimal points.