Can't get my if/else statements to apply to cout

Here is the code in question. Whenever I compile, it always gives me zeroes for my total cost and discount



#include<iostream>
#include<fstream>
#include<cmath>
#include<iomanip>
#include<string>


using namespace std;




//Variables

string clientName;
string type;

int packageWeight;
int shipDistance;
int mileageRate;

float shippingRate;
float discount;
float totalCost;
float intermediateCost;
float discountTotal;


//If, Else for the Cost Rate and Miles Shipped
float calculateRate(int packageWeight, float shippingRate){
if (packageWeight<=2){
shippingRate = 1.10;
}
else
if(packageWeight<=6){
shippingRate = 2.20;
}
else
if(packageWeight<=10){
shippingRate = 3.70;
}
else
if(packageWeight<=20){
shippingRate = 4.80;
}

return shippingRate;
}
//Customer Type and Discount
float evaluateDiscount(string type, int shipDistance){

if(type == "N"){
discount = 0;
}
else
if(type == "R" && shipDistance >= 1000){
discount = 0.05;
}
else
if(type == "V" && shipDistance >= 500 ){
discount = 0.12;
}
else{
cout << "Invalid Customer type, Please try again";
}

return discount;
}

//function to define rate per 500 miles shipped
float calculateMilage(int shipDistance, float mileageRate){

if(shipDistance >= 10 && shipDistance <= 500)
mileageRate = 1;

else
if(shipDistance >= 501 && shipDistance <= 1000)
mileageRate = 2;

else
if(shipDistance >= 1001 && shipDistance <= 1500)
mileageRate = 3;

else
if(shipDistance >= 1501 && shipDistance <= 2000)
mileageRate = 4;

else
if(shipDistance >= 2001 && shipDistance <= 2500)
mileageRate = 5;

else
if(shipDistance>= 2501 && shipDistance <= 3000)
mileageRate = 6;

return 0;
}

int main()
{



cout << "Name" << endl;
cin >> clientName;
cout << "Type" << endl;
cin >> type;
cout << "Weight" << endl;
cin >> packageWeight;
cout << "Distance" << endl;
cin >> shipDistance;




//Discount Math
intermediateCost = shippingRate*mileageRate;
discountTotal = discount*intermediateCost;
totalCost = intermediateCost-(discountTotal);

cout << setprecision(2);

cout << fixed << setw(9) << "Customer" << setw(12) << "Customer" <<
setw(11) << "Package" << setw(12) << "Distance" << setw(22) << "Amount" << endl;
cout << fixed << setw(8) << "Name" << setw(12) << "Type" << setw(12) <<
"Weight" << setw(12) << "Shipped" << setw(12) << "Discount" << setw(9) << "Due"
<< endl;
cout << left << setw(16) << clientName << right << setw(2) << type << setw(12) <<
packageWeight << setw(14) << shipDistance << setw(8) << "$" << setw(4) << discountTotal <<
setw(6) << "$" << setw(4) << totalCost << endl;



return 0;


}
Last edited on
it always gives me zeroes for my total cost and discount


So, the two are created in global space (this is bad, stop doing this) with no particular value set. Could be anything.

1
2
float discount;
float totalCost;


Then main starts, and then your calculations:
1
2
3
intermediateCost = shippingRate*mileageRate;
discountTotal = discount*intermediateCost;
totalCost = intermediateCost-(discountTotal);

None of those variables have been set to anything; they're all whatever random value is in the memory.

So now discount and totalCost are just random values.

And then you output the values.

So you create them with random values, set them with more random values, and then output them.

I see you also have a number of functions that you never called. Did you mean to call the functions?
wouldn't discount, shippingRate and mileageRate be calling back to the if/else statements for their respective data? I'm sorry, I'm really new at this.
No.

A function only executes if you call it. The function main gets called for you; that's where your programme effectively starts.

If you want other functions to be called, you must call them yourself.

See section "Calling a function" : https://www.tutorialspoint.com/cplusplus/cpp_functions.htm



Topic archived. No new replies allowed.