Skip to search.

Breaking News Visit Yahoo! News for the latest.

×Close this window

  1. Home >
  2. All Categories >
  3. Computers & Internet >
  4. Programming & Design >
  5. Resolved Question
MagicWand MagicWan...
Member since:
March 30, 2006
Total points:
2,144 (Level 3)

Resolved Question

Show me another »

How to Write a C++ program to Calculate the Cost of This?

Write a c++ program to calculate the cost of a visit to a gas station. The program should ask the user whether he or she wants regular (2.629$ per gallon) or premium (2.979$ per gallon) of gasoline, whether or not a car wash is desired. Of course the program must also ask how many gallons it took to fill the tank. Then the program should calculate the cost of the gas and the total cost including a car wash if there was one. Help me please, Thank You!
chris by chris
Member since:
June 27, 2009
Total points:
181 (Level 1)

Best Answer - Chosen by Asker

double x, g;
char y;

cout<<"Press 1 for regular 2 for premium."<<endl;
cin>>x;
cout<<"would you like a car wash? y/n?"<<endl;
cin>>y;
cout<<"How many gallons to fill your tank?"<<endl;
cin>>g;

if ( x = 1 ) {
x = g * 2.629;
} else {
x = g * 2.979;

if ( y = "y" ) {
x = x + whatever car wash costs
}

cout<<"your total is: "<<x<<endl;
  • 2 people rated this as good
Asker's Rating:
4 out of 5
Asker's Comment:
Thank You!

There are currently no comments for this question.

Other Answers (1)

  • Luja by Luja
    Member since:
    September 25, 2009
    Total points:
    152 (Level 1)
    #include<iostream.h>
    #include<conio.h>
    main()
    {
    int type;
    float typecost[]={2.629,2.979};
    float carwashcost=0;
    int carwash;
    int noofgallons;
    float totalcost;

    cout<<"Enter Type:\n1-Regular (2.629$ per gallon)\n2-Premium (2.979$ per gallon)\n";
    cin>>type;
    cout<<"Number of gallons:";
    cin>>noofgallons;
    cout<<"Desire Car Wash:\n1-Yes \n2-No\n";
    cin>>carwash;
    if (carwash==1)
    {
    cout<<"Cost for Car Wash:\n";
    cin>>carwashcost;
    }
    totalcost = (typecost[type-1]*noofgallons) + carwashcost;
    cout<<"Total cost for gas is "<<(typecost[type-1]*noofgallons)<<" and total cost for the service is "<<totalcost;
    getche();
    }
    • 1 person rated this as good

Answers International

Yahoo! does not evaluate or guarantee the accuracy of any Yahoo! Answers content. Click here for the Full Disclaimer.

Help us improve Yahoo! Answers. Send Feedback