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(constchar*);
void print();
};
#endif
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!
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:
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 intsizeof(prodAr) it works fine. Anyone have an idea?
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.
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.