having a few problems with my code - output

Hey all im trying to get my code to work but I get a few errors in the int main
Anyone able to help me?
I believe the root of the problem is in line 150 but I cant seem to fix it.
Thanks

Full code:
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
using namespace std;

char instr[81];
FILE *stream;
int getline(char*,int);

//double getline(string*,double);
class Custome {// The class
  int jj;
  
 public:  
 Custome(){ 
      jj = 0.0;
  }
  Custome(int jj){
      this->jj = jj;
}
void set_jj(){
    getline(instr,81);
    jj = atof(instr);
    }
  int get_jj(){return jj;}
  void calc_jj_precent(){
      
  }
  ~Custome(){};
};
  class New : public Custome{
  
  int account;
    char* firstName;  
    char* lastName;  
    double minutes;
    char* length;
   double active;
    char stat[81];
    New(){};
    New(int account, char* firstName, char* lastName, double minutes, char* length, double active, char* stat, int jj, double calc_jj_precent) : Custome(jj){
            this->account=account;
			std::strcpy(this->firstName,firstName);
			this->minutes=minutes;
			std::strcpy(this->length,length);
			this->active=active;
		    std::strcpy(this->stat,stat);
}

		void set_account(){
		   getline(instr,81);}
		 int get_account(){return account;}
		 
		void set_firstName(){
		    getline(firstName,81);}
		 char *get_firstName(){return firstName;}
		 
		 void set_lastName(){
		    getline(lastName,81);}
		 char *get_lastName(){return lastName;}

          void set_minutes(){
		   getline(instr,81);}
		 double get_minutes(){return minutes;}
		
	     void set_length(){
		    getline(length,81);}
		 char *get_length(){return length;}
		 
		  void set_active(){
		   getline(instr,81);}
		 double get_active(){return active;}
		 
 void set_stat(){
		    getline(stat,81);}
		 char *get_stat(){return stat;}
		
	
		
		double calc_jj_precent(){return (get_jj()/100.0);}
		
		
		
		friend istream & operator >> (istream &in, New &New)
	
			{ 
				   
				    New.set_account();
					
					New.set_firstName();
					
					New.set_lastName();

					New.set_minutes();
					
					New.set_length();
					
					New.set_active();
					
					New.set_stat();
					
					New.set_jj();
					
					New.calc_jj_precent();
						
					return in; 
			}//friend	

		
		

	friend ostream &operator<<(ostream &output, New &New){		
		output << "\nAccount "	<<New.get_account();
		output << "\nFirstnamed "		<<New.get_firstName();
		output << "\nLastnamed "		<<New.get_lastName();
		output << "\nMinutes "		<<New.get_minutes();
		output << "\nLength "		<<New.get_length();
		output << "\nActive "		<<New.get_active();
		output << "\nStat "		<<New.get_stat();
		output << "\nJj "		<<New.get_jj()<<endl;
		output << "\nJjPrecent "		<<New.calc_jj_precent()<<endl;
			return output;
		}
		
		void display();
		~New(){};
			
	

};

void New::display(){ 
	cout << "\nAccount: " 		<< account;
	cout << "\nFirstName: " 	<< firstName;
	cout << "\nLastName: "		<< lastName;
cout << "\nMinutes: " 		<< minutes;
	cout << "\nLength: " 			<< length;
	cout << "\nActive: " 			<< active;
	cout << "\nStat: " 		<< stat;
	cout << "\nJj: " 		<< get_jj();
	cout << "\nJjprecent: " << calc_jj_precent()<<endl;
}

int n,num_recs=0;

