Multiple Error Messages to Compile Program

I'm trying to build a program and get rid of all the errors. I think I solved some, but I still have many errors listed.

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
171
172
173
174
175
176
177
178
179
180
181
  #include <iostream> 
#include <iomanip> 

using namespace std; 

const float PERSON_WT = 170.0; //missing ; and added it after 170.0 
const float LBS_PER_GAL = 6.7; // missing ; and added itafter 6.7   // warning C4305: 'initializing' : truncation from 'double' to 'const float'
const float EMPTY_WEIGHT = 9887.0; 
const float EMPTY_MOMENT = 3153953.0; 
 
void CargoMoment (int closet, int baggage ); //changed float to void  and changed (int, int) to (int closet, int baggage)  //error: see declaration of 'CargoMoment'
void CrewMoment( int crew );                 //changed float to void and added crew after int                             //error:  see declaration of 'CrewMoment'
void FuelMoment( int fuel );                 //changed float to void and added fual after int                             //error: see declaration of 'FuelMoment'

void GetData( int& crew , int& passengers, int& closet, int& baggage, int& fuel ); 
//change ( int&, int&, int&, int&, int& ) to ( int& crew, int& passengers, int& closet, int& baggage, int& fuel)

void PassengerMoment( int passengers);       //changed float to void and added passengers after int  //see declaration of 'PassengerMoment'
void PrintWarning(); 
 
int main() 
{ 
 int crew; 
 int passengers; 
 int closet; 
 int baggage; 
 int fuel; 
 float totalWt; 
 float centerOfGravity; 
 
 cout << fixed << showpoint << setprecision(2); 
 
 GetData(crew, passengers, closet, baggage, fuel); 
 
 totalWt = 
 EMPTY_WEIGHT + int(passengers + crew) * PERSON_WT + //for rid of float before passengers and changed it to int
 int(baggage + closet) + int(fuel) * LBS_PER_GAL;  //for rid of float before (baggage + closet) and (fuel) and changed it to int
 centerOfGravity = 
 CrewMoment(crew) + PassengerMoment(passengers) + //took out the ( in (CreMoment  //error C2186: '+' : illegal operand of type 'void'
 CargoMoment(closet, baggage) + FuelMoment(fuel) + 
 (EMPTY_MOMENT) / totalWt; //changed EMPTY_MOMENT) to (EMPTY_MOMENT) 
 
 cout << "Total weight is " << totalWt << " pounds." << endl; 
 cout << "Center of gravity is " << centerOfGravity 
 << " inches from the front of the plane." << endl; 
 PrintWarning(); 
 return 0; 
} 
 
//****************************************************************** 
 
void GetData( int& crew, int& passengers, int& closet, int& baggage, int& fuel) //added the ( symbol after fuel 
{ 
 

cout << "Enter the number of crew members." << endl; 
 cin >> crew; 
 cout << "Enter the number of passengers." << endl; 
 cin >> passengers; 
 cout << "Enter the weight, in pounds, of cargo in the" << endl 
 << " closet, rounded up to the nearest whole number." 
 << endl; 
 cin >> closet; 
 cout << "Enter the weight, in pounds, of cargo in the" << endl 
 << " aft baggage compartment, rounded up to the" << endl 
 << " nearest whole number." << endl; 
 cin >> baggage; 
 cout << "Enter the number of U.S. gallons of fuel" << endl 
 << " loaded, rounded up to the nearest whole number." 
 << endl; 
 cin >> fuel; 
 cout << endl; 
 cout << "Starship loading data as entered:" << endl 
 << " Crew: " << setw(6) << crew << endl 
 << " Passengers: " << setw(6) << passengers << endl 
 << " Closet weight: " << setw(6) << closet << " pounds" 
 << endl 
 << " Baggage weight:" << setw(6) << baggage << " pounds" 
 << endl 
 << " Fuel: " << setw(6) << fuel << " gallons" 
 << endl << endl; 
} 
 
