Begginer C++ Problem

Hello!
I recently started to study C++ but I'm stuck on this basic problem:

You have the choice of buying two cars. One is more fuel efficient than the
other, but also more expensive. You know the price and fuel efficiency (in miles per gallon, mpg) of both cars. You plan to keep the car for ten years. Assume a price of $4 per gallon of gas and usage of 15,000 miles per year. You will pay cash for the car and not worry about financing costs. Which car is the better deal?


I have figured out an algorithm but I have absolutely no idea how to put it in C++.It's my first "exercise" and getting stuck so early is really demotivating me :/
If any kind soul could give me some insight,I'll greatly appreciate !
Hi people,

could you please post the algorithm? And could you also post the code you think it has to be? Then we can correct if necessary. Good luck!
#include<iostream>;
using namespace std;

int main ()
 int (purchase price 1);
 int (purchase price 2);
 int (fuel efficiency 1);
 int (fuel efficiency 2
      
cout<<"Insert the price of the first car"  <<endl;
cin>>purchase price 1;
cout<<"Insert the price of the fuel efficiency of the first car"<<endl;
cin>>fuel efficiency 1;
cout<<"Insert the price of the second car"<<endl;
cin>>purchase price 2;
cout<<"Insert the price of the fuel efficiency of the second car"<<endl;
cin>>fuel efficiency 2;


{annual fuel cost 1=4*annual fuel consumed 1;
annual fuel cost 2=4*annual fuel consumed 2;}

{annual fuel consumed 1=10000/fuel efficiency 1;
annual fuel consumed 2=10000/fuel efficiency 2;}

{operating cost 1=10*annual fuel cost 1;
operating cost 2=10*annual fuel cost 2;}

{total cost 1=purchase price 1+operating cost 1;
total cost 2=purchase price 2+operating cost 2;}


 If  { total cost 1 < total cost 2
 cout<<"Choose first car";
 else cout<<"Choose second car";

}

return 0;


Told you I'm very new at this so please don't laugh >_>
Well, your overall idea seems good, but you are struggling on the syntax. Go re-read the beginning sections over the tutorials and see if you can fix the errors.

http://cplusplus.com/doc/tutorial/
1 >C++ never uses spaces in the name of the variable.For example the variable 'purchase price 1' is wrong.You may use underscore( _ ) for this purpose and declare the variable as
int purchase_price_1;

2> The statement for including file never terminates with semicolon.
therefore the first line of the program should be
#include<iostream>

3> Initialise the body of function main by '{' and terminate it by '}'.
therefore the main will initialise as
int main()
{
.
.
.
main body ie your program
.
.
.
} //this is to terminate the main body after statement return 0;

4> Never use variables until the are declared.



:-) happy programing
Last edited on
I am beginner too and I enjoyed coding this one :)

to give you hints, here what should you do after taking what prashant gupta and firedraco said in mind:

1- inside the main function, declare the variables you are going to use:
int Car1, Car2, Gas1, Gas2

2- Then prompt the user to insert the values of these variables using cout and cin

3- then do the math:
if ((Car1 + (Gas1 * 15000)) < (Car2 + (Gas2 * 15000))) { Go for Car1, else Go for Car2 }

(you got the idea just write the code properly :) )
Last edited on
Topic archived. No new replies allowed.