C++ crash om execution

Whenever i run my code this error pops out

exception 13: general protection fault at 08C7:210E error code 0240
ax=0B0A, bx=0B0A,cx=0BZA,dx=0F97,di=0000,si=001E
ds=05CF limit=FFFF segment# 004D C:\TURBOC3\BIN\TC.EXE
es=0F97 limit=1007 segment#
cs=08C7 limit=470F segment# 0095 C:\TURBOC3\BIN\TC.EXE
ss=05CF limit=FFFF segment# 004D C:\TURBOC3\BIN\TC.EXE
error code=0240 limit=INVL segment#


Here is the code ..





#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();
}
Last edited on
Whenever I drive my car, the engine light comes on.

That's about as useful for diagnosing what's wrong with my car as your post is for diagnosing what's wrong with your code.
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.
closed account (48T7M4Gy)
https://en.wikipedia.org/wiki/General_protection_fault

Famous last words for me but my intuition and guess would be a an array limit exceeded/char* type problem.
Added the code someone check pls
closed account (48T7M4Gy)
Added the code someone check pls


Before we get into it what is your program supposed to do.

At what point when you were writing so many lines did you start to get your error messages?

Might be a good idea to show us a sample of the data file too.
There's at least one thing - and probably two - with the line in case 2:
for ( j=0; j<= no; i++)
Think j ... or i ... Not both.

I'd also be suspicious that all the loops have<= in rather than <, so counting through no+1 items in total.
Last edited on
Applied these still same error
If you show us the latest version of your code, we can look into it some more.

When doing so, please use code tags to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/
Here's your code indented and with tags. See comments below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#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.
still 1 error and y u were saying line 59: is invalid ?? when that ting is working on line 71

fully edited code.. that previous crash is now gone
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

#include<fstream.h>
#include<conio.h>
#include<stdio.h>

class ITEMS
{
 int ID ; char GIFT [20]; float cost;
 public:
 void Get()
 {
 cin>> ID; gets(GIFT); cin>>cost;
 }
 void See()
{ cout<<ID<<":"<<GIFT<<":"<<cost<<endl;
}
int 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;
     fo.open("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 ; j++)
    { 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 input;
	input.open("items.dat", ios::in|ios::binary);
      ofstream output;
     output.open ("temp.dat",ios::out|ios::binary);
    for(i=0; i<no; i++)
    {
     while( input.read((char*)&S[i],sizeof(S[i])) )
      {    if(S[i].getid()!=uid)
	   {
	   output.write((char*)&S[i],sizeof(S[i])) ;
	   }
      }
      input.close();
      output.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();
}

Last edited on
Line 17: Why does getid return a float when ID is an int?

Line 66: You're comparing the result of getid (a float) to an int. floats are approximations You may not get a valid comparison.
ok done what about that illegal structure operation line 51:
Last edited on
 
cout<<"details are"<<S[j].See();

See() doesn't return anything and you can't insert a void function into cout.

This should work.
1
2
cout<<"details are: ";
S[j].See();
THanks so much every one who helped finally the program is working ..
Topic archived. No new replies allowed.