Problem while testing class methods

Hi all,
So for a c++ assignment I am creating two classes, Product and Store. I got my product class working and tested it successfully, however, I'm having issues with my Store class implementation. It compiles/builds fine, but when I execute in Quincy it just says Quincy has stopped working and closes. We turn in our assignments through a unix server, and when I test it there, I see lots of random numbers and characters and the message Segmentation Fault at the very end. Here's my code:

Store.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef STORE_H
#define STORE_H
#include "Product.h"

class Store
   {
      private:
		Product prodAr[30];
		int numProds;	   
	  public:           
        Store();
		Store(const char*);
		void print();
               
   };

#endif 


Store.cpp
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
#include "Store.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;

Store::Store()
	{
	numProds = 0;
	}
	
Store::Store(const char* fileName)
	{
	ifstream inFile;
	inFile.open(fileName);
	
	if(inFile.fail())
		{
		cout<< "Failed to open file";
		exit(-1);
		}
	
	inFile.read((char*) this, sizeof(Store));
	
	inFile.close();
	}

void Store::print()
	{
	cout << "Product Inventory Listing" << endl;
	
	for(int i; i < (int) sizeof(prodAr); i++)
		{
		prodAr[i].print();
		cout << endl;
		}
	
	}


And here's the main program I am using to test out the class:
1
2
3
4
5
6
7
8
9
10
11
12
13
   #include <iostream>
   #include "Store.h"

   using std::cout;
   using std::endl;

   int main()
      {
		Store store1("storedata");
		store1.print();
		
      return 0;
      }


The program is reading a file storedata using binary input, and prodAr[] is an array of Product objects (30 maximum).
Any ideas as to what could be causing the problems here? Thanks in advance for any input! Here's a link to the actual assignment (i think step 5 or 6 is where I went wrong): http://faculty.cs.niu.edu/~mcmahon/CS241/Assign/as02_part1_cs241_f12.html
Thanks!
for(int i; i < (int) sizeof(prodAr); i++)

Notice at the beginning here you declare int i but don't initialize it. What's happening is that the prgram is creating the variable with a garbage value and it's starting the loop. I bet if you add this:

for (int i = 0;...)

It would work.
Thanks for the input, I changed that, but still getting pages and pages of this when I attempt to run:

:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:  0.00774519350
00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:  0.00909327152
spf=00;36:    AIL=/var/mail/z1577699                        1323327073898528700067250061845622872472662834170325413717296712481627397922019862249178976370550175608612963613031887650165751159661635206124558523396711448960401857714314677946663021067176483096926457575155695616.001818321775
:/usr/bin:/bin:/usr/gamesin:/usr/games                                 314186567563201845663230564686102528.001601070397
TF-8                                                          0.001918986355
va/*:.        ONNECTION=131.156.211.13 31849 131.156.145.24 22  0.001145851648
Segmentation fault
Those are file types. I don't know UNIX, but this seems to be a UNIX problem.

Does this happen to only you?
The program is reading a file storedata using binary input


No, it's not.
It's not a unix problem, I get the same issue when I execute it using quincy or dev.

And cire, good catch, i changed the input to this: inFile.open(fileName, ios::binary);

Still having the same issue. I know my problem is in my print method for loop because if I use a number (12 - which is the actual number of products in the data file) instead of int sizeof(prodAr) it works fine. Anyone have an idea?



Last edited on
sizeof(prodAr) returns the actual size of all of the objects in the array, not the number of elements in the array. i < numProds seems like it might be an appropriate control expression for some reason.
Last edited on
Thank you, but how would I get numProds to accurately represent the number of elements in the array?
Since you're reading in the entire Store object in the constructor, I assumed it was present in the file you're reading from. If it isn't, your constructor is not correct.

Checking the state of inFile after the read would be appropriate.
Topic archived. No new replies allowed.