Setting a number to the hundredths place

Making a tip calculator does this make the final answer to the hundredths place like money

#include <iostream>
#include <cmath>
#include <iomanip>


using namespace std;

double valuePassingExample (float ntotal);
double ntotal;

int main (void)
{
float food, tax, ntax, total, ntotal, ftotal;
int twentytotal, tentotal, fivetotal, onetotal, quartertotal, dimetotal, nickeltotal, pennietotal;
cout<<setw(8)<<fixed<<setprecision(2);
cout << "input cost of food" << endl;
cin >> food;
cout << "input sales tax raging from 4.3% to 11%" << endl;
cin >> tax;
ntax=tax/100;
total=food*ntax;
ntotal=total+food;

twentytotal = ntotal / 20;
cout <<"You get back "<< twentytotal << " twenty dollar bill(s)"<<endl;
tentotal = ((ntotal - (20*twentytotal))/10);
cout << "You get back "<<tentotal <<" ten dollar bill(s)"<< endl;
fivetotal = ((ntotal - ((20*twentytotal)+(10*tentotal)))/5);
cout << "You get back " <<fivetotal<< " five dollar bill(s)" << endl;
onetotal = (ntotal- ((20*twentytotal)+(10*tentotal)+(5*fivetotal)));
cout<<"You get back "<<onetotal<<" one dollar bill(s)"<<endl;
quartertotal = (((ntotal*100) - ((2000*(twentytotal))+(1000*(tentotal))+(500*(fivetotal))+(100*(onetotal))))/25);
cout<<"You get back " << quartertotal<< " quarter(s)"<<endl;
dimetotal = (((ntotal*100) - ((2000*(twentytotal))+(1000*(tentotal))+(500*(fivetotal))+(100*(onetotal))+(25*quartertotal)))/10);
cout<<"You get back "<<dimetotal<<" dime(s)"<<endl;
nickeltotal = (((ntotal*100) - ((2000*(twentytotal))+(1000*(tentotal))+(500*(fivetotal))+(100*(onetotal))+(25*quartertotal)+(10*dimetotal)))/5);
cout<<"You get back " <<nickeltotal<<" nickel(s)"<<endl;
pennietotal = ((ntotal*100) - ((2000*(twentytotal))+(1000*(tentotal))+(500*(fivetotal))+(100*(onetotal))+(25*quartertotal)+(10*dimetotal)+(5*nickeltotal)));
cout<<"You get back " <<pennietotal<< " pennie(s)"<<endl;
system("pause");



ntotal = valuePassingExample (ntotal);


cout << endl;
cout << "New total = "<< ntotal << endl << "\n";


system("pause");
return (0);
}
double valuePassingExample(float ntotal)
{
double ftotal;

char percent;
float npercent;
cout << "Please choose a tip either 15%, 18% or 20% " << endl;
cin >> percent;
if (percent = 15)
{
npercent = 0.15;
}
else if (percent = 18)
{
npercent = 0.18;
}
else if (percent = 20)
{
npercent = 0.20;
}
ftotal=((npercent*ntotal)+(ntotal));
cout << "Your Final Total is " << ftotal << endl;


return (ftotal);
}
When you're checking a value, use double equals. A single equals is for assigning.
Example..
1
2
3
4
if (percent = 15)
{
npercent = 0.15;
}

should be
1
2
3
4
if (percent == 15) // checks IF percent is equal to 15
{
npercent = 0.15; // If it is, assign npercent to be 0.15
}
Last edited on
I tried that and I got an error when I ran it
I get this error "The variable 'npercent' is being used without being initialized" but if I have only one "=" then it works but only with 15 percent.
Last edited on
percent is a char but you are doing a comparison with an int. Plus, you are using a char and not a char array, which will only give you the first char. If you want to use an int, change percent to an int, otherwise use a char array and use "" around the value when doing the comparison. For this exercise, i would strongly recommend using an int instead of a char[]
Topic archived. No new replies allowed.