|
-
April 17th, 2010, 01:03 PM
#1
Why does my program keeps crashing
Ok i have an assignment that required me to create a simple database using C++. I tried using both ways which is structures and 2d arrays. But it just keep crashing the command prompt!! Can somebody take a look at the code and tell me the problem? Thank you very much in advance. P.S: I am new to programming. I can hardly understand classes so please be detailed in your explaination.
---------------------------------------------------------------
the structure program
------------------------------------------------------------
#include <iostream>
using namespace std;
int add_record()
{struct weapon
{int id;
int name;
int quantity;
};
int i,k,size,l;
i=1;
l=0;
k=1;
size=0;
weapon add[size];
cout<< "Enter size of record"<< endl;
cin>> size;
for( l=0; l<size;l++)
{
cin>> add[l].id;
}
for( l=0; l<size;l++)
{
cout<< add[l].id<< endl;
}
return 0;
}
int main()
{
add_record();
system ("PAUSE");
return 0;
}
=========================================================
the array prog
=========================================
#include <iostream>
using namespace std;
int main()
{int row, a,b;
b=0;
const int column=3;
char weapon [row][column];
row=1;
cout<< "Please enter row "<< endl;
cin>> row ;
for ( a=0; a<row; a++)
{b=0;
for (b=0; b<3; b++)
{ cout << "Enter id, quantity,name" <<endl;
cin>>weapon[a][b];
}
};
for ( a=0; a<row; a++)
{b=0;
for (b=0; b<3; b++)
{
cout<< weapon[a][b]<< " ";
}
cout<< endl;
};
system ("pause");
return 0;
}
_--------------------------------------------------------------------------------
-
April 17th, 2010, 01:10 PM
#2
Re: Why does my program keeps crashing
#include <iostream>
using namespace std;
int add_record()
{struct weapon
{int id;
int name;
int quantity;
};
int i,k,size,l;
i=1;
l=0;
k=1;
size=0;
weapon add[size];
cout<< "Enter size of record"<< endl;
cin>> size;
for( l=0; l<size;l++)
{
cin>> add[l].id;
cin>> add[l].name;
cin>> add[l].quantity;
}
for( l=0; l<size;l++)
{
cout<< add[l].id<< endl;
cout<< add[l].name<< endl;
cout<< add[l].quantity<< endl;
}
return 0;
}
int main()
{
add_record();
system ("PAUSE");
return 0;
}
This is the real structure program. I post the wrong thing sorry.
-
April 17th, 2010, 01:18 PM
#3
Re: Why does my program keeps crashing
Code:
size=0;
weapon add[size];
cout<< "Enter size of record"<< endl;
cin>> size;
The size changed later never results in allocating an additional weapon. Your add still remains of zero size.
Best regards,
Igor
-
April 17th, 2010, 11:19 PM
#4
Re: Why does my program keeps crashing
I see i will try improving it and see the result thank you
Tags for this Thread
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
|