Hi all,
I am a beginner at C++ so please do bear with me if I do not understand your answers.
Firstly, I am making a simple program for checking how long my bus takes to reach home. This would be my database for my 4 buses. I think its a 2-deminesion array.
1.I want to know how I can only allow the user to 4 buses, I cannot int or char to declare the bus numbers(15,27,45,46) I have to put a "num" in front of it and I also want to know how I can declare my time input. In order for me to complete my program the user has to input the bus no. and the time period(which is every 1/2 hour e.g 1:00 or 6:30). Also, after I've declared both time period and the bus no. how can I code the program such that when the user inputs a bus no. and a time period the program will display the shown integer?
1 2 3 4 5 6 7 8 9
|
//Database for all timings for the 4 buses.
#include <stdio.h>
int timePeriod[={1:00,1:30,2:00,2:30,3:00,3:30,4:00,4:30,5:00,5:30,6:00,6:30,7:00};
int bus15Timing[]={13,13,15,15,14,14,12,13,12,24,28,28,28};
int bus27Timing[]={18,18,15,15,14,21,19,11,13,26,25,25,25};
int bus45Timing[]={15,15,16,16,18,22,19,18,19,24,24,23,25};
int bus46Timing[]={22,23,23,16,16,12,11,12,15,30,26,25,25};
|
Next
I need to display the shortest and longest time taken to reach home for all time periods and buses. how do I do so?
Overall,
I just want to know how to declare my bus numbers and time periods so that the user will enter an invalid input an cause my program to fail and also how to get
the largest and smallest int in an array and display like the shortest/longest time for each specific bus.
Thank You for your time.
This is what my menu looks like.
[code}
#include <stdio.h>
void main (void)
{
extern int bus15Timing[], bus27Timing[], bus45Timing[], bus46Timing[];//declare as external global variable(All Options)
int num15,num27,num45,num46;//declare bus numbers(Option 1)
int averagetime;//Variable for Option B(done)
char selection;//Options 1,2,3,4,5,6
printf("=================================Bus Travel Viewer=============================\n");
printf("1:Display Buslist.\n");
printf("2:Display the daily average travel time.\n");
printf("3:Display predicted shortest travel time.\n");
printf("4:Display the longest/shortest travel time for all time periods.\n");
printf("5:Display the longest time to reach home from 1pm-7pm.\n");
printf("6:Please select Option 1,2,3,4,5,6.\n");
scanf("%c",&selection);
switch (selection)
{
case'1':
{
char num15,num27,num45,num46;//ensure user does not input wrong bus number
printf("The following are the bus numbers 15,17,45,46.\n");
printf("Please type in your bus number.\n");
scanf("%d",&num15,&num27,&num45,&num46);
switch(num15,num27,num45,num46)
case'15':
printf("Please select the time period from 1pm to 7pm with half-hour intervals.\n");
printf("E.g 130=1:30, 600=6:00.\n");
break;
case'27':
printf("Please select the time period from 1pm to 7pm with half-hour intervals.\n");
printf("E.g 130=1:30, 600=6:00.\n");
break;
case'45':
printf("Please select the time period from 1pm to 7pm with half-hour intervals.\n");
printf("E.g 130=1:30, 600=6:00.\n");
break;
case'46':
printf("Please select the time period from 1pm to 7pm with half-hour intervals.\n");
printf("E.g 130=1:30, 600=6:00.\n");
break;
}
break;
case'2':
averagetime=(bus15Timing[0]+bus27Timing[0]+bus45Timing[0]+bus46Timing[0])/4;
printf(" The daily average travel time is %d\nmins",averagetime);
break;
case'3':
printf("Please select the time period from 1pm to 7pm with half-hour intervals.\n");
printf("E.g 130=1:30, 600=6:00.\n");
break;
case'4':;
//Finding the largest/longest time taken to reach home for each bus
default:printf("Sorry , you have entered an invalid key.\n");
}
}
[/code}