Any Hint How I Solve This....

Write a C++ program to define a class Metro with the following specifications :
 Metro No
 Metro Name
 No of Seats
 Starting point
 Destination
Write a menu driven program by using appropriate manipulators to
a. Accept details of n metros.
b. Display all metro details.
c. Display details of metro from specified starting and ending destination by user.



So This Is the Question That I Am Trying to Solve and I Have Written the following code for it:
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
   1 
 
#include<iostream>
 2 
 
#include<string.h>
 3 
 
#include<stdio.h>
 4 
 
using namespace std;
 5 
 
 6 
 
int i,n;
 7 
 
 8 
 
class Metro
 9 
 
{
 10 
 
    int mno,No_of_Seats;
 11 
 
    char mname[20],Starting_point[20],Destination[20];
 12 
 
public:
 13 
 
    void accept()
 14 
 
    {
 15 
 
        cout<"Enter Metro Details:";
 16 
 
        cout<<"Metro No.=";
 17 
 
        cin>>mno;
 18 
 
        cout<<"Metro Name=";
 19 
 
        cin>>mname;
 20 
 
        cout<<"No_of_Seats=";
 21 
 
        cin>>No_of_Seats;
 22 
 
        cout<<"Starting point=";
 23 
 
        cin>>Starting_point;
 24 
 
        cout<<"Destination=";
 25 
 
        cin>>Destination;
 26 
 
        cout<<"\n";
 27 
 
    }
 28 
 
 29 
 
    void display()
 30 
 
    {
 31 
 
        cout<<"Metro No.=";
 32 
 
        cout<<mno;
 33 
 
        cout<<"\nMetro Name=";
 34 
 
        cout<<mname;
 35 
 
        cout<<"\nNo_of_Seats=";
 36 
 
        cout<<No_of_Seats;
 37 
 
        cout<<"\nStarting point=";
 38 
 
        cout<<Starting_point;
 39 
 
        cout<<"\nDestination=";
 40 
 
        cout<<Destination;
 41 
 
        cout<<"\n";
 42 
 
    }
 43 
 
 44 
 
    void metrosearch(Metro m[],int n)
 45 
 
    {
 46 
 
        char a[20],b[20];
 47 
 
        cout<<"Enter Starting Point:";
 48 
 
        cin>>a;
 49 
 
        cout<<"Enter Destination:";
 50 
 
        cin>>b;
 51 
 
        for(i=0;i<=n;i++)
 52 
 
        {
 53 
 
            if(a==m[i].Starting_point && b==m[i].Destination)
 54 
 
            {
 55 
 
                m[i].display();
 56 
 
            }
 57 
 
        }
 58 
 
    }
 59 
 
};
 60 
 
 61 
 
int main()
 62 
 
{
 63 
 
    Metro m[10];
 64 
 
    cout<<"Enter Of No. Of Details To Be Accept:";
 65 
 
    cin>>n;
 66 
 
    cout<<"---MENU---\n 1.Accept \n 2.Display \n 3.Display details of metro from specified starting and ending destination by user \n 4.Exit:";
 67 
 
    cout<<"Enter Your Choice:";
 68 
 
    int ch;
 69 
 
    cin>>ch;
 70 
 
    if(ch>=1 && ch<=4)
 71 
 
    {
 72 
 
        switch(ch)
 73 
 
        {
 74 
 
        case 1:
 75 
 
            for(i=1;i<=n;i++)
 76 
 
            {
 77 
 
                m[i].accept();
 78 
 
            }
 79 
 
        case 2:
 80 
 
            for(i=1;i<=n;i++)
 81 
 
            {
 82 
 
                m[i].display();
 83 
 
            }
 84 
 
        case 3:
 85 
 
            m[i].metrosearch(m,n);
 86 
 
        case 4:
 87 
 
            break;
 88 
 
        }
 89 
 
    }
 90 
 
    else
 91 
 
    {
 92 
 
        cout<<"Invalid Choice!!";
 93 
 
    }
 94 
 
}
 95 

In The Above
code I can accept the details and display the details but can solve the 3rd problem

"Sorry For My Bad English"
Last edited on
You have:
1
2
3
4
5
6
7
8
9
#include <iostream>

int main() {
  char a[] = "dog";
  char b[] = "dog";
  if ( a == b ) {
    std::cout << "a == b\n"
  }
}

That does not compare C-strings. That compares two pointers. They are not equal.

http://www.cplusplus.com/reference/cstring/strcmp/
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <cstring>

int main() {
  char a[] = "dog";
  char b[] = "dog";
  if ( strcmp( a, b ) == 0 ) {
    std::cout << "a == b\n"
  }
}


Or better yet:
http://www.cplusplus.com/reference/string/string/operators/
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>