int main(void) {

// Create an instance of the Player class
New New(New.get_account(),New.get_firstName(),New.get_lastName(),New.get_minutes(),New.get_length(),New.active(),New.get_stat(),New.get_jj(),New.calc_jj_precent());


//Used to create the file initially.

stream = fopen("New.txt","w+b");

for(int i=0;i<3;i++){
	cin >> New;
	
				/*New.set_account();				
				New.set_firstName();
				New.set_lastName();
				New.set_minutes();				
				New.set_length();				
				New.set_active();
				New.set_stat();
				New.set_jj();				
				New.calc_jj_precent();
				*/
	
		//Write to the File

	fseek(stream,i*sizeof(New),0);	
	fwrite(&New,sizeof(New),1,stream);

}
fclose(stream);


stream = fopen("New.txt","r+b");

for(;;){
	   //Read a record
	n = fread(&New,sizeof(New),1,stream);
	if(!n)break;

	cout <<"\nPrinting output from New.Display() function:";
	New.display();
	
	cout <<"\n\nPrinting output from New friend function:";
	cout << New;
	
	num_recs++;
		
	void pause();
	{
		cout << "\n PRESS ANY KEY TO CONTINUE.... \n";
		
		
	}
}

cout << "\n\nThe number of records read from the file was: " << num_recs <<endl;

fclose(stream);

void pause();
{
	cout << "\n PRESS ANY KEY TO CONTINUE.... \n";
	
	
}

}//main 










line150
1
2
 
New New(New.get_account(),New.get_firstName(),New.get_lastName(),New.get_minutes(),New.get_length(),New.active(),New.get_stat(),New.get_jj(),New.calc_jj_precent()); 


line158
cin >> New;

line173
fseek(stream,i*sizeof(New),0);

line174
fwrite(&New,sizeof(New),1,stream);

line184
n = fread(&New,sizeof(New),1,stream);

line188
New.display();
Last edited on
OK, some things.

1. Pick an indent style and stick to it -> https://en.wikipedia.org/wiki/Indentation_style
In particular, don't mix spaces and tabs. Your editor might cope, but sooner or later, some other editor or forum post will make a steaming pile out of it.
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
199
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
using namespace std;

char instr[81];
FILE *stream;
int getline(char *, int);

//double getline(string*,double);
class Custome {                 // The class
  int jj;

public:
  Custome() {
    jj = 0.0;
  }
  Custome(int jj) {
    this->jj = jj;
  }
  void set_jj() {
    getline(instr, 81);
    jj = atof(instr);
  }
  int get_jj() {
    return jj;
  }
  void calc_jj_precent() {
  }
  ~Custome() {
  };
};

class New:public Custome {
  int account;
  char *firstName;
  char *lastName;
  double minutes;
  char *length;
  double active;
  char stat[81];
  New() {
  };
  New(int account, char *firstName, char *lastName, double minutes, char *length, double active, char *stat, int jj,
      double calc_jj_precent):Custome(jj) {
    this->account = account;
    std::strcpy(this->firstName, firstName);
    this->minutes = minutes;
    std::strcpy(this->length, length);
    this->active = active;
    std::strcpy(this->stat, stat);
  }
  void set_account() {
    getline(instr, 81);
  }
  int get_account() {
    return account;
  }
  void set_firstName() {
    getline(firstName, 81);
  }
  char *get_firstName() {
    return firstName;
  }
  void set_lastName() {
    getline(lastName, 81);
  }
  char *get_lastName() {
    return lastName;
  }
  void set_minutes() {
    getline(instr, 81);
  }
  double get_minutes() {
    return minutes;
  }
  void set_length() {
    getline(length, 81);
  }
  char *get_length() {
    return length;
  }
  void set_active() {
    getline(instr, 81);
  }
  double get_active() {
    return active;
  }
  void set_stat() {
    getline(stat, 81);
  }
  char *get_stat() {
    return stat;
  }
  double calc_jj_precent() {
    return (get_jj() / 100.0);
  }
  friend istream & operator >>(istream & in, New & New)
  {
    New.set_account();
    New.set_firstName();
    New.set_lastName();
    New.set_minutes();
    New.set_length();
    New.set_active();
    New.set_stat();
    New.set_jj();
    New.calc_jj_precent();
    return in;
  }                             //friend
  friend ostream & operator<<(ostream & output, New & New) {
    output << "\nAccount " << New.get_account();
    output << "\nFirstnamed " << New.get_firstName();
    output << "\nLastnamed " << New.get_lastName();
    output << "\nMinutes " << New.get_minutes();
    output << "\nLength " << New.get_length();
    output << "\nActive " << New.get_active();
    output << "\nStat " << New.get_stat();
    output << "\nJj " << New.get_jj() << endl;
    output << "\nJjPrecent " << New.calc_jj_precent() << endl;
    return output;
  }

