Thread: switch and string copy problems ?

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    15

    switch and string copy problems ?

    How do i change an int to a char when using a switch and the string copy command

    the way im doing it isnt working

    everything is declared right. What i want is when 1 is pressed it prints Boarding at tescos

    printf("\nWhat is your boarding stop number: ");
    scanf("%d",boarding);
    bus_stop_name(boarding);
    printf("Boarding at %s", stop_name);


    bus_stop_name(int number)
    {
    switch(number)
    {
    case 1: strcpy(stop_name,"Tescos");
    break;

    }
    return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Ten to one says that the problem is here:

    scanf("%d",boarding);

    Note that scanf takes a pointer, so you need to include the address-of operator.

    scanf ( "%d", &boarding );

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    15

    still getting the same error

    thanks for your advice i tried it but im still getting 1 error with the bus_stop_name function

    it says expression syntax error

    can anyone see the errror from this code which ill provide

    #include <conio.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #include <dos.h>

    main()
    {
    clrscr();
    gotoxy(30, 5);
    printf("*******************");
    gotoxy(30,6);
    printf("Welcome to BromBus");
    gotoxy(30,7);
    printf("*******************\n");

    printf("\nWhat is your boarding stop number: ");
    scanf("%d", &boarding);
    bus_stop_name(boarding);
    printf("Boarding at %s", stop_name);

    printf("\nWhat is your destination stop number: ");
    scanf("%d",&destination);
    bus_stop_name(destination);
    printf("Travelling to %s", stop_name);

    printf("\nHow many adults are travelling: ");
    scanf("%d", &adults);

    printf("\nHow many children are travelling: ");
    scanf("%d", &children);

    printf("\nHow many oaps are travelling: ");
    scanf("%d", &oaps);

    bus_stop_name(int number)
    {
    switch(number)
    {
    case 1: strcpy(stop_name,"Tescos");
    break;
    case 2: strcpy(stop_name,"The Glades");
    break;
    case 3: strcpy(stop_name, "East Street");
    break;
    case 4: strcpy(stop_name, "Downham");
    break;
    case 5: strcpy(stop_name, "Catford");
    break;
    case 6: strcpy(stop_name, "Catford High Street");
    break;
    case 7: strcpy(stop_name, "Catford Dogs");
    break;
    case 8: strcpy(stop_name, "Ladywell");
    break;
    case 9: strcpy(stop_name, "Ladywell Arena");
    break;
    case 10: strcpy(stop_name, "Lewisham");
    break;
    case 11: strcpy(stop_name, "The Riverdale");
    break;
    case 12: strcpy(stop_name, "New Cross Road");
    break;
    case 13: strcpy(stop_name, "New Cross Gate");
    break;
    case 14: strcpy(stop_name, "Peckham High Street");
    break;
    case 15: strcpy(stop_name, "Peckham Rye");
    break;
    case 16: strcpy(stop_name, "Camberwell Green");
    break;
    case 17: strcpy(stop_name, "Elephant & Castle");
    break;
    case 18: strcpy(stop_name, "London Bridge");
    break;
    case 19: strcpy(stop_name, "Charing Cross");
    break;
    case 20: strcpy(stop_name,"Oxford Street");
    break;

    }
    return 0;
    }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    1: You can't nest functions.
    2: You can't use variables unless you've declared them first.

    This works for me, but I had to remove the calls to nonstandard conio functions:
    Code:
    #include <conio.h> 
    #include <stdio.h>  
    #include <string.h> 
    
    char stop_name[BUFSIZ] = {0};
    
    void bus_stop_name ( int number );
    
    int main ( void ) 
    { 
      int boarding, destination, adults, children, oaps;
      printf("*******************\n"); 
      printf("Welcome to BromBus\n"); 
      printf("*******************\n"); 
    
      printf("\nWhat is your boarding stop number: "); 
      scanf("%d", &boarding); 
      bus_stop_name(boarding); 
      printf("Boarding at %s", stop_name); 
    
      printf("\nWhat is your destination stop number: "); 
      scanf("%d",&destination); 
      bus_stop_name(destination); 
      printf("Travelling to %s", stop_name); 
    
      printf("\nHow many adults are travelling: "); 
      scanf("%d", &adults);
    
      printf("\nHow many children are travelling: "); 
      scanf("%d", &children); 
    
      printf("\nHow many oaps are travelling: "); 
      scanf("%d", &oaps); 
      return 0; 
    }
    
    void bus_stop_name ( int number )
    {
      switch(number) 
      { 
      case 1: strcpy(stop_name,"Tescos"); break; 
      case 2: strcpy(stop_name,"The Glades"); break; 
      case 3: strcpy(stop_name, "East Street"); break; 
      case 4: strcpy(stop_name, "Downham"); break; 
      case 5: strcpy(stop_name, "Catford"); break; 
      case 6: strcpy(stop_name, "Catford High Street"); break; 
      case 7: strcpy(stop_name, "Catford Dogs"); break; 
      case 8: strcpy(stop_name, "Ladywell"); break; 
      case 9: strcpy(stop_name, "Ladywell Arena"); break; 
      case 10: strcpy(stop_name, "Lewisham"); break; 
      case 11: strcpy(stop_name, "The Riverdale"); break; 
      case 12: strcpy(stop_name, "New Cross Road"); break; 
      case 13: strcpy(stop_name, "New Cross Gate"); break; 
      case 14: strcpy(stop_name, "Peckham High Street"); break; 
      case 15: strcpy(stop_name, "Peckham Rye"); break; 
      case 16: strcpy(stop_name, "Camberwell Green"); break; 
      case 17: strcpy(stop_name, "Elephant & Castle"); break; 
      case 18: strcpy(stop_name, "London Bridge"); break; 
      case 19: strcpy(stop_name, "Charing Cross"); break; 
      case 20: strcpy(stop_name,"Oxford Street"); break; 
      } 
    }
    However, to save yourself a lot of trouble with that switch statement, you could simply declare an array of strings with each street name and then use number as the index:
    Code:
    void bus_stop_name ( int number )
    {
      static const char *stops[NUMSTOPS] = 
      {
        {"Tescos"          },{"The Glades"         },{"East Street"        },
        {"Downham"         },{"Catford"            },{"Catford High Street"},
        {"Catford Dogs"    },{"Ladywell"           },{"Ladywell Arena"     },
        {"Lewisham"        },{"The Riverdale"      },{"New Cross Road"     },
        {"New Cross Gate"  },{"Peckham High Street"},{"Peckham Rye"        },
        {"Camberwell Green"},{"Elephant & Castle"  },{"London Bridge"      },
        {"Charing Cross"   },{"Oxford Street"      },
      };
      if ( number > 0 && number < NUMSTOPS )
        strcpy ( stop_name, stops[number - 1] );
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    15
    thanks for your help i got the program working. i just have one quick question

    char stop_name[BUFSIZ] = {0};

    is what made the difference in the program could you tell me why
    [BUFSIZ] = {0}; was used and what it does.

    many thanks

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >[BUFSIZ] = {0}; was used and what it does.
    That is an initialization of the array, BUFSIZ is a standard macro defining an array size, usually 512 or 1024. The {0} part says to initialize the array to zeros. I'll explain in more detail when I have some more time, if someone else doesn't beat me to it that is.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-30-2008, 02:48 PM
  2. switch string
    By l2u in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2006, 08:28 AM
  3. some really weird problems with string !
    By mellisa in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2003, 04:56 AM
  4. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM