Need assistance with assignment(creating menus,Arrays)

Hi all, I have only studied C++ for 8weeks so I am still a very "new" beginner, I know only very basic C++codes such as float int and I have only learnt <stdio.h>. I have an assignment due in a couple of weeks and it is about a travel time viewer from my school to home of four different buses. What I need to do is to create a program such that after I input the bus number and the time period(From 1-6:30pm with half-hour intervals)of the bus I would add the bus waiting time and the journey home time.These are the options I wish to add.

First I need to make a menu which I am not sure if I have done properly, please help.
Secondly, I want to create an array such that after I input the bus number(which is shown on a bus list)followed by the time period I would be able to get the addition of the column.(See database for bus timings)
Thirdly,I want to get the average travel time of all 4 buses individually.
In addition,I need to display the longest and shortest travel time for all time periods, if there a formula or a loop that can detect the longest travel time or the shortest for all 4 buses individually?
Also,I would like to know after I choose a time period can I display the output of the longest journey time of all 4 buses individually?
Next, similar to the previous option I want to display the time period and allow the user to choose the time period and display the shortest time home.
Lastly, I want to create a return button for every "page" on my program so that the user may return to the original page in case the user may have accidentally pressed something wrong.

Thank You for your attention and help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
My Menu code
  #include <stdio.h>

void menu();
void busMenu();
void timePeriod();

int main()
{
	printf("Welcome To The Bus Travel Viewer.\n");
	printf("Press the RETURN key to continue....\n");
	getchar();
	menu();

	return 0;
}
void menu()
{
	char menu1choice;
	char menu2choice;
	char menu3choice;
	char menu4choice;
	char menu5choice;

	 
	    printf("Bus Travel Viewer.\n");
	    printf("A:Buslist.\n");
	    printf("B:Display Daily Average Travel Time.\n");
		printf("C:Display Predicted shortest travel time.\n");
	    printf("D:Display Longest/Shortest travel time for all time periods.\n");
		printf("E:Display The Longest Time Taken To Reach Home.\n");
		printf("Please select A,B,C,D or E.\n");
		printf("Press the RETURN key to exit program.\n");
	    scanf("%c", &menu1choice);
	    switch ( menu1choice )
		{	    
			case 'A':
			case'a':
			{
			char selection;
			int time, timeHome;
	        printf("Please select your bus number from the following.\n");
			printf("A:15.\n");
			printf("B:27.\n");
			printf("C:45.\n");
			printf("D:46.\n");
			scanf("%c,", &selection);
			switch (selection)
			{
				case'A':
				case'a':
						printf("Please enter the current time period from 1pm-6:30pm with half-hour intervals.\n");
						scanf("%d",&time);
						timeHome=bus15Timings

			default:printf("Sorry, you have input a wrong key.\n");
				 

			scanf("%c,", &selection);

			
				
	        break;
			}
	 
	    }
}
This is my database for my bus timings.
#include <stdio.h>

void main(void)

int bus15Timings [3][12] = {		{1:00,1:30,2:00,2:30,3:00,3:30,4:00,4:30,5:00,5:30,6:00,6:30},//Time Period
									{5,5,6,6,7,7,5,5,4,4,8,8},//bus15 waiting time	
									{8,8,9,9,7,7,7,8,8,20,20,20}//bus15 time taken to reach home
							};	

int bus27Timings [3][12] = {		{1:00,1:30,2:00,2:30,3:00,3:30,4:00,4:30,5:00,5:30,6:00,6:30},//Timw Period
									{8,8,7,7,6,6,4,4,6,6,5},//bus27 waiting time
									{10,10,8,8,8,15,15,7,7,20,20,20}//bus27 time taken to reach home
							};

int bus45Timings [3][12] = {		{1:00,1:30,2:00,2:30,3:00,3:30,4:00,4:30,5:00,5:30,6:00,6:30},//Time Period
									{10,10,8,8,10,12,9,6,7,4,4,3},//bus45 waiting time
									{5,5,8,8,8,10,10,12,12,20,20,20}//bus45 time taken to reach home
							};

int bus46Timings [3][12] = {		{1:00,1:30,2:00,2:30,3:00,3:30,4:00,4:30,5:00,5:30,6:00,6:30},//Time Period
									{12,8,8,5,5,3,2,5,8,10,6,5},//bus46 waiting time
									{10,15,15,11,11,9,9,7,7,20,20,20}//bus46 time taken to reach home
							};
Have you even tried to compile this? You have many syntax errors.

Line 53: References bus15Timings has not been seen yet by the compiler and is therefore unbdefined. Statement is incomplete. Needs subscripts and a ; Lines 73-93 should go at the top of your program.

Line 68: Comments need //

Line 71: What is this? Is this a function prototype? If so, it needs a ; If it's a function definition, it needs a body. main should always return an int.
You already have a main() at line 8.

Lines 73,78,83,88: These are int arrays. 1:00 1:30 etc are not valid ints.
You might want to use a float to represent the time period.

Have you covered structs yet? If you have, your timing information would be better suited to a struct such as:
1
2
3
4
5
6
7
8
9
10
struct BusTiming
{  float	period;
   int		wait_time;
   int		time_home;
};

BusTiming bus15 = { 1.0, 5, 8,    // 1:00 as a float
                    1.5, 5, 8, 
                    // etc
                  };


edit:
With regard to your questions:
1) I would break your menu into two separate functions. One to get the menu choice. The second to get bus number.

2) It's going to be awkward dealing with 4 separate arrays (one for each bus). Ideally, you want an array of BusTiming structs.

