nested switch

This is a discussion on nested switch within the C++ Programming forums, part of the General Programming Boards category; I am working on my website and I need some information about nested switches for C++. I am working on ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    5

    nested switch

    I am working on my website and I need some information about nested switches for C++. I am working on nesting the abbrevations of states where when the user puts in the first letter of the state it finds the state name. If anyone has an example of a nested switch or can give me some insite I would appreciate it. If anyone knows of a good website I can refer too that would also help.

    Thanks

  2. #2
    Unregistered
    Guest
    you might not need a nested switch. One thing you have to think about is multiple states with the same first letter, heres a makeshift example:

    Code:
    char firstletter;
    string State[52];
    
    cin >> firstletter;
    
    switch(firstletter)
    {
         case 'W':
         {
              State[0] = "Wyoming";
              State[1] = "Washington";
              // etc...
              break;
         }
          
          default:
               cout << "None of the states start with that letter." << endl;
    }
    
    // to output it
    
    for (int N = 0; N < 52; N++)
         cout << State[N] << endl;
    one problem with this is that when your outputting it will output the whole array instead of just the blocks with the states of the same letter. This is just something to start with. There are many other ways to do this.

  3. #3
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    that was me above.
    the best things in life are simple.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    20
    Heres a lab i did using nested switches it might help.

    Code:
    /*******************************************************************
    *Lab 4                                                             *
    *By Whire Rider                                                    *
    *                                                                  *
    *Finds the name of a state from an abbreviation                    *
    *                                                                  *
    *******************************************************************/
    
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
    	char input[2];	//an array for the two letters
    	
    	cout<<endl  	//print out a title
    		<<"\t\tFIND THE STATE NAME"<<endl
    		<<"\t\t==================="<<endl<<endl;
    
    	cout<<"Enter the state abbreviations: ";
    	cin.getline(input,3);	//enter the abbreviation
    	
    	for(int i = 0;i<2;i++)	//make each element uppercase
    		input[i] = toupper(input[i]);
    
    
    	switch(input[0]) //switch for first letter
    	{
    	case 'M':
    		switch(input[1])	//switch for second letter
    		{
    		case 'E':cout<<"Maine"<<endl;break;
    		case 'D':cout<<"Maryland"<<endl;break;
    		case 'A':cout<<"Massachusetts"<<endl;break;
    		case 'I':cout<<"Michigan"<<endl;break;
    		case 'N':cout<<"Minnesota"<<endl;break;
    		case 'S':cout<<"Mississippi"<<endl;break;
    		case 'O':cout<<"Missouri"<<endl;break;
    		case 'T':cout<<"Montana"<<endl;break;
    		default: cout<<"No state has the abbreviation \'"<<input<<"\'"<<endl;
    		}
    		break;
    	case 'N':
    		switch(input[1])	//switch for second letter again
    		{
    		case 'E':cout<<"Nebraska"<<endl;break;
    		case 'V':cout<<"Nevada"<<endl;break;
    		case 'H':cout<<"New Hampshire"<<endl;break;
    		case 'J':cout<<"New Jersey"<<endl;break;
    		case 'M':cout<<"New Mexico"<<endl;break;
    		case 'Y':cout<<"New York"<<endl;break;
    		case 'C':cout<<"North Carolina"<<endl;break;
    		case 'D':cout<<"North Dakota"<<endl;break;
    		default: cout<<"No state has the abbreviation \'"<<input<<"\'"<<endl;
    		}
    		break;
    	default:
    		cout<<"I do not know of a state that starts with an \'"<<input[0]<<"\' character"<<endl;
    	}
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  2. nested switch issue
    By fsu_altek in forum C Programming
    Replies: 3
    Last Post: 02-15-2006, 10:29 PM
  3. A nested if inside the switch
    By Extropian in forum C Programming
    Replies: 20
    Last Post: 08-15-2005, 02:23 AM
  4. nested switch
    By kurz7 in forum C Programming
    Replies: 1
    Last Post: 09-07-2003, 10:32 AM
  5. Help with Nested Switch
    By liquidspaces in forum C++ Programming
    Replies: 9
    Last Post: 03-28-2003, 04:08 PM

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