C++ proggy ?

This is a discussion on C++ proggy ? within the C++ Programming forums, part of the General Programming Boards category; Pasted the whole thing below. Works great so far, what I now need to do is instead of having the ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    5

    C++ proggy ?

    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;

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    a switch statement can use chars, just change the choice variable to a char and use...

    case 't':
    break;
    case 's':
    break;
    ....

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    5
    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

    ???
    Last edited by MrDrummond; 08-02-2004 at 09:23 PM. Reason: i'm a noob

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,700
    better post the new code...
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  5. #5
    Some Guy
    Join Date
    Jul 2004
    Posts
    32
    Did you change the variable "choice" type from int to char? It should work with that (it works for me).

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    5
    yea i got it. It was the <lack of> quotes. I edited my message instead of posting new. Thanks for all the help everybody....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. computer problems after proggy installation
    By *ClownPimp* in forum Tech Board
    Replies: 4
    Last Post: 06-03-2003, 05:28 PM
  2. help with proggy!
    By gcn_zelda in forum C++ Programming
    Replies: 7
    Last Post: 03-17-2003, 08:13 PM
  3. pointer proggy
    By sworc66 in forum C Programming
    Replies: 3
    Last Post: 09-03-2002, 02:41 AM
  4. My best C++ Programs
    By Paro in forum C++ Programming
    Replies: 113
    Last Post: 03-24-2002, 10:52 AM
  5. the history of Wingdings (and other nonsensical things)
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 01-19-2002, 12:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21