The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
C programming help!!
Discuss C programming help!! in the C Programming forum on Dev Shed. C programming help!! C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 21st, 2002, 01:07 PM
|
|
Junior Member
|
|
Join Date: May 2002
Posts: 12
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
C programming help!!
Hi im a 18 year student from the UK I really need help with a project i have to work on for my college assignment. Im learning borland c i would say my level is basic i have to write a bus ticket program which im struggling with.
This is what i have to do.
The public transport company BromBus, have decided they would like to investigate the feasibility of upgrading their traditional ticket machines further and have decided to offer the tender for the contract of producing a program written in C.
The new system is based around a VDU display and keypad for data entry. The specification from BromBus is as follows.
The route is made up of twenty stops, each of which is allocated a unique stop number starting from 1, busses will follow the same journey in either direction.
When run, the program must display an opening screen featuring the company name and a prompt for the boarding stop number; when a number is entered the appropriate address should print to the screen.
The driver should then see a prompt for passengers waiting to board, if the answer is No:
A default summary screen showing that both the number of passengers boarded and fares taken was zero.
If passengers are waiting to board, then the screen should display:
A prompt for destination stop number; that will be
accompanied by the address following entry.
Prompts for number of adults, children and OAP's
Note that passengers are treated as groups.
When the details are completed for a group, a ticket display should appear for 15 seconds showing details of:
Boarding stop number and address.
Number of adults, children and OAP's in the group.
Default fare and total fare for each category.
Total fare taken for the group.
The total number of passengers in the group.
The process described above should be repeated until no more passenger groups are left.
Standard Passenger Fare Charges
Passenger
Standard Fare
Adult
35p
Child
27p
OAP
15p
I have to use at least 3 functions in the code. I am really having problems understanding functions. i have tried many examples and read many web sites but most web sites arent very clear when explaining. i am using an old version of c called C++ 3.0
Any ideas or solutions to tackling this program would be most appreciated.
Many thanks
|

May 21st, 2002, 02:17 PM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
a function consists of three parts:
- input values
- output values (usually only one)
- some code to produce the output value.
in c this looks like this:
Code:
int sum(int a,b) {
return a+b;
}
this would be a mathematical function.
a function can also group a task in one central location:
Code:
char ask_driver() {
char tmp;
printf("Are there guests?");
do {
scanf("%c",tmp);
} while ( (tmp!='y') && (tmp!='n') )
return tmp;
}
this function takes no input parameters "()" and returns one character (weŽll use "y" for yes and "n" for no.). it does not allow you to type anything else.
functions are handy if you need to re-use code. if you type code twice and you correct a mistake in the first, 99% youŽll forget about the second time this code appears. say your bus company rises prices.
and your code will look more readable.
compare this:
Code:
int main() {
while (true) {
printf("how many passengers");
do {
scanf("%d",&passengers);
} while (passengers<=1)
printf("how many adults");
do {
scanf("%d",&adults);
} while (adults<=0)
printf("how many children");
do {
scanf("%d",&children);
} while (adults<=0)
}
}
compared to:
Code:
void ask(char *question, *fmt;void *answer; int min) {
do {
printf(question);
scanf(*fmt, answer);
} while (*(int*)answer<=min);
}
int main() {
while (true) {
ask("How many passengers","%d",&passengers,1)
ask("How many adults","%d",&adults,0)
ask("How many children","%d",&children,0)
}
}
isnŽt the second one much easier to understand?
disclaimer: no responsibility. this code is untested and probably needs modification before it compiles. sorry for the long post 
|

May 21st, 2002, 02:50 PM
|
|
Junior Member
|
|
Join Date: May 2002
Posts: 12
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
thanks
thanks for replying i have found your examples helpful.
When i start coding im unsure as to how i should start of this program.
How will i get the numbers 1- 20 to produce a street name when a stop number is entered ?
how will i get the program it pause for 15 secsonds ?
|

May 21st, 2002, 02:55 PM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
for the numbers, this is called a list or an array.
in c they work like this:
Code:
char *station[20];
int init() {
station[0]="Sesame Street";
station[1]="Elm Street";
station[2]="Downing Street";
... and so on
}
int main() {
int nr;
printf("Which station number?");
scanf("%d",nr);
printf("Station %d is called: %s.\n",nr,station[nr-1]);
}
the -1 is because c arrays always start with 0. so station[0] to station[19] will be used.
this would be a good use for a function (input nr, output station name)
pausing is afaik done using sleep(milliseconds), but could depend on your os (not sure).
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|