int main() {
  std::string a = "dog";
  std::string b = "dog";
  if ( a == b ) {
    std::cout << "a == b\n"
  }
}

I don’t think the metrosearch() function should be a method of the class Metro.
I think it should be a standalone function.
So I just Changed metrosearch Code To This
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void metrosearch(Metro m[],int n)
    {
        char a[20],b[20];
        cout<<"Enter Starting Point";
        cin>>a;
        cout<<"Enter Destination";
        cin>>b;
        for(i=0;i=n;i++)
        {
            if((strcmp(a,m[i].Starting_point)==0) && (strcmp(b,m[i].Destination)==0))
            {
                cout<<"Found";
            }
            else
            {
                coutNot<<"Not Found";
            }
        }
    }

But still No Output
Output-Not foundfoundNot found


Sorry For My Bad English
Last edited on
Hi, noobcoder9867.
Your English isn’t a problem – my English is poor as well –, but it seems you’re not giving us your original code. I can’t believe you can compile a code like:
1
2
3
4
coutEnter Starting Point;
cina;
coutEnter Destination;
cinb;

And there’re similar syntax errors in your first post too.

This make pointlessly hard to help you, but I think I can spot at least one issue:
1
2
3
4
5
6
7
8
9
10
11
class Metro
{
    int mno,No_of_Seats;
    char mname[20],Starting_point[20],Destination[20];
    // ...
};