//****************************************************************** 
 
void CrewMoment( /* in */ int crew ) //changed float to void
 
{ 
 const float CREW_DISTANCE = 143.0; // Distance to crew seats 
 // from front 
 
 return int(crew) * PERSON_WT * CREW_DISTANCE; //changed float(crew) to int(crew) //error C2562: 'CrewMoment' : 'void' function returning a value
} 
 
//****************************************************************** 
 
void PassengerMoment( int passengers ) //changed float to void
{ 
 const float ROW1_DIST = 219.0; 
 const float ROW2_DIST = 265.0; 
 const float ROW3_DIST = 295.0; 
 const float ROW4_DIST = 341.0; 
 float moment = 0.0; 

 if (passengers > 6) 
 { 
 moment = moment + 
 int(passengers - 6) * PERSON_WT * ROW4_DIST; //changed float(passengers -6) to int(passengers -6) 
 passengers = 6; 
 } 
 if (passengers > 4) 
 { 
 moment = moment + 
 int(passengers - 4) * PERSON_WT * ROW3_DIST; //changed float (passengers -4) to int (passengers -4) 
 passengers = 4; 
 } 
 if (passengers > 2) 
 { 
 moment = moment + 
 int(passengers - 2) * PERSON_WT * ROW1_DIST; //float to int
 passengers = 2; 
 } 
 if (passengers > 0) 
 moment = moment + 
 int(passengers) * PERSON_WT * ROW2_DIST; //float to int
 return moment;                           //error C2562: 'PassengerMoment' : 'void' function returning a value
} 
 
//****************************************************************** 
 
void CargoMoment( int closet, int baggage ) //changed float to void
{ 
 const float CLOSET_DIST = 182.0; 
 const float BAGGAGE_DIST = 386.0; 
 
 return int(closet) * CLOSET_DIST + int(baggage) * BAGGAGE_DIST; //changed both float(closet) and float(baggage) to int(closet) and int(baggage)
} 

//'CargoMoment' : 'void' function returning a value

 
//****************************************************************** 
 
void FuelMoment (int fuel ) //changed float to void and added the missing ( symbol 
 
{ 
 float fuelWt; 
 float fuelDistance; 
 
 fuelWt = int(fuel) * LBS_PER_GAL; //changed float to int
 if (fuel < 60) 
 fuelDistance = int(fuel) * 314.6; //changed float to int  //warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
 else if (fuel < 361) 

fuelDistance = 305.8 + (-0.01233 * int(fuel - 60)); //float to int    //warning C4244: ""
 else if (fuel < 521) 
 fuelDistance = 303.0 + ( 0.12500 * int(fuel - 361)); //float to int   //warning C4244
 else 
 fuelDistance = 323.0 + (-0.04444 * int(fuel - 521)); //float to int    //warning C4244
 return fuelDistance * fuelWt; 
} 
 
//error C2562: 'FuelMoment' : 'void' function returning a value

//****************************************************************** 
void PrintWarning() 
{ 
 cout << endl 
 << "Notice: This program assumes that passengers" << endl 
 << " fill the seat rows in order 2, 1, 3, 4, and" << endl 
 << " that each passenger and crew member weighs " 
 << PERSON_WT << " pounds." << endl 
 << " It also assumes that Jet-A fuel weighs " 
 << LBS_PER_GAL << " pounds" << endl 
 << " per U.S. gallon. The center of gravity" << endl 
 << " calculations for fuel are approximate. If" << endl 
 << " the aircraft is loaded near its limits, the" << endl 
 << " pilot's operating handbook should be used" << endl 
 << " to compute weight and center of gravity" << endl 
 << " with more accuracy." << endl;
} 
Hi BC300,

warning C4305: 'initializing' : truncation from 'double' to 'const float'


This happens because C++ defaults to the double type for decimal numbers. Doubles have more precision (15 or 16 significant figures) than floats (7 or 8 significant figures). The easy solution is to change all the decimal number variables to double. float is only necessary if you have billions of them or you use a graphics library say that requires them.

