11 Replies - 451 Views - Last Post: 22 November 2010 - 04:02 PM Rate Topic: -----

#1 titova87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-September 10

Passing objects erases contents

Posted 21 November 2010 - 12:34 PM

I am using a class function that adds and objects to a new array element and increments that array.
First, this is done by reading creating a new struct, reading from text and assigning the values to struct member, then passing that struct to the add movie function which adds the struct's member's values to those of an object array element.

I run into a problem when I try more or less do the same thing, but allow this time the user to input the files into the struct, then pass the struct to the purchase function which calls the add movie member function and passes to it the information to then be assigned to an object array element. From testing, the information is successfully passed, but it is never assigned to the array.

This is a homework assignment and I can not do this a different way- it must be done using the steps I've just described.
So I need help figuring out how to make this work.

Thanks!

//******************************************************************************
//
//		File:			Inventory.cpp	  
//		
//		
//******************************************************************************

#include <string>
#include <iostream>
#include<string>
#include <fstream>
#include "Inventory.h"
using namespace std;

void getData(Inventory &);
int menu();
void displayOneMovie(Inventory &);
void checkOut(Inventory &);
void checkIn(Inventory &);
void purchase(Inventory &);

const int SIZE=100;

int main()
{
	Inventory inventory(SIZE);
	getData(inventory);  
	int option=menu();
	while (option!=6)
	{
		switch (option)
		{
			case 1: purchase(inventory);                 
					break;
			case 2: checkOut(inventory);
					break;
			case 3: checkIn(inventory);
					break;
			case 4: displayOneMovie(inventory);
					break;
			case 5: inventory.displayInventory();
					break;
		}
		option=menu();
	}
	
	
	return 0;
}

int menu(){
	int choice=0;
	cout<<"Make a selection from the list below:"<<endl;
	cout<<"1.    Purchase a movie" <<endl;
	cout<<"2.    Check out a movie"<<endl;
	cout<<"3.    Check in a movie "<<endl;
	cout<<"4.    Display a movie's information "<<endl;
	cout<<"5.    Display the movie inventory "<<endl;
	cout<<"6.    Quit "<<endl;
	cin>>choice;
	return choice;
}




void getData(Inventory& inventory)
{
   ifstream inFile;                            //initializes inFile object
   inFile.open("Movies.txt");            

   if(!inFile){                                //test if file there
	   cout << "Error opening input file: Movies" << endl;
       exit (99);
   }
	Movie movie;
    inFile >> movie.MPAC;
	while(!inFile.eof()) { 	
		inFile >>movie.year;
		inFile >>movie.runTime;
		inFile >>movie.checkedIn;
		inFile >>movie.checkedOut;
		getline(inFile,movie.name);
		inventory.add(movie);
		inFile >> movie.MPAC;
	}
 
	inFile.close();
}

void displayOneMovie(Inventory & inventory){
	int mpac=0;
	cout<<"Please enter the movie's MPAC which"
		<<" you would like displayed: "<<endl;
	cin>>mpac;
	inventory.displayMovie(mpac);
	}

void checkOut(Inventory & inventory){
	int mpac=0;
	cout<<"Please enter the movie's MPAC which"
		<<" you would like checked out: "<<endl;
	cin>>mpac;
	inventory.checkMovieOut(mpac);
	}

void checkIn(Inventory & inventory){
	int mpac=0;
	cout<<"Please enter the movie's MPAC which"
		<<" you would like checked in: "<<endl;
	cin>>mpac;
	inventory.checkMovieIn(mpac);
	}

void purchase(Inventory & inventory){
	Movie movie;
	cout<<"Pleae enter the MPAC # of the movie you wish to purchase. "<<endl;
    cin >> movie.MPAC;
	cout<<"Pleae enter the year. "<<endl;
	cin >>movie.year;
	cout<<"Pleae enter the run time . "<<endl;
	cin >>movie.runTime;
	cout<<"Pleae enter the # amount of the movie you wish to purchase. "<<endl;
	cin >>movie.checkedIn;
	cout<<"Pleae enter the name of the movie. "<<endl;
	
	//getline(cin,movie.name);
	cin>>movie.name;
	movie.checkedOut=0;
	
	cout<<"////////test info/////// "<<endl;
    cout<< movie.MPAC<<endl;
	cout<< movie.year<<endl;
	cout<< movie.runTime<<endl;
	cout<< movie.checkedIn<<endl;
	cout<< movie.name<<endl;

	inventory.add(movie);

}

//******************************************************************************
//
//		File:			Inventory.h	
//******************************************************************************
#include <string>
#include <iostream>
using namespace std;
#ifndef MOVIEINVENTORY_H
#define MOVIEINVENTORY_H


struct Movie
{
	int MPAC;
	string name;
	int year;
	int runTime;
	int checkedIn;		
	int checkedOut;
};

class Inventory
{
	
	private:
		Movie* ptrMovie;
		int numOfMovies;

