|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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- |
|
#3
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > switch and string copy problems |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|