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 !
#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.
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;