Thread: Problems with switch()

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    37

    Problems with switch()

    Don't Laugh!!!
    I've been trying doing some work with the switch structure and I have run into some problems. I'm wondering, is it possible to work with this or should I just do it with an if structure.
    Here is the section below.
    The problem arises as soon as the compiler reaches case '10'. Is it possible to overcome this?

    Code:
    switch(result)
    {
    		
    		case '1':
    			fputs(station1, stdout);                                                
    			break;
    		case '2':
    			fputs(station2, stdout);
    			break;
    		case '3':
    			fputs(station3, stdout);
    			break;
    		case '4':
    			puts(station4, stdout);
    			break;
    		case '5':
    			fputs(station5, stdout);
    			break;
    		case '6':
    			fputs(station6, stdout);
    			break;
    		case '7':
    			fputs(station7, stdout);
    			break;
    		case '8':
    			fputs(station8, stdout);
    			break;
    		case '9':
    			fputs(station9, stdout);
    			break;
    		case "10":
    			fputs(station10, stdout);
    			break;
    		case '11':
    			fputs(station11, stdout);
    			break;
    		case '12':
    			fputs(station12, stdout);
    			break;
    		case '13':
    			fputs(station13, stdout);
    			break;
    		case '14':
    			fputs(station14, stdout);
    			break;
    		case '15':
    			fputs(station15, stdout);
    			break;
    		case '16':
    			fputs(station16, stdout);
    			break;
    		case '17':
    			fputs(station17, stdout);
    			break;
    		case '18':
    			fputs(station18, stdout);
    			break;
    		case '19':
    			fputs(station19, stdout);
    			break;
    		case '20':
    			fputs(station20, stdout);
    			break;
    		case '21':
    			fputs(station21, stdout);
    			break;
    		case '22':
    			fputs(station22, stdout);
    			break;
    		case '23':
    			fputs(station23, stdout);
    			break;
    		case '24':
    			fputs(station24, stdout);
    			break;
    		case '25':
    			fputs(station25, stdout);
    			break;
    		case '26':
    			fputs(station26, stdout);
    			break;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
            case '11':
    This is not a character: '11'.

    You can't use a switch with strings.

    Try a board search of some keywords: "switch strings".

    [edit]Ever consider first converting the index and then using an array for station?
    Last edited by Dave_Sinkula; 01-26-2005 at 10:52 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    37
    Thanks for the advice Dave. However, I think I should have also mentioned that I'm a beginner. "Board search?" This is the first time I hearing about it. What is it?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by duvernais28
    Thanks for the advice Dave. However, I think I should have also mentioned that I'm a beginner. "Board search?" This is the first time I hearing about it. What is it?
    It's one of those hyperlinked bits of text on many pages:
    http://cboard.cprogramming.com/search.php

    The FAQ may also be very useful.
    Last edited by Dave_Sinkula; 01-26-2005 at 10:57 PM. Reason: Added FAQ link.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    hello duvernais, welcome to the boards. I'm not sure if result is of type int, if it is you should be able to do cases of any constant; also, drop the single quotes surrounding the constants.

    > "Board search?" This is the first time I hearing about it. What is it?

    on top of each page at these forums you have a "search" link; press it, and explore there is also a search textbox at the bottom of each forum, you can use that to search for info in that specific forum only.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    37
    Thanks guys.
    Axon, result is actually a long, I am doing this operation after using the strtol() function. I'll follow your advise and see what comes up. Thanks again guys.

  7. #7
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    > result is actually a long

    doesn't matter; drop the single quotes and it'll work fine.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    case '4':
    puts(station4, stdout);
    Why is this one puts, and the rest are fputs?

    Also, this is just begging to be an array, as in
    Code:
    fputs( station[result], stdout );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    37
    Thanks alot Salem. I've already made the correction. Yes you're right, the stations are actually string arrays. I have already defined them all. " Result" actually coincides with the cases.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    37
    I've gone through my code and I don't think the problem was really with switch(). I havent figured out what it is though. Here's the code.
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    void main(void)
    {
        int *p;
        long result;
        char code[BUFSIZ];
        char station1[] = "(1) Gros Islet Station (GR)";
        char station2[] = "(2) Central Police Station (CPS)";
        char station3[] = "(3) Marchand Police Station (MA)";
        char station4[] = "(4) La Caye Police Station (LC)";
        char station5[] = "(5) Dennery Police Station (DE)";
        char station6[] = "(6) Micoud Police Station (MI)";
        char station7[] = "(7) Vieux Fort Police station (VX)";
        char station8[] = "(8) Choiseul Police Station (CH)";
        char station9[] = "(9) Soufriere Police Station (SU)";
        char station10[] = "(10) Canaries Police station (CN)";
        char station11[] = "(11) Anse La Raye Police Station (AN)";
        char station12[] = "(12) Marigot Police Station (MB)";
        char station13[] = "(13) Police Headquaters (PHQ)";
        char station14[] = "(14) Criminal Investigations Department (CID)";
        char station15[] = "(15) Criminal Records Office (CRO)";
        char station16[] = "(16) Traffic Unit (TU)";
        char station17[] = "(17) Immigration Department (IMM)";
        char station18[] = "(18) Community Relations Branch (CRB)";
        char station19[] = "(19) Special Branch (SB)";
        char station20[] = "(20) Complaints Unit (CU)";
        char station21[] = "(21) Police Motor Pool (PMP)";
        char station22[] = "(22) Drug Squad (DS)";
        char station23[] = "(23) Police Training School (PTS)";
        char station24[] = "(24) Special Services Unit (SSU)";
        char station25[] = "(25) Police Control Room (PCR)";
        char station26[] = "(26) Police Marine unit (PMU)";
    
        fputs("Department Code (1-26): ", stdout);
        fgets(code, sizeof(code), stdin);
        fflush(stdin);
    
        result = strtol(code, &p, 10);
    
        if(code[0] != '\n' && (*p == '\n' || *p == '\0'))
        {
            if(result < 1)
                    {
                        fputs("! Error: Out of Range(1-26). Press Enter to continue", stdout);
                            fflush(stdin);
                        getchar();
                        clrscr();
                        main();
                    }
                    else
                        if(result > 26)
                        {
                                fputs("! Error: Out of Range(1-26). Press Enter to continue", stdout);
                                    fflush(stdin);
                                getchar();
                                clrscr();
                                main();
                        }
                        else
                        {
                                    
                                clrscr();
                                    getchar();
    
                                switch(result)
                    {
                        case 1:
                            fputs(station1, stdout);
                                                    break;
                        case 2:
                            fputs(station2, stdout);
                            break;
                        case 3:
                            fputs(station3, stdout);
                            break;
                        case 4:
                            fputs(station4, stdout);
                            break;
                        case 5:
                            fputs(station5, stdout);
                            break;
                        case 6:
                            fputs(station6, stdout);
                            break;
                        case 7:
                            fputs(station7, stdout);
                            break;
                        case 8:
                            fputs(station8, stdout);
                            break;
                        case 9:
                            fputs(station9, stdout);
                            break;
                        case 10:
                            fputs(station10, stdout);
                            break;
                        case 11:
                            fputs(station11, stdout);
                            break;
                        case 12:
                            fputs(station12, stdout);
                            break;
                        case 13:
                            fputs(station13, stdout);
                            break;
                        case 14:
                            fputs(station14, stdout);
                            break;
                        case 15:
                            fputs(station15, stdout);
                            break;
                        case 16:
                            fputs(station16, stdout);
                            break;
                        case 17:
                            fputs(station17, stdout);
                            break;
                        case 18:
                            fputs(station18, stdout);
                            break;
                        case 19:
                            fputs(station19, stdout);
                            break;
                        case 20:
                            fputs(station20, stdout);
                            break;
                        case 21:
                            fputs(station21, stdout);
                            break;
                        case 22:
                            fputs(station22, stdout);
                            break;
                        case 23:
                            fputs(station23, stdout);
                            break;
                        case 24:
                            fputs(station24, stdout);
                            break;
                        case 25:
                            fputs(station25, stdout);
                            break;
                        case 26:
                            fputs(station26, stdout);
                            break;
                    }
                        }
            }
            else
            {
                    fputs("Invalid input. Please enter a number.", stdout);
                    fflush(stdin);
                    getchar();
                    clrscr();
                    main();
            }
    }

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    what it the problem you are encountering? it compile and runs fine for me (except that 'p' should be a char* - not an int*). also, as Salem suggested, you could use an array like this:

    Code:
    
    int main(void) {
     char *p;
     long result;
     char code[BUFSIZ];
     char * stations[] = {
         "(1) Gros Islet Station (GR)", 
         "(2) Central Police Station (CPS)", 
         "(3) Marchand Police Station (MA)", 
         "(4) La Caye Police Station (LC)", 
         "(5) Dennery Police Station (DE)", 
         "(6) Micoud Police Station (MI)", 
         "(7) Vieux Fort Police station (VX)", 
         "(8) Choiseul Police Station (CH)", 
         "(9) Soufriere Police Station (SU)", 
         "(10) Canaries Police station (CN)", 
         "(11) Anse La Raye Police Station (AN)", 
         "(12) Marigot Police Station (MB)", 
         "(13) Police Headquaters (PHQ)", 
         "(14) Criminal Investigations Department (CID)", 
         "(15) Criminal Records Office (CRO)", 
         "(16) Traffic Unit (TU)", 
         "(17) Immigration Department (IMM)", 
         "(18) Community Relations Branch (CRB)", 
         "(19) Special Branch (SB)", 
         "(20) Complaints Unit (CU)", 
         "(21) Police Motor Pool (PMP)", 
         "(22) Drug Squad (DS)", 
         "(23) Police Training School (PTS)", 
         "(24) Special Services Unit (SSU)", 
         "(25) Police Control Room (PCR)", 
         "(26) Police Marine unit (PMU)"  };
    
     for(;;) { 
      fputs("Department Code (1-26): ", stdout);
      fgets(code, sizeof(code), stdin);
      result = strtol(code, &p, 10);
      if(code[0] != '\n' && (*p == '\n' || *p == '\0')) {
       if(result < 1 || result > 26) {
        fputs("! Error: Out of Range(1-26). Press Enter to continue", stdout);
        getchar();
        clrscr();
        continue;
        }
       fputs(stations[result-1], stdout); 
       }   
      break;
      }
     return 0; 
     }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That ought to be
    const char * stations[] = ...


    And ideally, it would be loaded from a resource file.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> That ought to be const char * stations[] = ...

    good point.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    37
    Thanks alot guys. My knowledge of pointers is very limited. It would have taken me quite a long time to figure this out. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  2. nested switch issue
    By fsu_altek in forum C Programming
    Replies: 3
    Last Post: 02-15-2006, 10:29 PM
  3. switch problems...
    By rexnprogress in forum C Programming
    Replies: 6
    Last Post: 11-26-2005, 07:43 PM
  4. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  5. switch and string copy problems ?
    By Neildadon in forum C Programming
    Replies: 5
    Last Post: 06-12-2002, 07:42 AM