  void display();

  ~New() {
  };
};

void New::display()
{
  cout << "\nAccount: " << account;
  cout << "\nFirstName: " << firstName;
  cout << "\nLastName: " << lastName;
  cout << "\nMinutes: " << minutes;
  cout << "\nLength: " << length;
  cout << "\nActive: " << active;
  cout << "\nStat: " << stat;
  cout << "\nJj: " << get_jj();
  cout << "\nJjprecent: " << calc_jj_precent() << endl;
}

int n, num_recs = 0;

int main(void)
{
  // Create an instance of the Player class
  New New(New.get_account(), New.get_firstName(), New.get_lastName(), New.get_minutes(), New.get_length(), New.active(),
          New.get_stat(), New.get_jj(), New.calc_jj_precent());

  //Used to create the file initially.
  stream = fopen("New.txt", "w+b");
  for (int i = 0; i < 3; i++) {
    cin >> New;
    /*New.set_account();
       New.set_firstName();
       New.set_lastName();
       New.set_minutes();
       New.set_length();
       New.set_active();
       New.set_stat();
       New.set_jj();
       New.calc_jj_precent();
     */
    //Write to the File
    fseek(stream, i * sizeof(New), 0);
    fwrite(&New, sizeof(New), 1, stream);
  }
  fclose(stream);

  stream = fopen("New.txt", "r+b");
  for (;;) {
    //Read a record
    n = fread(&New, sizeof(New), 1, stream);
    if (!n)
      break;

    cout << "\nPrinting output from New.Display() function:";
    New.display();
    cout << "\n\nPrinting output from New friend function:";
    cout << New;
    num_recs++;
    void pause();
    {
      cout << "\n PRESS ANY KEY TO CONTINUE.... \n";
    }
  }

  cout << "\n\nThe number of records read from the file was: " << num_recs << endl;

  fclose(stream);

  void pause();
  {
    cout << "\n PRESS ANY KEY TO CONTINUE.... \n";
  }

}                               //main 


> char instr[81];
> FILE *stream;
> int getline(char *, int);
1. Avoid global variables.
2. This is C++, so why are you using a C-style FILE* stream
3. Don't re-prototype standard methods -> https://www.cplusplus.com/reference/istream/istream/getline/

> std::strcpy(this->firstName, firstName);
You don't allocate any memory to copy into.

Replace ALL your char* with std::string, and get something that works without the need for all this micro-management of memory.

Also, you should have prototyped this whole 'New' class with just a couple of members and methods, before massively copy/pasting yourself into a much deeper hole.

> New New(New.get_account(), New.get_firstName(), New.get_lastName(), New.get_minutes(), New.get_length(), New.active(),
> New.get_stat(), New.get_jj(), New.calc_jj_precent());
This is strangely incestuous. How can you call your constructed 'get' methods at the start of your constructor?

> fwrite(&New, sizeof(New), 1, stream);
No, and No.
Your first 'No' is that your class contains pointers. It's not going to magically dereference those pointers for you.
Your second 'No' is that you already overload the << and >> operators.
You should use these to output and input instances of your class.



Last edited on
As you are using fixed size record file binary read/write for the classes (fseek, fwrite etc), the classes cannot contain any dynamic memory or pointers to memory (ie no string, vector , char* etc). You should only use arrays of basic types (int, double, char etc).

If you want to use say string as a type for a variable in a class (recommended), then you need to read/write the file as text sequentially but you cannot easily read an individual record. In this case it would be usual for the whole file to be read into say a vector of class first and then used as appropriate.

You need to decide which file method you are going to use - or has this been specified as part of the program spec. You can't mix.



Topic archived. No new replies allowed.