You are here:

C++/Don't understand how to start this program at all :(


1) using standard linux compiler
2) I am taking my first course in C++ and so far have been understanding it just fine until our recent assignment.  It involves figuring a carpool effciency rate for a company to remimburse employees "The Enviromental Awareness Club of Bigcorp Intl is proposing that the company subsidize, at $.08 per passenger km, the commuting costs of employees who form carpools that meet a prescribed minimum passenger efficiency.  Passenger efficiency P(in passenger * k/L) is defined as  P= ns/l where n is the number of passengers, s is the distance travled in km, and l is the number of liters of gas used.
Write a program that prompts the user for a minimum pasenger efficiency and then processes a file of data on exisitng carpools(carpool.txt), displaying a table of all carpools that meet the passenger efficiency minimum.  The file represents each carpool as a data line containing three numbers: number of people in the carpool, the total commuting distance per five day week, and the number of liters of gasoline consumed in a week of commuting.  The data file ends with a line of zeros.  Display your results in a table with this format:


Passenger  Commute  Gas Used  Efficiency  Subsidy

   4         75       11.0     27.3        24.00
   2         60       4.5      26.7         9.60

carpool.txt looks like this:
2 45 9
3 98 3
4 34 7
6 84 4
7 45 9
8 38 7
9 30 6

3)So far all I have managed to get is the prompting for user to input effeiciency, but then I am stumped as to how to read the values from the file and what to do with them.  I declared <fstream> already and set it to read my file, but do not understand how to get it to pull each value and store them in a variable where I can manippulate them for the efficency formula, any help or suggestions are greatly appreciated!

This is definately a tricky question. I've come across problems like yours when it comes to knowing what information goes where. Fortunately, there is a relatively easy solution to this.

Before you start reading the file, create an vector of type 'int' (or double, but since we know there are no decimals in the input file, 'int' will work). Then when you start reading the file, take each number that's read in and put it into the vector. So the first three elements of the vector would be '2', '45', '9' (without the single quotes, of course), and so on until you hit all the zeros. You can put all the zeros into the vector if you want to, but it depends on whether you want to use the zeros as part of  the loop you'll need later on (are other ways of doing it too though).

Now since all the information on the various carpools are stored, we need to know how to use those values. For carpool 1, you will need the values in positions 0, 1, and 2. For carpool 2 we'll need the values in positions 3, 4, and 5, etc. So if we want to apply the formula to all the sets of carpools, we would use a 'for' loop with some temporary variables. After it has run three times, then we would apply the formula and get the result. It would look something like this:

/*Don't quote me on the vector_name.length(), I don't remember if that's the right way to find out how long a vector is. This is a bit of an odd way to do it, but it works.*/

int temp_var_1;
int temp_var_2;
int temp_var_3;
int temp_counter = 0;
double efficiency;

//Start 'for' loop
for (int i = 0; i != vector_name.length(); i++) {

if (temp_counter % 3 == 0) {
temp_var_1 =;
temp_var_2 =;
temp_var_3 =;
temp_counter = 0;
efficiency = (temp_var_1 * temp_var_2) / temp_var_3;
cout << "The passenger efficiency is: " << efficiency << endl;
} //end 'if' statement
} //end 'for' loop

I hope that helps. If you need any clarification just let me know.

Best Of Luck,


All Answers

Answers by Expert:

Ask Experts


Alan F.


Having problems with any basic or intermediate parts of C++? I can answer those questions for you using simple English. I have been taking C++ in school for a couple years, so I still remember what it was like to be a beginner (especially with C++, which is a difficult language)!


I have a C++ web page that discuses the basics of C++. The address is:

©2017 All rights reserved.