Switch/case problem

This is a discussion on Switch/case problem within the C++ Programming forums, part of the General Programming Boards category; I've made a code to recognize a number and then declare a matching course. But it appears that this code ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    45

    Question Switch/case problem

    I've made a code to recognize a number and then declare a matching course.
    But it appears that this code sucks because i get 32 errors.
    Must be some sort of record......

    Code:
    char* course_check(int course)
    {
    	
    	
    	switch(course)
    	{
    	case 1:
    		char courseString[]="Maths";
    	    break;
    	case 2:
    		char courseString[]="English";
    	    break;
    	case 3:
    		char courseString[]="Deutch";
    	    break;
    	case 4: 
    		char courseString[]="French";
    	    break;
    	case 5:
    		char courseString[]="German";
                       break;
    	case 6:
    		char courseString[]="Geography";
                       break;
    	case 7:
    		char courseString[]="History";
                       break;
    	else:
    		char courseString[]="No Course";
                    }       
    	char* p=&courseString; .
    	return *p;
    }

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Correct me if im wrong but in the end of a switch statements its a default case not an else case. It should be
    Code:
    Default:
    	char courseString[]="No Course";
                    }
    Also i dont know if this was when you were entering your code, but theres a period in the end of one of yoru lines

    Code:
    char* p=&courseString; .
    Should be:
    Code:
    char* p=&courseString;

    One more thing. If woudl help to make your codes consistent when indenting so that it's easier to read. You have breaks all over the sides and its hard to figure out erros like that.
    Last edited by gamer4life687; 01-31-2003 at 09:38 AM.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    A switch does not comprise of if/else.

    Code:
    case 7:  {  char courseString[]="History";
                      break;   }
    
    default : {  char courseString[]="No Course";
                      break;   }
    }
    Kuphryn

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    What about removing the switch and use an array?
    Code:
    const char * course_check(int course)
    {
       static const char *courseString[] = {
          "No Course", "Maths", "English", "Deutch",
          "French", "German", "Geography", "History" };
    
       if(course < 0 || course > 7)
          course = 0;
    
       return courseString[course];
    }

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Also, courseString goes out of scope when exiting the switch statement. You have to declare it before, or something.
    I'm not even sure you can declare variables inside a switch due to the fact that the program flow can go through several cases:
    Code:
    switch(NrOfAdds)
    {
       case ThreeAdds:
          Myvar++;
    
       case TwoAdds:
          Myvar++;
    
       case OneAdd:
          Myvar++;
          break;
    
       default:
          break;
    }
    In the code below, it's not certain that MyVar is defined or not:
    Code:
    switch(Something)
    {
       case A:
          int Myvar;
    
       case B:
          MyVar = 4;
          break;
    
       default:
          break;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch/case problem?
    By Wiiplayer12 in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2008, 11:13 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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