	public:
		Inventory();			// Default constructor
		Inventory(int); 		      // Overloaded constructor
		~Inventory();			// Destructor
		int findMovie(int);		// Find a movie given the MPAC
		void displayMovie(int);
		int getNumOfMovies();		// Get the number of movies
		void add(Movie); 
		void checkMovieOut(int);	// Check out a movie
		void checkMovieIn(int);		// Check in a movie
		void displayInventory();
};

#endif

//******************************************************************************
//
//		File:			Inventory.cpp		
//******************************************************************************
#include <string>
#include <iostream>
#include "Inventory.h"
using namespace std;


Inventory::Inventory(){			// Default constructor
	numOfMovies=0;
	ptrMovie = new Movie [10];
}

Inventory::Inventory(int num){		      // Overloaded constructor
	numOfMovies=0;
	ptrMovie = new Movie [num];	
}

Inventory::~Inventory(){			// Destructor
	cout<<"The program has terminated"<<endl;
	delete [] ptrMovie;
}

void Inventory::displayMovie(int userMPAC){
	int i = findMovie(userMPAC);
	cout<<"Movie checked in: "
	<< ptrMovie[i].MPAC
	<< "-"<< ptrMovie[i].name
	<< " "<< ptrMovie[i].year
	<< " "<< ptrMovie[i].runTime<<endl;
	ptrMovie[i].checkedIn++;
	ptrMovie[i].checkedOut--;
	cout << " checked in: "<< ptrMovie[i].checkedIn
	<< " checked out: "<< ptrMovie[i].checkedOut
	<< endl;
}


void Inventory::add(Movie x){
	ptrMovie[numOfMovies]=x;
	numOfMovies++;

	cout<<"////////test info/////// "<<endl;
    cout<< ptrMovie[numOfMovies].MPAC<<endl;
	cout<< ptrMovie[numOfMovies].year<<endl;
	cout<< ptrMovie[numOfMovies].runTime<<endl;
	cout<< ptrMovie[numOfMovies].checkedIn<<endl;
	cout<< ptrMovie[numOfMovies].name<<endl;

}

void Inventory::displayInventory(){
	for(int i=0; i<numOfMovies; i++){  
		cout<<"Movie is ";
		cout<< ptrMovie[i].MPAC;
		cout<<" "<< ptrMovie[i].name;
		cout<< " "<< ptrMovie[i].year;
		cout<< " "<< ptrMovie[i].runTime;
		cout<<" # checked in:"<<ptrMovie[i].checkedIn;
		cout<<" # checked out:"<<ptrMovie[i].checkedOut;
		cout<< endl;
	}
	int numTotal=getNumOfMovies();
	cout<<"Total owned in the store is: "<< numTotal<<endl;
}

void Inventory::checkMovieOut(int userMPAC){
	int i = findMovie(userMPAC);
	if(ptrMovie[i].checkedIn>0){
		cout<<"Movie checked out: "
		<< ptrMovie[i].MPAC
		<< "-"<< ptrMovie[i].name
		<< " "<< ptrMovie[i].year
		<< " "<< ptrMovie[i].runTime<<endl;
		ptrMovie[i].checkedIn--;
		ptrMovie[i].checkedOut++;
		cout << " checked in: "<< ptrMovie[i].checkedIn
		<< " checked out: "<< ptrMovie[i].checkedOut
		<< endl;
	}
	else
		cout<<"The movie has not been purchased yet."<<endl;
}

void Inventory::checkMovieIn(int userMPAC){
	int i = findMovie(userMPAC);
	if(ptrMovie[i].checkedOut>0){
		cout<<"Movie checked in: "
		<< ptrMovie[i].MPAC
		<< "-"<< ptrMovie[i].name
		<< " "<< ptrMovie[i].year
		<< " "<< ptrMovie[i].runTime<<endl;
		ptrMovie[i].checkedIn++;
		ptrMovie[i].checkedOut--;
		cout << " checked in: "<< ptrMovie[i].checkedIn
		<< " checked out: "<< ptrMovie[i].checkedOut
		<< endl;
	}
	else
		cout<<"The movie has not been checked out yet."<<endl;

}

int Inventory::findMovie(int userMPAC){
	int i=0;
	bool found=false;
	while (i<numOfMovies){         
		if (userMPAC == ptrMovie[i].MPAC){
			found=true;
		}
		i++;
	}
	if (!found){
		cout<<"The movie was not found, please try again."<<endl;
	}
	return i;

}