C an C++ have rules about the promotion of variable types - for example if you multiply an int by a double the result will be a double. So there is no need to cast everywhere like you are doing, especially casting an int to an int is unnecessary.

I am not sure what you were trying to achieve by changing the function return types to void. Change them to double as well. void means that the function does not return a value.

Global variables are generally a bad idea, so move them into main(), then send them as arguments to whatever function needs them - don't forget to change the declaration and definition of the functions. If you can get away with it: make the variables local to the function, if that is the only function that will need them.

You have more const variables, it is easier & better not to have "magic numbers " like -0.01233 throughout the code.

Also, it is a very good idea to always initialise your variables with something - I like to do it at the same time as declaration.

Meaningful variable names make it much easier for others reading your code to understand what is going on. I was wondering what the closet variable was, or whether it was misspelled - Perhaps ClosetWeight, or ClosetHoldWeight, or ClosetBaggageWeight or even ClosetCompartmentWeight might have been better. I am leaning towards ClosetBaggageWeight as being the most descriptive. Don't be afraid to make the variable name longer (within reason) if it is going to aid clarity.

There is an operator += which means that this:

1
2
moment = moment + 
 int(passengers - 6) * PERSON_WT * ROW4_DIST;


can be changed to this:

moment += (passengers - 6) * PERSON_WT * ROW4_DIST;

There are lots of other assignment operators as well.

Finally, you should indent your code properly because that aids understanding. If you are using an IDE, then this should be easy - automatic even.

Hope all goes well :+)



Changed the code as you suggested. I'm still dealing with errors that say, "conversion from 'double' to 'float', possible loss of data".

I did changed my code to
 
moment += (passengers - 6) * PERSON_WT * ROW4_DIST;


But that gave me a syntax errror.

Sorry for the formatting. I was given the code like that and it's hard to see what I should fix.

Anyway, I changed the upper code to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
double PERSON_WT = 170.0; //missing ; and added it after 170.0 
double LBS_PER_GAL = 6.7; // missing ; and added itafter 6.7   // warning C4305: 'initializing' : truncation from 'double' to 'const float'
double EMPTY_WEIGHT = 9887.0; 
double EMPTY_MOMENT = 3153953.0; 
 
double CargoMoment (int closet, int baggage ); //changed float to void  and changed (int, int) to (int closet, int baggage)  //error: see declaration of 'CargoMoment'
double CrewMoment( int crew );                 //changed float to void and added crew after int                             //error:  see declaration of 'CrewMoment'
double FuelMoment( int fuel );                 //changed float to void and added fuel after int                             //error: see declaration of 'FuelMoment'

double GetData( int& crew , int& passengers, int& closet, int& baggage, int& fuel ); 
//change ( int&, int&, int&, int&, int& ) to ( int& crew, int& passengers, int& closet, int& baggage, int& fuel)

double PassengerMoment( int passengers);       //changed float to void and added passengers after int  //see declaration of 'PassengerMoment'
void PrintWarning(); 
 


Is that right?

And would I also change the const part here to double?

1
2
3
4
5
const float ROW1_DIST = 219.0; 
 const float ROW2_DIST = 265.0; 
 const float ROW3_DIST = 295.0; 
 const float ROW4_DIST = 341.0; 
 float moment = 0.0; 
Hi BC,

But that gave me a syntax errror.


What was the text of the error - post it as program output.

I might have stuffed up, but the idea of the operator is that a = a + b can be written a += b

I was given the code like that and it's hard to see what I should fix.


It's not your code??? Can you explain?
Are you using an IDE? There should be an option to select & format the code.

Anyway, I changed the upper code to:


Think about which of those variables need to be decimal numbers - those are doubles. Which ones are whole numbers ? These can be int, or even unsigned int if the value should always be positive.

Function declarations must match their definitions, so make sure you have done that.

And would I also change the const part here to double?


