try running the code in a debugger. Hopefully that will tell you where it crashes. If you can't do that, then post the full code here so someone can compile and test it.
#include<fstream.h>
#includde<conio.h>
#include<stdio.h>
class ITEMS
{
int ID;
char GIFT[20];
float cost;
public:
void Get()
{
cin >> CODE;
gets(GIFT);
cin >> cost;
}
void See()
{
cout << ID << ":" << GIFT << ":" << cost << endl;
}
float getid()
{
return ID;
}
float GetCost()
{
return Cost;
}
};
void
main()
{
ITEMS S[10];
char ch2;
int i, j, no, uid;
do {
cout << "1.enter data 2. display data 3. delete data";
int ch1;
cout << "enter ur choice";
cin >> ch1;
cout << "enter no of objects";
cin >> no;
switch (ch1) {
case 1:
for (i = 0; i <= no; i++) {
S[i].Get();
}
ofstream fo("items.dat", ios::out | ios::binary);
for (j = 0; j <= no; j++)
fo.write((char *) &S[j], sizeof(s[j]));
fo.close();
break;
case 2:
ifstream fi("items.dat", ios::in | ios::binary);
for (j = 0; j <= no; i++) {
fi.read((char *) &S[j], sizeof(S[j]));
if (S[j].GetCost() <= 2500) {
cout << "details are" << S[j].See();
}
}
fi.close();
break;
case 3:
cout << "enter id";
cin >> uid;
ifstream fi("items.dat", ios::in | ios::binary);
ofstream fo("temp.dat", ios::out | ios::binary);
for (i = 0; i <= no; i++) {
while (fi.read((char *) &S[i], sizeof(S[i]))) {
if (S.getid() != uid) {
fo.write((char *) &S[i], sizeof(S[i]))
}
}
fo.close();
fi.close();
remove("items.dat");
rename("temp.dat", "items.dat");
}
break;
default:
cout << "wrong input";
}
cout << "do you wanna continue Y or N";
cin >> ch2;
} while (ch2 == 'Y' || ch2 == 'y');
getch();
}
Line 13: CODE should be ID.
Line 14: Don't mix stdin and cin. This should be
1 2 3 4
getline(cin, str);
strncpy(GIFT, str.c_str(), sizeof(GIFT));
// null terminate in case str.size() >= sizeof(GIFT)
GIFT[sizeof(GIFT)-1] = 0;
Ideally, you'd actually store the description using a string instead of char[] so it can be any length, but they you wouldn't be able to read/write them directly with read() and write().
Line 27: Cost should be cost (lower case)
Line 31: main() must return int
Line 51: sizeof(s[j]) should be sizeof(S[j])
Line 49: You can't create an object with a constructor inside a switch case unless it's within a brace-delimited block of code. (i.e., inside { and }). The reason is that you could theoretically access fo within the code for case 2, but at that point, fo's constructor would not have been run. So you need to put braces around each case. Eg:
1 2 3 4 5 6 7 8 9 10 11
case 1:
{
for (i = 0; i <= no; i++) {
S[i].Get();
}
ofstream fo("items.dat", ios::out | ios::binary);
for (j = 0; j <= no; j++)
fo.write((char *) &S[j], sizeof(S[j]));
fo.close();
break;
}
Line 59: You can't do ostream << void, and since Item::See() returns void, this line is illegal. My advice would be to replace See() with operator<<(ostream &, const ITEMS &). Then this line could be cout << "details are" << S[j];
Line 71: S.getid() should be S[i].getid();
Line 72: Missing ; at the end.
With these changes and the ones pointed out by lastchance, the code seems to work for me.