Wow...you all can't seem to tell the level of the people you are talking to. It's obvious that you're in a basic C++ class and are doing homework regarding arrays. If not, well, then you should find that C++ class. And I take it you haven't gotten to
For
loops yet.
I'm going to try to rephrase some of the code...tell me if I'm wrong.
Your vehicle information is stored in the variable
data
which has 100 vehicles possible.
n
stores the current number of vehicles that have been input.
op
is the user input.
b
is used as a counter when displaying the vehicle information.
num
stores the input when a user is searching for a vehicle. I'm assuming
d
would be for indicating which vehicle to delete.
i
is another counter you use when searching.
Some basic problems: Before you use a counter, make sure you reset it first.
while (op==2){
cout<<"Option (2) \n";
while(b<n){
cout<<data.vehicalid<<'\t'<<data.vehicalnumber<<'\t'<<'\t'<<data.category<<'\t'<<data.brand <<'\t'<<data.price<<endl;
b++;
}
This will only work the first time, because
b
never gets reset. Same with your search.
Problem 2: With arrays, you need to make sure to identify which item in the array you're dealing with.
if(strcmp(data.vehicalnumber, num)==0) break;
should be
if(strcmp(data[i].vehicalnumber, num)==0) break;
and
cout<<data.vehicalid<<'\t'<<data.vehicalnumber<<'\t'<<data.category
should be
cout<<data[b].vehicalid<<'\t'<<data[b].vehicalnumber<<'\t'<<data[b].category
and same with the first excerpt I pulled out.
To delete an item, you need to copy all of the items beyond it down one. So, let's say you found the vehicle ID that matches the one you want to delete. And, let's say you've set
d
to that index. Now:
while (d<n) {
data[d].vehicalid = data[d+1].vehicalid; 'by the way, you spelled vehicle wrong
'copy the other values
d++;
}
Then, make sure to decrement
n
(
n--;
)
Seriously people...you need to understand your audience. This person may not have dealt with separate functions yet, switch statements, or for/next loops.