Think about that for a minute ... What does const mean?

I really think you need to read the tutorial at the top left of this page.
When I say that it's not my code, I mean that my task is to take existing code I was given by my professor and fix the mistakes it has.

Would it be better to post the original code? I tried to write comments where I made changes.

I'm using Visual Studio 2010 Express. It's my first semester using it. I used Dev C++ last year. I do get a message right on top of my code that says "global scope". I don't know how to change it to "local scope".

Here are the errors in a more clear way(107, 114...and so on are the number lines):


Error3 error C2059: syntax error : '=' 107
Error4 error C2144: syntax error : 'int' should be preceded by ';' 114
Error7 error C2059: syntax error : '=' 119
14 IntelliSense: expected an expression 107
15 IntelliSense: expected a ';' 114
16 IntelliSense: expected an expression 119



Warning 2 warning C4244: '=' : conversion from 'double' to 'float', possible loss of data 41
Error 3 error C2059: syntax error : '=' 107
Error 4 error C2144: syntax error : 'int' should be preceded by ';' 114
Warning 5 warning C4552: '+' : operator has no effect; expected operator with side-effect 114
Warning 6 warning C4552: '*' : operator has no effect; expected operator with side-effect 114
Error 7 error C2059: syntax error : '=' 119
Warning 8 warning C4244: '=' : conversion from 'double' to 'float', possible loss of data 125
Warning 9 warning C4244: '=' : conversion from 'double' to 'float', possible loss of data 150
Warning 10 warning C4244: '=' : conversion from 'double' to 'float', possible loss of data 152
Warning 11 warning C4244: '=' : conversion from 'double' to 'float', possible loss of data 155
Warning 12 warning C4244: '=' : conversion from 'double' to 'float', possible loss of data 157
Warning 13 warning C4244: '=' : conversion from 'double' to 'float', possible loss of data 159
14 IntelliSense: expected an expression 107
15 IntelliSense: expected a ';' 114
16 IntelliSense: expected an expression 119


I thought const stood for constant? Did I write it wrong?
..... task is to take existing code I was given by my professor and fix the mistakes it has.


In that case, your prof might be expecting the variable types to be left as floats then. I am not sure whether you might loose marks for changing them to double. The advice I have given about that is valid, but you should be very sure of yourself if you are going to challenge the prof ..... He / She might say that a float is plenty good enough to deal with a 4sf number - an argument against that is that compilers are optimised to deal with doubles, it will take longer to access floats. But ff you say that they will know that you had advice from some one else. Having said all that, it might still be possible to have doubles, but say you had forum advice that they were better than floats.

Would it be better to post the original code?


Post the code as you have it now, plus all errors or warnings as they are in full - literally copy + paste them.

I do get a message right on top of my code that says "global scope". I don't know how to change it to "local scope".


That is because of lines 6 - 9: the variables are defined with global scope - that is outside of main() . If you move those lines inside main, you will have to send them as arguments to any function that needs them. Read up about the scope of variables.

Error3 error C2059: syntax error : '=' 107
Error4 error C2144: syntax error : 'int' should be preceded by ';' 114
Error7 error C2059: syntax error : '=' 119
14 IntelliSense: expected an expression 107
15 IntelliSense: expected a ';' 114
16 IntelliSense: expected an expression 119


These are probably due to this type of code:

107
108
moment = moment + 
 int(passengers - 6) * PERSON_WT * ROW4_DIST;


That code should all be on one line, some editors allow wrapping of lines with an invisible character to signal this intention, others use a \ followed by a carriage return. It's not a lot of code - just put it on one line.

The other errors about floats & doubles I have already mentioned.

I thought const stood for constant? Did I write it wrong?


but you asked:

And would I also change the const part here to double?


so you have const float but want to change it to double ? The answer to that is very trivial - I am sure you can work it out.

Do you understand what the different types & qualifiers mean?

There you go - some stuff to think about.
Topic archived. No new replies allowed.