array of struct, searching for a certain char value

Hi there,

following code is about adding vehicle details(ID,number,category, brand,price etc.) to a menu and displaying entered details,searching for a particular vehicle based on either vehicle number or category and deleting a record of sold vehicle.

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
#include<iostream>

using namespace std;

struct vehicles 

{

char vehicalid[8];
char vehicalnumber[8];
char category[10];
char brand[20];
int price;


} data[100];

int main ()
{
int count=0;
char* point[8];
char num[8];
int option;
int b=0;	
int a;
int n=0;
	cout<<"option (1) add vehicle details \n";
	cout<<"option (2) view view details \n";
	cout<<"option (3) search for a vehicle \n";
	cout<<"option (4) delete vehicle details \n"<<endl;
	

loop:
	cout<<"option 01 \n"<<endl;
  
cout<<"enter vehicle ID \t";
cin>>data[n].vehicalid;
cout<<"enter vehicle number  \t";
cin>>data[n].vehicalnumber ;
cout<<"enter vehicle category \t";
cin>>data[n].category;
cout<<"enter brand of the vehicle \t";
cin>>data[n].brand;
cout<<"enter price of the vehicle \t";
cin>>data[n].price;



cout<<" Emter your option to continue  \t";
cin>>a;
n++;
if(a==1) goto loop;

switch (a){
case 2:
	cout<<n;
	cout<<endl;
	cout<<"option 2 \n"<<'\n';

	
	cout<<"ID \t"<<"Number \t"<<"category \t"<<"brand \t"<<"price \t"<<endl;
	while(b<n){
		cout<<data[b].vehicalid<<'\t'<<data[b].vehicalnumber<<'\t'<<'\t'<<data[b].category<<'\t'<<data[b].brand <<'\t'<<data[b].price<<endl;
		b++;
	}
	cout<<" Emter your option to continue  \t";
cin>>a;

if(a==1) goto loop;


	
		break;
case 3:
	cout<<"option (2) \n";
	cout<<"search for a vehicle \n";
	cout<<"option (1) by number \n";
	cout<<"option (2) by category \n";
	cin>>option;

	if(option==1)
		cout<<"enter the number \t";
	    cin>>num;

*point=data[b].vehicalnumber;
		while (*point!=num){
			count=count+1;
			
		b++;
		}
		cout<<data[count].vehicalid <<'\t'<<data[count].vehicalnumber<<'\t'<<data[count].category<<'\t'<<data[count].brand<<'\t'<<data[count].price<<endl;
	
cout<<" Emter your option to continue  \t";
cin>>a;

if(a==1) goto loop;
	
	break;
default:
	cout<<"default not done yet";
}








system("pause");
return 0;
}
																		


i 'm stuck at searching for vehicle details by vehicle id
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
case 3:
	cout<<"option (2) \n";
	cout<<"search for a vehicle \n";
	cout<<"option (1) by number \n";
	cout<<"option (2) by category \n";
	cin>>option;

	if(option==1)
		cout<<"enter the number \t";
	    cin>>num;

*point=data[b].vehicalnumber;
		while (*point!=num){
			count=count+1;
			
		b++;
		}
		cout<<data[count].vehicalid <<'\t'<<data[count].vehicalnumber<<'\t'<<data[count].category<<'\t'<<data[count].brand<<'\t'<<data[count].price<<endl;





Last edited on
1
2
3
for(int i = 0; i < NUMBER_OF_VEHICLES; i++){
    if(strcmp(data[i].vehicalnumber, num)==0) break;
}
now data[i] is the car you want!

Other notes.
Why would you make wehicalnumber a string? the label says NUMBER...
char* point[8]; This creates eight char pointers, while you only need one. (in fact you don't need any at all)
Don't use goto. At least try to avoid them. They make you program harder to understand and debug. There is absolutely no reason why you couldn't use a loop here.
Use std libraries. <string> and <vector> would be useful here.
Try to split your code into functions. Don't put everything into main. Eventually your code will become a mess otherwise.
Declare your variables where you use them. That way it is easier to know what is their type.
Don't system("pause"). See this http://www.cplusplus.com/forum/articles/11153/
Topic archived. No new replies allowed.