void metrosearch(Metro m[],int n)
{
    // ...
    if((strcmp(a,m[i].Starting_point)==0) && (strcmp(b,m[i].Destination)==0))

Starting_point and Destination are class Metro private properties.
You can’t access them directly from outside their class.
Once you’ve made metrosearch() a standalone function, you should also provide methods to access those variables.
@Enoizat
Thankx For Your Help But I have solved The Code Without using it as a standalone function
Here Is My Code
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
92
93
94
95
96
97
98
99
100
101
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;

int i,n,pos;

class Metro
{
    int mno,No_of_Seats;
    char mname[20],Starting_point[20],Destination[20];
public:
    void accept()
    {
        cout<<"Enter Metro Details:";
        cout<<"Metro No.=";
        cin>>mno;
        cout<<"Metro Name=";
        cin>>mname;
        cout<<"No_of_Seats=";
        cin>>No_of_Seats;
        cout<<"Starting point=";
        cin>>Starting_point;
        cout<<"Destination=";
        cin>>Destination;
        cout<<"\n";
        system("cls");
    }

    void display()
    {
        cout<<"Details Are:\n";
        cout<<"Metro No.=";
        cout<<mno;
        cout<<"\nMetro Name=";
        cout<<mname;
        cout<<"\nNo_of_Seats=";
        cout<<No_of_Seats;
        cout<<"\nStarting point=";
        cout<<Starting_point;
        cout<<"\nDestination=";
        cout<<Destination;
        cout<<"\n\n\n";
    }

    void metrosearch(Metro m[],int n)
    {
        char a[20],b[20];
        cout<<"Enter Starting Point:";
        cin>>a;
        cout<<"Enter Destination:";
        cin>>b;
        for(i=0;i<n;i++)
        {
            if((strcmp(a,m[i].Starting_point)==0) && (strcmp(b,m[i].Destination)==0))
            {
                cout<<"Details Are Found"<<endl;
                pos=i;
                cout<<"\n\n";
                m[pos].display();
            }
        }
    }
};

int main()
{
    Metro m[10];
    cout<<"Enter Of No. Of Details To Be Accept:";
    cin>>n;
    cout<<"---MENU---\n 1.Accept \n 2.Display \n 3.Display details of metro from specified starting and ending destination by user \n 4.Exit:";
    cout<<"Enter Your Choice:";
    int ch;
    cin>>ch;
    if(ch>=1 && ch<=4)
    {
        switch(ch)
        {
        case 1:
            for(i=0;i<n;i++)
            {
                m[i].accept();
            }
        case 2:
            for(i=0;i<n;i++)
            {
                m[i].display();
            }
        case 3:
            m[i].metrosearch(m,n);
        case 4:
            break;
        }
    }
    else
    {
        cout<<"Invalid Choice!!";
    }
}

And About The Syntax Error that You mentioned:
1
2
3
4
coutEnter Starting Point;
cina;
coutEnter Destination;
cinb;

Was A copying Mistake
and There Are No similar syntax errors in my first post.
Thankx For Your Help Again
Last edited on
You have an issue in your code:
1
2
3
4
5
6
7
8
9
10
int i;
Metro m[10];

for ( i=0; i<n; i++ )
{
}
// i==10 after the loop

m[i].foo(); // same as m[10].foo()
// m[10] is out-of-range 
You welcome, noobcoder9867.
But…
But I have solved The Code Without using it as a standalone function

I’m afraid your code doesn’t compile (besides what keskiverto has already pointed out).
http://coliru.stacked-crooked.com/a/42f0221110867e78

And About The Syntax Error that You mentioned […] Was A copying Mistake

Yes, but computers usually don’t make errors in copy&pasting, that’s why I presumed you had not posted your original code.

and There Are No similar syntax errors in my first post.

Was that too a harsh critic? Anyway, here you are:
 
cout<"Enter Metro Details:";


If you’re happy with your solution, stick to it.
If you want to compare your solution with different ones, just ask.

Happy coding!
@Enoizat
I Am Sorry About The Syntax Error, Cause When I Try To Compile It (Using IDE: Code Blocks) There Were No Errors And I Have Changed My Code:
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
92
93
94
95
96
97
98
99
100
101
102
103
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;

int i,n,pos;

class Metro
{
    int mno,No_of_Seats;
    char mname[20],Starting_point[20],Destination[20];
public:
    void accept()
    {
        cout<<"Enter Metro Details:";
        cout<<"\nMetro No.=";
        cin>>mno;
        cout<<"Metro Name=";
        cin>>mname;
        cout<<"No_of_Seats=";
        cin>>No_of_Seats;
        cout<<"Starting point=";
        cin>>Starting_point;
        cout<<"Destination=";
        cin>>Destination;
        cout<<"\n";
        system("cls");
    }

    void display()
    {
        cout<<"Details Are:\n";
        cout<<"Metro No.=";
        cout<<mno;
        cout<<"\nMetro Name=";
        cout<<mname;
        cout<<"\nNo_of_Seats=";
        cout<<No_of_Seats;
        cout<<"\nStarting point=";
        cout<<Starting_point;
        cout<<"\nDestination=";
        cout<<Destination;
        cout<<"\n\n\n";
    }

    void metrosearch(Metro m[],int n)
    {
        char a[20],b[20];
        cout<<"Enter Starting Point:";
        cin>>a;
        cout<<"Enter Destination:";
        cin>>b;
        for(i=0;i<n;i++)
        {
            if((strcmp(a,m[i].Starting_point)==0) && (strcmp(b,m[i].Destination)==0))
            {
                cout<<"Details Are Found"<<endl;
                pos=i;
                cout<<"\n\n";
                m[pos].display();
            }
        }
    }
};

int main()
{
    Metro m[10];
    cout<<"Enter Of No. Of Details To Be Accept:";
    cin>>n;
    while(1)
    {
    	cout<<"---MENU---\n 1.Accept \n 2.Display \n 3.Display details of metro from specified starting and ending destination by user \n 4.Exit:";
    	cout<<"\n\nEnter Your Choice:";
    	int ch;
    	cin>>ch;
        	switch(ch)
        	{
        	case 1:
            	for(i=0;i<n;i++)
            	{
                	m[i].accept();
            	}
            	break;
        	case 2:
            	for(i=0;i<n;i++)
            	{
                	m[i].display();
           		}
           		break;
        	case 3:
            		m[i].metrosearch(m,n);
            		break;
        	case 4:
        		exit(1);
            	break;
            default:
            		cout<<"Invalid Choice!!";
            		break;
        	}
   	}
}

And Yes This Is My Original Code

Feel Free To Give Me More Suggestions

I suggest reading the assignment:
Write a menu driven program by using appropriate manipulators to

Even if there’s no a standard definition of manipulators (*), I think your teacher is asking about the getters and setters methods.
For what I can see, there are no ‘manipulators’ in in you class — and I’m totally fine with that, because you don’t need them.
But it sounds as if your teacher wants them, doesn’t it?

If you tried to access your private members from a standalone function, you would be forced to add them.
Is it possible that’s what your teacher wants?

Please consider: your Metro class doesn’t need to be aware of a C-style array of Metros, like an int variable doesn’t need to know it is contained in a int myarr[].

This is how I would develop that code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Metro
{
    // ...
    const char * getStartingPoint() const;  // define this method
    const char * getDestination() const;    // define this method
    // ...
};


// Return an int as position for the C-style Metro array in main():
int findMetroInCArray(const Metro * const lines, int size)
{
    char start[20]
       , dest[20];
    std::cout << "Enter starting station: ";
    std::cin >> start;
    std::cout << "Enter destination: ";
    std::cin >> dest;
    for (int i {}; i <= size; ++i) {
        // compare your private properties to 'start' and 'dest'
        // by getters here
}



- - -
(*) Actually the term manipulators is well defined in C++, but for streams:
http://www.cplusplus.com/reference/library/manipulators/



Topic archived. No new replies allowed.