How to reuse menu

How can I reuse display function to use it for entering the destination?



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
#include <iostream>
using namespace std;


void fn()
{
//Contains user input 
}

void display()
{
//This contains a menu display

int ch;
//display name of cities choose
cout<<"Choose your option"<<endl;
cin>>ch;

switch(ch)
{
case 1:
//if he pick 1 it will show available locations in that city
fn()//fn function is used to get the input of users choice  of starting location;
break;

case 2:..

case 3:...


return 0;
}

It's unclear from what you posted, what function fn() is supposed to do.

You can't reuse display() as it exists, your switch statement would get reexecuted. It would also cause recursion, which I don't think you want to deal with.

I would suggest moving lines 14-17 to a new function.
1
2
3
4
5
6
int get_user_choice ()
{  int ch;
    cout<<"Choose your option"<<endl;
    //  do some edits here if you want
    return ch;
}

Also move line 15 to after line 21.
1
2
3
4
case 1:  display_list_of_cities ();
             city = get_user_input ();  //  always returns with a valid city
             fn (city);
             break;






Topic archived. No new replies allowed.