Need help inputting values with switch statements.
Oct 10, 2016 at 3:57am UTC
Hello, I'm currently writing a program that determines the cost of a travel based on travel distance, mpg, gas type, and either state (Michigan or Ohio). I am given values of the price of each gas type (regular, midgrade, and premium) for each state.
Michigan -
regular: 2.27
midgrade: 2.49
premium: 2.70
Ohio -
regular: 2.28
midgrade: 2.46
premium: 2.55
I've done everything so far and it works when I run it, besides the ending "total travel cost." I can't figure out how to input the prices of gas for whatever choice the user makes. Does this make sense? Thanks!
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
#include <conio.h> // For function getch()
#include <cstdlib> // For several general-purpose functions
#include <fstream> // For file handling
#include <iomanip> // For formatted output
#include <iostream> // For cin, cout, and system
#include <string> // For string data type
using namespace std; // So "std::cout" may be abbreviated to "cout"
int main()
{
int state;
int gastype;
int price; //price of gas
int mpg;
int distance;
int cost; //final cost
cout << "Welcome to the Travel Cost Calculator!" << endl;
cout << " " << endl; //blank line
cout << "Please select the state:" << endl;
cout << "0 - Michigan" << endl;
cout << "1 - Ohio" << endl;
cout << "Choice: " ;
cin >> state;
switch (state)
{
case 0:
cout << "Michigan Selected" << endl;
break ;
case 1:
cout << "Ohio Selected" << endl;
break ;
default :
cout << "The number entered isn't valid." << endl;
}
//===============================================================================
cout << " " << endl; //blank line
cout << "Please select the gas type:" << endl;
cout << "0 - Regular" << endl;
cout << "1 - Midgrade" << endl;
cout << "2 - Premium" << endl;
cout << "Choice: " ;
cin >> gastype;
switch (gastype)
{
case 0:
cout << "Regular Selected" << endl;
break ;
case 1:
cout << "Midgrade Selected" << endl;
break ;
case 2:
cout << "Premium Selected" << endl;
break ;
default :
cout << "The number entered isn't valid." << endl;
}
//===============================================================================
cout << " " << endl; //blank line
cout << "Input MPG: " ;
cin >> mpg;
//===============================================================================
cout << " " << endl; //blank line
cout << "Input Travel Distance (in miles): " ;
cin >> distance;
//===============================================================================
cost = distance / mpg * price;
//===============================================================================
cout << " " << endl; //blank line
cout << "Your travel cost is: " << cost;
//===============================================================================
return 0;
}
Oct 10, 2016 at 6:52am UTC
You can set the prices before the break for each case.
Topic archived. No new replies allowed.