Click here to Skip to main content
14,691,457 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi there

i'm new to c++ and i hv been trying to make a little program
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.

following is the answer i could come up with


#include <iostream>
#include <string>

using namespace std;

struct vehicles

{

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


} data[100];

int main ()
{
int d=0;
int i=0;
int op=0;
int n=0;
int b=0;
char num[8];


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;

cout<<"option 01 \n"<<endl;

cout<<"enter your Option \n";
cin>>op;
loop:

while (op==1){

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;

n++;

cout<<"Enter Your Option to continue \t";
cin>>op;

if(op!=1)goto loop;

}

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++;
}
cout<<"Enter your option to continue \t";
cin>>op;
if(op!=2)goto loop;

}
while (op==3){
cout<<"Option (3) \n";

cout<<"enter vehicle number \n";
cin>>num;

while (i<n){
i++;
if(strcmp(data.vehicalnumber, num)==0) break;
}


cout<<"search resutl !! \n";
cout<<"vehicle id\t"<<"Number\t"<<"Category\t"<<"Brand\t"<<"price\n";
cout<<data.vehicalid<<'\t'<<data.vehicalnumber<<'\t'<<data.category<<'\t'<<data.brand<<'\t'<<data.price<<'\n';
cout<<"Enter your option to continue \t";
cin>>op;
if(op!=3)goto loop;

}



return 0;
}</pre>


i dont hv any idea how to delete details of a particular vehicle

and when first add vehicle details and go on to the second option to show detail it works fine but again if i try to add details of a vehicle and to see the details of all vehicle entered so far ,only latest details entered is shown

if someone can help me on this that would be really helpful
Posted

Navaneeth - he's almost certainly doing homework and is expected to learn how arrays work, not to move on to dynamic containers.
   
agskanchana wrote:
while (op==1){



agskanchana wrote:
if(op!=2)goto loop;


This is really messy. You should just have one input for op, a switch statement, and no goto statements. I'd break the seperate things you do off in to methods.

If you structure the code a little better, you might find it easier to debug.


agskanchana wrote:
data[100];


I did not know that this worked, but, your obvious issue is that you've used the memory for 100 items, if you have 1 or 100, and you cannot have 101. That's fine for a learning exercise, but eventually, you will not use this sort of code, you will use a dynamic container. In the meantime, the way you can delete an item, is to copy all your items up and decrement your counter. So if you have 75 items, n will equal 75. To delete item number 50, you need to copy items 51-75 down to their previous index, if order matters, or copy item 75 over item 50, if the order does not matter, then decrement your counter, erasing the last item. As your list is sequential, you delete an item by moving it to the end, and then moving the end, as the end is all you can move.
   
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.
   
v2
agskanchana wrote:
data[100];


Use containers like std::vector. It has got methods for inserting and removing items. It can expand automatically when you add new items. Also work with the std::string and avoid using character arrays.
   
cin>>op;
if(op!=3)goto loop;
}

return 0;



to changge
cin&gt;&gt;op;
else if {
cout<<"option (4) delete vehicle details \n"<<endl;}
return 0;
it is my opinions :rolleyes:
   
i totally agree with cristian.. first u should make you code proper..

one method to delete the element is you can simply overwrite it.
ex:
(i am not using the names that u r using for ur code you can just have
a reference from this ex)

enter the element to be deleted
cin>> elemid;

short storecount;
for(short i=0; i<maxsize_of_struct; i++)
{
database[i].vehicleid == elemid;
storecount = i;
}

//nw write another loop to overwrite
for(short i=storecount 0; i<maxsize_of_struct-1; i++)
{
database[i] == database[i+1]elemid;
}
database[i]= 0;//its a last element



hope it will solve your problem
   
v2
Why not try to use the ony way linked list in the list you just need to insert or delete one node can achieve your request, you may need to see when the cycle of get what you want :laugh:
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900