|
-
April 26th, 2010, 08:18 AM
#1
Help with my program
Weee almost finish this program but one last thing. Why does this program crash whenever i quit after i do something to the records . Somebody please compile the program and try it and please tell me what is the problem. And guys try deleting the data then view them. Whats the prob??(ignore the #)
#
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct weapon
{
string id;
string name;
string quantity;
};
int choice(weapon add[],int size);
weapon view(weapon add[],int size);
weapon deleting( weapon add[], int size);
weapon modify( weapon add[],int size);
int choice(weapon add[],int size)
{
int again;
int option;
cout<< "1: Delete weapon \n";
cout<< "2: Modify records \n";
cout<< "3: View records\n";
cout<< "4: Exit \n" ;
cin>> option;
switch(option)
{
case 1 eleting(add,size); break;
case 2:modify(add,size);break;
case 3:view( add,size); break;
case 4:again=2; break;
}
cout<<again;
return again;
}
//DELETING FUNCTION
weapon deleting( weapon add[], int size)
{
string weapon_id;
cout<< "Enter the id of teh weapon you wanna delete."<< endl;
cin>> weapon_id;
int b=0;
bool out=true;
//QUERY
int a;
for ( a=0; a<size|| out; a++)
{
if (add[a].id==weapon_id)
{
cout<< setw(15)<< add[a].id<< " "<<add[a].name<< " "<<add[a].quantity<< endl;
cout<< "This is teh weapon you wanna delete at row "<< a << endl;
out=false;
b=a;
}
}
//DELETING DATA
int n;
for( n=b; n<size; n++)
{
if((n+1)<size)
add[n]=add[n+1];
}
choice(add,size);
}
//MODIFYING DATA
weapon modify(weapon add[],int size)
{
string weapon_id;
cout<< "Enter the id of teh weapon you wanna modify."<< endl;
cin>> weapon_id;
int b=0;
bool out=true;
//QUERY
int a;
for ( a=0; a<size|| out; a++)
{
if (add[a].id==weapon_id)
{
cout<< setw(15)<< add[a].id<< " "<<add[a].name<< " "<<add[a].quantity<< endl;
cout<< "This is teh weapon you wanna delete at row "<< a << endl;
out=false;
b=a;
}
}
cout<<"Enter the new id"<< endl;
cin>> add[b].id;
cout<<"Enter the new name"<< endl;
cin>> add[b].name;
cout<<"Enter the new quantity"<< endl;
cin>> add[b].quantity;
choice(add,size);
}
//VIEWING WEAPON
weapon view(weapon add[],int size)
{
cout<<setw(15)<< "Weapon id" <<" "<<setw(15)<<"Weapon_name" <<" " <<setw(15)<< "Weapon Quantity"<< endl;
for(int l=0; l<size;l++)
{
cout<<setw(15)<< add[l].id<<" "<<setw(15)<< add[l].name<<setw(15)<<add[l].quantity<< endl;
}
choice(add,size);
}
int main()
{
cout<< "First, you gotta insert the records first"<< endl;
int i,k,size,l;
i=1;
l=0;
k=1;
cout<< "Enter size of record"<< endl;
cin>> size;
weapon add[ size];
for( l=0; l<size;l++)
{
cout<<"Enter the id"<< endl;
cin>> add[l].id;
cout<<"Enter the name"<< endl;
cin>> add[l].name;
cout<<"Enter the quantity"<< endl;
cin>> add[l].quantity;
}
int again=1;
while (again==1)
{
again=choice(add,size);
}
return 0;
}
#
-
April 26th, 2010, 08:27 AM
#2
Re: Help with my program
Please use code tags and indentation.
The program didn't compile for me. Please provide code that compiles.
Please provide specific instructions to reproduce the crash.
Did you try the debugger?
-
April 26th, 2010, 09:09 AM
#3
Re: Help with my program
1) Your 3 functions: deleting, modify, and view have a return type of weapon ...
but you do not return anything in those functions. Either make the return type
void, or return an object.
2) The following is non-standard in C++ (but is a g++ extension):
Code:
cin>> size;
weapon add[ size];
3) You should not really be calling choice() in your 3 functions ... only main()
should be doing that.
-
April 26th, 2010, 09:55 AM
#4
Re: Help with my program
What is code tag and indentation?The program definitely compile if you remove the '#' at the beginning and ending of the codes.
The program crash whenever i try to quit the application by choosing option 4 after i modify or delete or view the record.
Lastly i dont know how to use the debugger. Is there a tutorial here?
-
April 26th, 2010, 10:00 AM
#5
Re: Help with my program
 Originally Posted by hayloiuy
What is code tag and indentation?The program definitely compile if you remove the '#' at the beginning and ending of the codes.
The program crash whenever i try to quit the application by choosing option 4 after i modify or delete or view the record.
Lastly i dont know how to use the debugger. Is there a tutorial here?
The program doesn't compile using Visual Studio 2005. See Philip's post.
Learn the debugger. You can't program without it. Your IDE should have come with some kind of documentation.
Code Tags are explained in the before you post sticky at the top of the forum.
-
April 26th, 2010, 04:08 PM
#6
Re: Help with my program
 Originally Posted by hayloiuy
The program definitely compile if you remove the '#' at the beginning and ending of the codes.
Add these command line switches if you're using g++ (dev-C++, CodeBlocks IDE's all use this compiler)
-Wall -pedantic
You will see that the program is not valid C++ and will not compile, for exactly the reasons that were pointed out -- a bogus array declaration.
Just a warning -- if this is a school assignment, and you hand in the program with the illegal array syntax that you've used, you're in danger of losing points on the assignment, because what you are doing is not legal C++. So you need to fix that part of the program by investigating how to create arrays with dynamic size
Regards,
Paul McKenzie
-
April 27th, 2010, 03:23 AM
#7
Re: Help with my program
 Originally Posted by GCDEF
The program doesn't compile using Visual Studio 2005. See Philip's post.
Learn the debugger. You can't program without it. Your IDE should have come with some kind of documentation.
Code Tags are explained in the before you post sticky at the top of the forum.
i dont understand the last sentence
-
April 27th, 2010, 03:42 AM
#8
Re: Help with my program
These [code] [/code] are code tags, put your code within them when posting on this forum and your code will then display properly formatted in your post.
Last edited by PredicateNormative; April 27th, 2010 at 03:45 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|