#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    12
    Rep Power
    0

    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
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    0
    Rep Power
    0
    what was the error you get?
    do you declare it the right place?

    This is the example answer...I declared the string variable as global variable. but this is not quite good in realily...

    #include <stdio.h>
    #include <string.h> /* need to use string library */

    #define SIZE 200

    /* to up readable */
    #define TESCOS 1
    #define KUALA_LUMPUR 2
    #define TOKYO 3

    char stop_name[SIZE]; /* global variable */

    void BusStop(int);

    int main()
    {
    int number;

    printf("What is your boarding stop number?\n");
    scanf("%d",&number);

    BusStop(number);
    printf("Boarding at %s \n",stop_name);

    return 0;
    }

    void BusStop(int number)
    {
    switch(number)
    {
    case TESCOS: /* for choice 1 */
    strcpy(stop_name,"Tescos");
    break;
    case KUALA_LUMPUR: /* for choice 2 */
    strcpy(stop_name, "Kuala Lumpur");
    break;
    default:
    strcpy(stop_name, "No name!");
    break;
    }
    }

    /* program end */


    -zainul akramin-
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,595
    Rep Power
    4207
    Your bug is right here:

    scanf("%d",boarding);

    should be:

    scanf("%d", &boarding);

    You should be passing the address of the second argument (boarding) to the scanf function, so that it can alter the value of the variable boarding.

IMN logo majestic logo threadwatch logo seochat tools logo