the insert1() is not working please help

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<fstream>
using namespace std;
class movie
{
    int movieno;
    char moviename[25];
    float price;
 public:
    int compare(char nm[])
    {
        if(strcmpi(moviename,nm)==0)
        return 1;
        else
        return 0;
    }
    void input();
    void display();
    float return_price(){return price;}
    int return_mno(){return movieno;}
    void inc()
    { if(price>200)
      price=price*1.1;
    }

};
void movie::input()
{
    cout<<"enter movie no";cin>>movieno;
    cout<<endl<<"enter movie name : ";cin.ignore();cin.getline(moviename,25);
    cout<<endl<<"enter price";cin>>price;
}
void create()
{   ofstream fout;
    fout.open("movie.dat",ios::out|ios::binary);
    movie m1; char ch;
    do
    {
        m1.input();
        fout.write((char*)&m1,sizeof(m1));
        cout<<"want to enter more?(y/n)";
        cin>>ch;
    }while(ch=='y');
    fout.close();
}
void movie::display()
{
    cout<<"movie no : "<<movieno<<endl;
    cout<<"movie name : "<<moviename<<endl;
    cout<<"price : "<<price<<endl;
}
void output()
{   ifstream fin;
    fin.open("movie.dat",ios::binary|ios::in);
    movie m1;
    fin.read((char*)&m1,sizeof(m1));
    while(fin)
    {
        m1.display();
        fin.read((char*)&m1,sizeof(m1));
    }
    fin.close();
}
void append()
{
    ofstream fout;
    fout.open("movie.dat",ios::app|ios::binary);
    char ch='y';movie m1;
    m1.input();
    fout.write((char*)&m1,sizeof(m1));
    cout<<"wnat to enter more??";cin>>ch;
    while(ch=='y')
    {
        m1.input();
        fout.write((char*)&m1,sizeof(m1));
        cout<<"want to enter more?";cin>>ch;
    }
    fout.close();
}
void search1()
{
    char name[25];
    cout<<"enter name : ";cin.ignore();cin.getline(name,25);
    ifstream fin;
    fin.open("movie.dat",ios::binary|ios::in);
    movie m1;
    fin.read((char*)&m1,sizeof(m1));
    while(fin)
    {
        if(m1.compare(name))
        cout<<"price is : "<<m1.return_price();
        fin.read((char*)&m1,sizeof(m1));
    }
    fin.close();
}
void increase()
{
    movie m1;
    ifstream fin;
    ofstream fout;
    fin.open("movie.dat",ios::binary|ios::in);
    fout.open("temp.dat",ios::binary|ios::out);
    fin.read((char*)&m1,sizeof(m1));

    while(fin)
    {   m1.inc();
        fout.write((char*)&m1,sizeof(m1));
        fin.read((char*)&m1,sizeof(m1));
    }
    fin.close();
    fout.close();
    remove("movie.dat");
    rename("temp.dat","movie.dat");
}
void delete1()
{   char nm[25];
    cout<<"enter name";cin.ignore();cin.getline(nm,25);
    ifstream fin;
    fin.open("movie.dat",ios::binary|ios::in);
    ofstream fout;
    fout.open("temp.dat",ios::binary|ios::out);
    movie m1;
    fin.read((char*)&m1,sizeof(m1));
    while(fin)
    {
        if(m1.compare(nm))
        cout<<"deleted";
        else
        fout.write((char*)&m1,sizeof(m1));
        fin.read((char*)&m1,sizeof(m1));
    }
    fin.close();
    fout.close();
    remove("movie.dat");
    rename("temp.dat","movie.dat");
}
void insert1()
{
    movie m2;char ch='y';int x;
    m2.input();
    movie m1;
    ifstream fin;
    fin.open("movie.dat",ios::binary|ios::in);
    ofstream fout;
    fout.open("temp.dat",ios::binary|ios::out);
    fin.read((char*)&m1,sizeof(m1));
    while(fin,ch=='y')
    {
        if(m2.return_mno()>=m1.return_mno())
            fout.write((char*)&m1,sizeof(m1));
        else
        {
            fout.write((char*)&m2,sizeof(m2));
            ch='n';
        }
    }
    if(ch=='n')
    {
    while(fin)
    {
    fout.write((char*&)m1,sizeof(m1));
    fin.read((char*)&m1,sizeof(m1));
    }
}
    fin.close();
    fout.close();
    remove("movie.dat");
    rename("temp.dat","movie.dat");
}


int main()
{
    int ch;char choice;
    do{
    cout<<"enter 1 to input "<<endl;
    cout<<"enter 2 to output "<<endl;
    cout<<"enter 3 to append "<<endl;
    cout<<"enter 4 to search "<<endl;
    cout<<"enter 5 to increase "<<endl;
    cout<<"enter 6 to delete "<<endl;
    cout<<"enter 7 to insert "<<endl;
    cin>>ch;
    switch(ch)
    {
        case 1 : create();break;
        case 2 : output();break;
        case 3 : append();break;
        case 4 : search1();break;
        case 5 : increase();break;
        case 6 : delete1();break;
        case 7 : insert1();break;
    }
    cout<<"want to cont?";cin>>choice;
}while(choice=='y');
}
Last edited on
i used codeblocks for the above code,only the insert1() function is not working
insert1() is supposed to insert a new record in file assuming the data is in sorted order of movie no.
Use code tags pls. It really hurts when we have to go through black on white, so much that people don't usually do it. <code></code>, except replace the arrow brackets with []
sorry ,i have made the changes
See that's so much better. Also simple but fatal mistake for programmers: misspellings.

Line 14: string comparison function "strcmp"

I haven't copied the code into a compiler like VS2010 (which I use) so I can't guarantee all parts of the code work, but after correcting the spelling, and running it in cpp.sh, it seemed to run ok.
Last edited on
all parts of prog are ok except insert1() as told earlier,after using insert1(),the display() does not work.
while(fin,ch=='y') is the same as while(ch=='y')
Additionally your loop might be infinite as you do not read anything in your loop.
Topic archived. No new replies allowed.