C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old June 9th, 2002, 03:20 PM
Neildadon Neildadon is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2002
Posts: 12 Neildadon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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;
}

Reply With Quote
  #2  
Old June 9th, 2002, 05:59 PM
zainul zainul is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 0 zainul User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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-

Reply With Quote
  #3  
Old June 9th, 2002, 08:33 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 49 m 40 sec
Reputation Power: 797
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > switch and string copy problems


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway