• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

c++ question

railer

Golden Member
Pasted the whole thing. 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....


#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;
 
Change choice to a char and have "case 't'", "case 'f'", etc. A char is a number, just a small one.

edit: what do you need floating point numbers for? Avoid them if you can.
 
Nevermind...I'm such a noob. Forgot the quotes.....need em for chars and not #'s...grrr. Thanks again!

thanks....

but when i do that...i get errors.....

error C2065: 'E' : undeclared identifier
error C2051: case expression not constant

???
 
The foo in "case foo:" needs to be a constant; you can't use a variable. (I'm not sure if a const static variable will work (i.e. a const global))
 
It sure does; that's not very ideal code that you quoted. 😛

(edit: and this isn't c. there are huge differences, notably where operators are concerned)
 
Back
Top