int Inventory::getNumOfMovies(){
	return numOfMovies;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Passing objects erases contents

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4013
  • View blog
  • Posts: 12,393
  • Joined: 25-December 09

Re: Passing objects erases contents

Posted 21 November 2010 - 12:50 PM

When I compile your code I get the following warnings.

Quote

main.cpp|21|warning: ‘class Inventory’ has pointer data members|
main.cpp|21|warning: but does not override ‘Inventory(const Inventory&)’|
main.cpp|21|warning: or ‘operator=(const Inventory&)’|


This is saying that you should create the Copy and operator= functions in order for this class to work properly.

You should probably also create a destructor to take care of properly deleting the memory created by the constructor.

Please post your movie.txt file.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 titova87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-September 10

Re: Passing objects erases contents

Posted 21 November 2010 - 01:27 PM

Thanks Jim

I've attached the txt file.I am now trying to figure out how to write those functions- they are new to me.

edit:: I should have to use these for this piece of hmw

Attached File(s)


This post has been edited by titova87: 21 November 2010 - 01:48 PM

Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Passing objects erases contents

Posted 21 November 2010 - 01:35 PM

void Inventory::add(Movie x){
    ptrMovie[numOfMovies]=x;
    numOfMovies++;

    cout<<"////////test info/////// "<<endl;
    cout<< ptrMovie[numOfMovies].MPAC<<endl;
    cout<< ptrMovie[numOfMovies].year<<endl;
    cout<< ptrMovie[numOfMovies].runTime<<endl;
    cout<< ptrMovie[numOfMovies].checkedIn<<endl;
    cout<< ptrMovie[numOfMovies].name<<endl;

}



You sure about this? Let's take an example. Let's say numofMovies, to start with, is 5. You add a movie. So you copy over the data to ptrMovie[5]. Then you make numOfMovies 6. Then you spit out data from ptrMovie[numofMovies], which translates to ptrMovie[6]. Is this OK?
Was This Post Helpful? 1
  • +
  • -

#5 titova87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-September 10

Re: Passing objects erases contents

Posted 21 November 2010 - 01:44 PM

hey thanks a ton for that- yeah that makes more sense.
so i fixed this and the movie, when i purhcase it, does show in the inventory when i display the whole inventory, but
the program won't allow me to display it by itself it or check it out

This post has been edited by titova87: 21 November 2010 - 01:48 PM

Was This Post Helpful? 0
  • +
  • -

#6 titova87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-September 10

Re: Passing objects erases contents

Posted 21 November 2010 - 01:56 PM

This si what the output is when I try to display the newly purchased movie's info:

Please enter the movie's MPAC which you would like displayed:
5454
Movie checked in: -842150451- -842150451 -842150451
checked in: -842150450 checked out: -842150452
Was This Post Helpful? 0
  • +
  • -

#7 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Passing objects erases contents

Posted 21 November 2010 - 02:03 PM

So how did you try and isolate the problem? What have you discovered so far?
Was This Post Helpful? 0
  • +
  • -

#8 titova87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-September 10

Re: Passing objects erases contents

Posted 21 November 2010 - 02:21 PM

View PostOler1s, on 21 November 2010 - 01:03 PM, said:

So how did you try and isolate the problem? What have you discovered so far?


Just that it shows up fine when I display the whole inventory, but it seems like my program can't find it when I search for it by itself (through display one movie function) or whne I try to check it out
Was This Post Helpful? 0
  • +
  • -

#9 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Passing objects erases contents

Posted 21 November 2010 - 05:08 PM

Quote

but it seems like my program can't find it when I search for it by itself (through display one movie function
So it seems like whatever code you have to find a movie is incorrect. Where is your code for finding a movie? Have you examined its behavior?
Was This Post Helpful? 0
  • +
  • -

#10 titova87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-September 10

Re: Passing objects erases contents

Posted 22 November 2010 - 08:15 AM

Yep- my professor helped me out on this one.
I'm still to examine the differences between what I had before and what I now have and see what went wrong, but I wanted to post the solution on here for anyone else having a similar problem. Thanks to all of you who helped- and who help me every time I'm stuck =)
Here's then new code:

int Inventory::findMovie(int userMPAC){
	int i=0;
	int found= -1;
	while (i<numOfMovies){         
		if (userMPAC == ptrMovie[i].MPAC){
			found=i;
		}
		i++;
	}
	if (!found){
		cout<<"The movie was not found, please try again."<<endl;
	}
	return found;
}



Oler1s, the problem was in the find function. What I needed to do is change the bool found to an int initialize as -1. Then if you find the MPAC, set found to i. If found is -1, the the movie is not found. Then return return found, not i.


Again- thanks for the help
Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Passing objects erases contents

Posted 22 November 2010 - 08:30 AM

Sorry, but that's still wrong. The value of a boolean false is ZERO, therefore, when will this print?

if (!found){
    cout<<"The movie was not found, please try again."<<endl;
}



HINT: Not when a movie wasn't found, but when the movie was found in a particular location.

Also, if you've found the movie, why would you want to continue through the loop?
Was This Post Helpful? 0
  • +
  • -

#12 titova87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-September 10

Re: Passing objects erases contents

Posted 22 November 2010 - 04:02 PM

View PostJackOfAllTrades, on 22 November 2010 - 07:30 AM, said:

Sorry, but that's still wrong. The value of a boolean false is ZERO, therefore, when will this print?

if (!found){
    cout<<"The movie was not found, please try again."<<endl;
}



HINT: Not when a movie wasn't found, but when the movie was found in a particular location.

Also, if you've found the movie, why would you want to continue through the loop?



my found here is an int value, so i need the if (!found) into if (found<0)

thanks for catching that
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1