|
-
April 22nd, 2010, 01:02 PM
#1
More help with arrays?
I dont know whats the problem. I followed everything the teacher taught me in class but this code just keep having errors!!! I feel like crying now. I am too lazy to explain the problem now. Its late and i feel sad. I will just post the code and someone please be a kind person and compile it in your dev c++. Please tell me what is the problem. I have to sleep now. Thank you very much in advance.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct weapon
{
string id;
string name;
string quantity;
};
weapon view(weapon add[],int 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;
}
return add[size];
}
//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
for (int 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;
}
}
//DELETING DATA
int n=a;
for( n; n<size; n++)
{
add[n]=add[n+1]
}
}
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;
}
char quit;
bool again=true;
while(again)
{int choice;
cout<< "1: By choosing this mindblasting option you have choosen to either delete the record of weapon in the weapon inventory or you are stealing my weapons \n";
cout<< "2: By choosing this fantastic option you just condemned yourself to modify the records of my inventory \n";
cout<< "3: By choosing this crazy option you will have the ability to view my super duper top secret records \n";
cout<< "4: By choosing this sad option you have chosen to leave this place \n" ;
cin>> choice;
switch(choice)
{
case 1: cout<<"1"; break;
case 2:break;
case 3:view( add[size],size); break;
case 4: again=false; break;
}
cout<< "Again? Y/N"<< endl;
cin>> quit;
if (quit=='Y')
again=false;
}
system ("PAUSE");
return 0;
}
-
April 22nd, 2010, 01:15 PM
#2
Re: More help with arrays?
Other than the fact that it doesn't compile, it seems fine to me.
But:
Why do view and delete return a weapon? Not only is this useless, but the fact that you are returning from invalid memory makes your program crash.
Your program does not compile because of the following:
Code:
//DELETING DATA
int n=a; //here a is not defined, it was part of a loop. Maybe you want n = b?
for( n; n<size; n++)
{
add[n]=add[n+1] //needs a ;
}
}
Code:
int b=0;
bool out=true;
//QUERY
for (int 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;?
}
}
Weren't you planning to do something with b here? Maybe b was the value you wanted to assign to n?
-
April 22nd, 2010, 01:16 PM
#3
Re: More help with arrays?
I didn't compiled, but main issue looks to be with the array of user defined size.
You can't do: cin>> size; weapon add[size]; (any recent compiler will complain about this).
For that you will need a dynamicaly allocated array, declare it like this:
weapon *add = new weapon[size];
Don't forget to delete it (when you are done with it) like this:
delete[] add;
-
April 22nd, 2010, 01:17 PM
#4
Re: More help with arrays?
You have a few errors in syntax. I didn't even bother checking behaviour.
int n = a;
doesn't work because the scope of a is the previous for loop so after the loop a no longer exists and so cannot be assigned to n. One possible fix for that is to take the declaration of a out of the loop.
add[n]=add[n+1]
missing semicolon.
cout<< "Enter size of record"<< endl;
cin>> size;
weapon add[ size];
Impossible to do that. when you declare an array its size must be a constant. User input is not constant. How does your compiler know how much space to reserve on the stack for an array of undeterminate size?
There maybe more errors. Only had a cursory glance. fix those and see how much better things are.
BTW Please use [code][/code] tags around code blocks.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
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
|