Pasted the whole thing below. Works great so far, what I now need to do is instead of having the user type 1,2,3 or 4 for the car type, is type t,f,c, or d. Every time I try to assign letters to the constants, the program hangs. Do I need a different swtich statement. Do switches only work with #'s? Thanks....
Code:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const string HEADING = "Al's Awesome Autos" ;
const string CAR_CHOICE_1 = "Toyota";
const string CAR_CHOICE_2 = "Ford";
const string CAR_CHOICE_3 = "Chevy";
const string CAR_CHOICE_4 = "Dodge";
const double CAR_COST_1 = 25000.00;
const double CAR_COST_2 = 22000.00;
const double CAR_COST_3 = 18000.00;
const double CAR_COST_4 = 20000.00;
const int WIDTH = 40;
const char BLANK = ' ';
void PrintHeading ( void );
void PrintMenu ( void );
void PrintDivider ( void );
int main(void)
{
int choice;
string carType;
double carCost;
// Set the decimal precision to 2
cout.setf(ios::fixed);
cout.precision(2);
//Print the heading on the screen
PrintHeading ( );
// Display the menu of conversion options
PrintMenu ( );
//Get the menu choice from the user
cout << "Enter the type of car you want: ";
cin >> choice;
// Use a switch structure to set the conversion factor
switch( choice )
{
case 1:
carType = CAR_CHOICE_1;
carCost = CAR_COST_1;
break;
case 2:
carType = CAR_CHOICE_2;
carCost = CAR_COST_2;
break;
case 3:
carType = CAR_CHOICE_3;
carCost = CAR_COST_3;
break;
case 4:
carType = CAR_CHOICE_4;
carCost = CAR_COST_4;
break;
default:
carType = "Invalid";
carCost = 0.0;
break;
}
cout << endl << endl;
PrintDivider ( );
//if the menu choice is valid,
//print the cost of the car
if (!(carType == "Invalid"))
{
cout << "You have selected to buy a " << carType << endl;
cout << "The cost of the car is $ " << carCost << endl;
}
else
// otherwise it is an invalid selection
{
cout << "Illegal selection - no info given"
<< endl;
}
PrintDivider ();
cout << endl << endl;
return 0;
}
void PrintHeading ( void )
{
int stringLength = (int)HEADING.length();
PrintDivider( );
cout << setw((40-stringLength)/2) << BLANK;
cout << HEADING << endl;
PrintDivider( );
}
void PrintMenu ( void )
{
cout << "Menu of the type of Cars:" << endl;
cout << "1 - Toyota" << endl;
cout << "2 - Ford" << endl;
cout << "3 - Chevy" << endl;
cout << "4 - Dodge" << endl << endl;
}
void PrintDivider ( void )
{
cout << setfill('$');
cout << setw(WIDTH) << BLANK;
cout << setfill(' ');
cout << endl;