errors in variables

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  #include "stdafx.h"
#include <iostream>
#include <cmath>
#include <cctype>
#include <string>
using namespace 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;
}
else if (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;	
}
else if (response3 == 'n' )
{
OverHeightSurcharge=n;
}

{
int l;
cout<<"How long is your vehicle in feet?";	
cin >>l;
if (l <=30)
{
VehicleLengthA= y;
}
else if (l <= 30 && OverHeightSurcharge=y)
{
VehicleLengthB= y;
}
else if (30 < l || l <= 39)
{
VehicleLengthC= y;
}
else if (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;
}
}
}
Last edited on
1
2
3
4
5
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);


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
1
2
3
4
5
6
7
8
9
10
11
12
13
if (l <=30)
{
VehicleLengthA= y;
}
else if (l <= 30 && OverHeightSurcharge=y)
{
VehicleLengthB= y;
}
else if (30 < l || l <= 39)
{
VehicleLengthC= y;
}
else if (l >= 40)


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
}
else if (l <= 30 && OverHeightSurcharge=y)
{
          //code
}

else if (30 < l || l <= 39)
{
       //code
}

else 
{
       //code
}


Hopefully that make sense.
Last edited on
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.
Use switch
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

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.
i think i got it but:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "stdafx.h"
#include <iostream>
#include <cctype>
#include <string>
using namespace 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;
										}
								
								} 
	else if (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;
										}
								else if (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;					
									}
								else if   (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;
							}
					else if (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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
if (response1 == 'n')
{

// code 
}
else if (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.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13

swicth (response1)
{
   case 'y':
            .....
            break;
   case 'n':
            ......
            break;
   default:
            .......
            break;
}
got it figured out thanks guys.
Last edited on
Topic archived. No new replies allowed.