I don’t know what the heck is going on admittedly I have been drinking a lot of coffee however that still does not explain why this code is not working this is my fifth time rewriting this code in as many different ways and it only compiled once however the total did not exclude any of unneeded values. And now the variables are not allowing the program to compile did i miss something or am i just crazy. the only errors i am getting are:
warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning) and
error C2106: '=' : left operand must be l-value
I have no idea of what they mean or how to fix them any help would be greatly appreciated.
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <cctype>
#include <string>
usingnamespace std;
int main()
{
char response1;
char response2;
char response3;
int n=0;
int y=1;
int Adult;
int SeniorDisabled;
int Youth;
int BicycleSurcharge;
int VehicleAndDriverA;
int VehicleAndDriverSeniorDisabledB;
int OverHeightSurcharge;
int VehicleLengthA;
int VehicleLengthB;
int VehicleLengthC;
int k=1;
double total=(((13.15*Adult)+(6.55*SeniorDisabled)+(10.55*Youth)
+(4.00*BicycleSurcharge)+(51.20*VehicleAndDriverA)+
(44.60*VehicleAndDriverSeniorDisabledB)+(51.20*OverHeightSurcharge)
+(76.80*VehicleLengthA)+(153.60*VehicleLengthA*VehicleLengthB)
+(204.80*VehicleLengthC))/k);
cout<<"Are you driving a vehicle onto the ferry (y/n)?";
cin>> response1;
if (response1 == 'y')
{
cout<<"Is the driver a senior citizen (65 or over) or disabled (y/n)?";
cin >> response2;
if (response2 == 'y')
{
VehicleAndDriverSeniorDisabledB=y, VehicleAndDriverA=n;
}
elseif (response2 == 'n')
{
VehicleAndDriverA=y, VehicleAndDriverSeniorDisabledB=n;
cout<<"How many people in your group are traveling with a bicycle?";
cin>>BicycleSurcharge;
cout<<"How many passengers in your vehicle (excluding the driver)?"<< endl;
cout<<"Adults (age 19 to 64):";
cin>>Adult;
cout<<"Senior Citizens (65 or older) or Disabled Persons:";
cin>>SeniorDisabled;
cout<<"Youth (5 -18 years old):";
cin>>Youth;
cout<<"Your total fare is "<< total <<endl;
system ("pause");
return 0;
}
cout<<"How many passengers in your vehicle (excluding the driver)?"<< endl;
cout<<"Adults (age 19 to 64):";
cin>>Adult;
cout<<"Senior Citizens (65 or older) or Disabled Persons:";
cin>>SeniorDisabled;
cout<<"Youth (5 -18 years old):";
cin>>Youth;
cout<<"Is your vehicle over 7 feet, 6 inches in height (y/n)?";
cin >> response3;
if (response3 == 'y' )
{
OverHeightSurcharge=y;
}
elseif (response3 == 'n' )
{
OverHeightSurcharge=n;
}
{
int l;
cout<<"How long is your vehicle in feet?";
cin >>l;
if (l <=30)
{
VehicleLengthA= y;
}
elseif (l <= 30 && OverHeightSurcharge=y)
{
VehicleLengthB= y;
}
elseif (30 < l || l <= 39)
{
VehicleLengthC= y;
}
elseif (l >= 40)
{
k=0;
cout<< "Your total fare is "<< total <<endl<<
"Your vehicle is too long to get on the fary please find an alternate route"<<endl <<
system ("pause");
return 0;
}
cout<<"Your total fare is "<< total <<endl;
system ("pause");
system ("pause");
return 0;
}
}
}
Needs to be father down the program because essentially you are going
13.15* garbagedata /// and so forth
In other words adult, seniordisabled and so forth need to be defined before you can use them in a equation. You could just do the operation in the cout statement if you wanted.
also is line 67 onwards part of a function? or what is going on there? if its not you have return 0; three times when it is really needed only once at the end of the program (also this suggest some organization problems) unless you are building functions then it is a different ball game. Also if you give the direct lines for error it would be appreciated.
Furthermore some of your else if statments are not needed I would double check that out for example
essentially are causing the program to check for something that is are arguably already checked for. Like for the code above if some thing is not less then = 30 or is not between the range 30 - 39, then the answer can only be arguable be bigger. Accordingly only a else statement is need like so...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
if (l <=30)
{
//code
}
elseif (l <= 30 && OverHeightSurcharge=y)
{
//code
}
elseif (30 < l || l <= 39)
{
//code
}
else
{
//code
}
Thanks for the quick response the suggestions you made helped a lot
What I was trying to get the output to look something like this:
Welcome to the Fare Calculator
Are you driving a vehicle onto the ferry (y/n)?
Is the driver a senior citizen (65 or over) or disabled (y/n)?
How many passengers in your vehicle (excluding the driver)?
Adults (age 19 – 64):
Senior Citizens (65 or older) or Disabled Persons:
Youth (5 -18 years old):
Is your vehicle over 7 feet, 6 inches in height (y/n)?
How long is your vehicle in feet?
Your total fare is “total”
Thank you for using this Fare Calculator
I guess I made it more complicated than it had to be.
Well, the biggest problem I noticed was the organization of the code. Which I think may be contributing to complier errors. In general I like to define all my data then use whatever form be it a combo of cout and cin's to get that data initialized and then create the output. Also are some of copied code functions? because if not just the way you organized data may cause the biggest issue. In general the program should first declare the data like so
char response1;
char response2;
char response3;
int n=0;
int y=1;
int Adult;
int SeniorDisabled;
int Youth;
int BicycleSurcharge;
int VehicleAndDriverA;
int VehicleAndDriverSeniorDisabledB;
int OverHeightSurcharge;
int VehicleLengthA;
int VehicleLengthB;
int VehicleLengthC;
int l; // <-- this int doesn't need to hide in the middle of the program
int k=1;
double total; //note that this has to be declared before we define it as any equation.
//then go your if/if else/else statements to initialize the other data.
//then go
total = //the equation for total
cout << "this is the total: " << total << endl;
As to the complicated thing there are many ways to tackle a problem in programming. Hopefully this helps.
#include "stdafx.h"
#include <iostream>
#include <cctype>
#include <string>
usingnamespace std;
int main()
{
char response1(1);
char response2(1);
char response3(1);
int y= 1;
int n= 0;
int Adult;
int Senior_Disabled;
int Youth;
int Bicycle_Surcharge;
int Vehicle_And_DriverA;
int Vehicle_And_Driver_Senior_DisabledB;
int Over_Height_Surcharge;
int Vehicle_LengthA;
int Vehicle_LengthB;
int Vehicle_LengthC;
int l;
cout<<"Welcome to Dezmon Smith ’s Fare Calculator."<< endl;
cout<<"Are you driving a vehicle onto the ferry (y/n)?";
cin>> response1;
while (tolower(response1) != 'n' && tolower(response1) != 'y')
{
cout << "\nPlease enter y (Yes) or n (No): ";
cin>> response1;
}
if (response1 == 'n')
{
cout<<"How many Adults in your group(age 19 to 64):";
cin>>Adult;
cout<<"How many Senior Citizens (65 or older) or Disabled Persons"<< endl<< "in your group:";
cin>>Senior_Disabled;
cout<<"How many Youth (5 to 18 years old) in your group:";
cin>>Youth;
cout<<"How many people in your group are traveling with a bicycle?";
cin>>Bicycle_Surcharge;
Vehicle_And_DriverA=0;
Vehicle_And_Driver_Senior_DisabledB=0;
Over_Height_Surcharge=0;
Vehicle_LengthA= 0;
Vehicle_LengthB= 0;
Vehicle_LengthC= 0;
{
double total=((13.15*Adult)+(6.55*Senior_Disabled)+(10.55*Youth)
+(4.00*Bicycle_Surcharge)+(51.20*Vehicle_And_DriverA)+
(44.60*Vehicle_And_Driver_Senior_DisabledB)+(51.20*Over_Height_Surcharge)
+(76.80*Vehicle_LengthA)+(153.60*Vehicle_LengthB)+(204.80*Vehicle_LengthC));
cout<<"Your total fare is "<< total <<endl;
cout<<"Thank you for using Dezmon Smith ’s Fare Calculator."<<endl;
system ("pause");
return 0;
}
}
elseif (response1 == 'y')
{
cout<<"Is the driver a senior citizen (65 or over) or disabled (y/n)?";
cin >> response2;
while (tolower(response2) != 'n' && tolower(response2) != 'y')
{
cout << "\nPlease enter Y (Yes) or N (No): ";
cin >> response2;
}
if (response2== 'y')
{
Vehicle_And_Driver_Senior_DisabledB=1, Vehicle_And_DriverA=0;
}
elseif (response2 =='n')
{
Vehicle_And_DriverA=1, Vehicle_And_Driver_Senior_DisabledB=0;
}
{
cout<<"How many passengers in your vehicle (excluding the driver)?"<< endl;
cout<<"How many Adults (age 19 to 64):";
cin>>Adult;
cout<<"How many Senior Citizens (65 or older) or Disabled Persons:";
cin>>Senior_Disabled;
cout<<"How many Youth (5 to 18 years old):";
cin>>Youth;
}
cout<<"Is your vehicle over 7 feet, 6 inches in height (y/n)?";
cin >> response3;
while (tolower(response3) != 'n' && tolower(response3) != 'y')
{
cout << "\nPlease enter Y (Yes) or N (No): ";
cin >> response3;
}
if (response3 == 'y' )
{
Over_Height_Surcharge=1;
}
elseif (response3 == 'n' )
{
Over_Height_Surcharge=0;
}
cout<<"How long is your vehicle in feet?";
cin >>l;
while (l >= 40)
{
cout<<"Your vehicle is too long to get on the fary please find an alternate route"<<endl;
double total=0;
cout<<"Your total fare is "<< total <<endl;
cout<<"Thank you for using Dezmon Smith’s Fare Calculator."<<endl;
system ("pause");
return 0;
}
if ( l < 20 || l <=30 && Over_Height_Surcharge==1)
{
Bicycle_Surcharge=0;
Over_Height_Surcharge=0;
Vehicle_LengthA= 1;
Vehicle_LengthB= 0;
Vehicle_LengthC= 0;
double total=((13.15*Adult)+(6.55*Senior_Disabled)+(10.55*Youth)
+(4.00*Bicycle_Surcharge)+(51.20*Vehicle_And_DriverA)+
(44.60*Vehicle_And_Driver_Senior_DisabledB)+(51.20*Over_Height_Surcharge)
+(76.80*Vehicle_LengthA)+(153.60*Vehicle_LengthB)+(204.80*Vehicle_LengthC));
cout<<"Your total fare is "<< total <<endl;
cout<<"Thank you for using Dezmon Smith ’s Fare Calculator."<<endl;
system ("pause");
return 0;
}
elseif (30 < l && l <= 39 && Over_Height_Surcharge==1)
{
Bicycle_Surcharge=0;
Over_Height_Surcharge=0;
Vehicle_LengthA= 0;
Vehicle_LengthB= 0;
Vehicle_LengthC= 1;
double total=((13.15*Adult)+(6.55*Senior_Disabled)+(10.55*Youth)
+(4.00*Bicycle_Surcharge)+(51.20*Vehicle_And_DriverA)+
(44.60*Vehicle_And_Driver_Senior_DisabledB)+(51.20*Over_Height_Surcharge)
+(76.80*Vehicle_LengthA)+(153.60*Vehicle_LengthB)+(204.80*Vehicle_LengthC));
cout<<"Your total fare is "<< total <<endl;
cout<<"Thank you for using Dezmon Smith ’s Fare Calculator."<<endl;
system ("pause");
return 0;
}
}
}
i have run this code several times and run in to a calculation error ? is there something off about my if statements that is causing this?
There is weird bracket placements in your statement through the code.
Like for example on line 36 and 55 you have two open bracket statements yet only one if statement. This done a few times throughout your code.
if (response1 == 'n')
{
cout<<"How many Adults in your group(age 19 to 64):";
cin>>Adult;
cout<<"How many Senior Citizens (65 or older) or Disabled Persons"<< endl<< "in your group:";
cin>>Senior_Disabled;
cout<<"How many Youth (5 to 18 years old) in your group:";
cin>>Youth;
cout<<"How many people in your group are traveling with a bicycle?";
cin>>Bicycle_Surcharge;
Vehicle_And_DriverA=0;
Vehicle_And_Driver_Senior_DisabledB=0;
Over_Height_Surcharge=0;
Vehicle_LengthA= 0;
Vehicle_LengthB= 0;
Vehicle_LengthC= 0;
{// not needed
double total=((13.15*Adult)+(6.55*Senior_Disabled)+(10.55*Youth)
+(4.00*Bicycle_Surcharge)+(51.20*Vehicle_And_DriverA)+
(44.60*Vehicle_And_Driver_Senior_DisabledB)+(51.20*Over_Height_Surcharge)
+(76.80*Vehicle_LengthA)+(153.60*Vehicle_LengthB)+(204.80*Vehicle_LengthC));
cout<<"Your total fare is "<< total <<endl;
cout<<"Thank you for using Dezmon Smith ’s Fare Calculator."<<endl;
system ("pause");
return 0;
} //not needed
}
also, I would suggest just declaring total at the top such as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// the other declarations;
int l;
double total;
// and then in your code go
total=((13.15*Adult)+(6.55*Senior_Disabled)+(10.55*Youth)
+(4.00*Bicycle_Surcharge)+(51.20*Vehicle_And_DriverA)+
(44.60*Vehicle_And_Driver_Senior_DisabledB)+(51.20*Over_Height_Surcharge)
+(76.80*Vehicle_LengthA)+(153.60*Vehicle_LengthB)+(204.80*Vehicle_LengthC));
// or you just remove the variable all together and go
cout<<"Your total fare is "<< ((13.15*Adult)+(6.55*Senior_Disabled)+(10.55*Youth)
+(4.00*Bicycle_Surcharge)+(51.20*Vehicle_And_DriverA)+
(44.60*Vehicle_And_Driver_Senior_DisabledB)+(51.20*Over_Height_Surcharge)
+(76.80*Vehicle_LengthA)+(153.60*Vehicle_LengthB)+(204.80*Vehicle_LengthC)) << endl;
Once again your using a else if argument when it is not really needed for example:
if (response1 == 'n')
{
// code
}
elseif (response1 == 'y')
{
// code
}
// logically since you checked whether the answer can only be 'y' or 'n' then the answer should only be either or.
[code]
if (response1 == 'n')
{
// code
}
else
{
// code
}
Your starting to get close to the finished product. Just check your bracket placement are they really needed? and also for your if and else if statements are they checking for something that ideally could only be? ie if it is y then can it only be n?
I find it helpful to write a section of the code in a different cpp file and test if it works on its own before before adding it all into a bigger file. Perhaps this will help you as well? In terms of the calculation error it is most likely because of something getting multiplied or added with a piece of undefined data. Try to repeat the last time this came up with a debugger and you'll see the culprit.