3) You're going to need to compute the average travel time for each route. Simply iterate through the structs and add up the time_home and divide by the number of busses for that route (12).

4) For longest and shortest travel times, again iterate through each array and find the largest and smallest time_home.

5) Iterate through the routes finding the smallest time_home for each route. Hint: uses the smallest time_home from each route.
Last edited on
Hi, thank you for your quick reply. Now i know that I do not have to add the sum of one column in an array, Since my program doesn't require a option for how long the bus takes to come(wait_time), it made things a lot easier for me thank you.
What I want to clarify:
1) How do I float my time period with float period; ?
I have specific values for them from {1:00,1:30,2:00,2:30,3:00,3:30,4:00,4:30,5:00,5:30,6:00,6:30}//Time Period
and why doesn't the C++ recognize the ":" symbol in 1:00 despite floating it? how do I solve it?
2)This is my new database
#include <stdio.h>
void main(void)
float timePeriod;


int bus15Timings [3][12] = {

{1:00,1:30,2:00,2:30,3:00,3:30,4:00,4:30,5:00,5:30,6:00,6:30},//Time Period
{13,13,15,15,14,14,12,13,12,24,28,28}//Total time taken to reach home by bus 15
};

int bus27Timings [3][12] = { {1:00,1:30,2:00,2:30,3:00,3:30,4:00,4:30,5:00,5:30,6:00,6:30},//Time Period
{18,18,15,15,14,21,19,11,13,26,25,25}//Total time taken to reach home by bus 27
};

int bus45Timings [3][12] = { {1:00,1:30,2:00,2:30,3:00,3:30,4:00,4:30,5:00,5:30,6:00,6:30},//Time Period
{15,15,16,16,18,22,19,18,19,24,24,23}//Total time taken to reach home by bus 45
};

int bus46Timings [3][12] = { {1:00,1:30,2:00,2:30,3:00,3:30,4:00,4:30,5:00,5:30,6:00,6:30},//Time Period
{22,23,23,16,16,12,11,12,15,20,26,25}//Total Time taken to reach home by bus 46
};
Is it possible to simply enter the time_period and the program would be able to identify the allocated time period and printf the value at its respective position?

3)Would it be possible if you could show me how I do menus with options followed by sub-options? I am still very unclear on how it works.

Thank You.

You didn't answer my question about whether you have learned structs or not.

1) I gave you an example of using a float within a struct. If you haven't learned structs yet, then you will need to make an array of float variables.
1
2
3
 
float bus_period[12] = { 1.0, 1.5 /* etc */ };
//  Since all bus periods are the same, you only need one copy of this array. 


why doesn't the C++ recognize the ":" symbol in 1:00 despite floating it?

Because : is not a decimal point. For simplicity, I chose to represent times as decimal hours (floats). ie. 1:00 is 1.0. You might want to use a 24 hour clock to distinguish am from pm. Another approach is to represent the hour and minute using separate ints. Or you could represent the time as a string.
1
2
string bus_period[12] = { "1:00", "1:30", /* etc */ };
//  Note that the times are quoted strings 

You might be able to recognize a time separator of : using locales, but I think that is beyond where you are right now.

Is it possible to simply enter the time_period and the program would be able to identify the allocated time period

Yes. Search the bus_period array (whether float or string) for a match.

3) Will post back with some examples.

edit:
If you store the time as floats and then choose to output the floats, you'll have to deal with converting the fractional part of an hour to minutes.

My suggestion above to use strings assumes you've covered C++ strings. Since your code is purely C code, that's probably not the case. You could still use C-strings.
 
const char * bus_period[12] = { "1:00", "1:30", /* etc */ };


Last edited on
Here's an example of a couple of menu functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>

bool top_level_menu();
int SelectBus ();
void DisplayAverageTravelTime ();

int main()
{	printf("Welcome To The Bus Travel Viewer.\n");
	while (top_level_menu())
		;
	return 0;
}

//	Ask the user for a bus number.  
//	Ensure that it is valid.
int SelectBus()
{	const int bus_number[4] = { 15, 27, 45, 46 }; 
	int bus;

	printf("Please enter your bus number from the following.\n");
	for (int i=0; i<4; i++)
		printf ("\t%d\n", bus_number[i]);	
	while (true)
	{	scanf("%d,", &bus);
		for (int i=0; i<4; i++)
			if (bus == bus_number[i])
				return bus;		//	Valid bus number
		printf ("Not a valid bus number.  Please reenter.\n");
	}
}

bool top_level_menu()
{	int menuchoice;
	int bus;

	printf("1. Display Buslist.\n");
	printf("2. Display Daily Average Travel Time.\n");
	printf("3. Display Predicted shortest travel time.\n");
    printf("4. Display Longest/Shortest travel time for all time periods.\n");
	printf("5. Display The Longest Time Taken To Reach Home.\n");
	printf("6. Exit program\n");	
	while (true)
	{	printf("Please select 1-6.\n");
		scanf("%d", &menuchoice);
		switch ( menuchoice )
		{	case 1:
				bus = SelectBus ();
				break;
			case 2:
				DisplayAverageTravelTime();		
				break;
			//	etc. for cases 3-5
			case 6:
				return false;	//	Tell main to exit
			default:
				printf ("Not a valid choice\n");
				continue;		//	back to top of loop
		}
		return true;	// main will call top_level_menu again
    }
}

Note that at lines 47 and 50, each case of the switch statement has it's own function call. This is much cleaner that trying to nest switch statements and makes the code much easier to read. If a nested switch statement is needed in one of the cases, it can be included in the respective function.
Last edited on
Topic archived. No new replies allowed.