Thread: having probs with adding vector member for class with push_back funct

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    2

    having probs with adding vector member for class with push_back funct

    I am having probs in the addApt function where I am using the push_back function to add another vector. when I run the prog and add an apt, it doesn't actually add the apt even though the "success" statement has shown from my apartment constructor. I think it might have something to do with having the constructor in the parameter for the push_back function but I am unsure. thanks for your help!!!

    Code:
    #include <iostream>
    #include <string>
    #include "apartment.h"
    #include <vector>
    
    using namespace std;
    
    void displayHeader();
    //displays program header
    void displayMenu();
    //displays main menu
    void addApt(vector<apartment> currentApt);
    //precondition: able to add vector if apt does not exist
    //postcondition: adds record to currentApt
    void delApt(vector<apartment> currentApt);
    //precondition: able to delete vector if apt exists and there is no rent due
    //postcondition: vector is removed from currentApt
    int searchForApt(vector<apartment> currentApt, int build_num, int apt_num);
    //preconditin: able to search for a particular record if currentApt is populated
    //postcondition returns vector position of record if found and -1 if not found
    void makePmt(vector<apartment> currentApt);
    //precondition: able to reduce current balance if apt exists and payment doesn't exceed balance
    //postcondition: amtDue is reduced by amount
    void displayInfo(vector<apartment> currentApt);
    //displays the list of the current apts and their respective info
    void addRent(vector<apartment> currentApt);
    //precondition: able to add one month's rent as long as the vectors exist
    //postcondition: increases amtDue by calculate_rent()
    
    <code>
    void main()
    {
    	int menuChoice;
    	vector<apartment> currentApt; //initializes vectors of apartments
    	do
    	{
    		displayMenu(); //display the main menu
    		cin>>menuChoice; //receive choice for main menu
    		switch(menuChoice) //perform the following based on menu choice
    		{
    			case 1: //choice 1
    				addApt(currentApt); //add an apartment
    				break;
    			case 2: //choice 2
    				delApt(currentApt); //delete an apartment
    				break;
    			case 3: //choice 3
    				makePmt(currentApt); //make a rent payment
    				break;
    			case 4: //choice 4
    				displayInfo(currentApt); //display list of currently rented apts
    				break;
    			case 5: //choice 5
    				addRent(currentApt); //add one month's rent to currently rented apts
    				break;
    			case 6: //choice 6
    				break; //exit program
    			default: //perform the following for incorrect input
    				cout<<endl;
    				cout<<"CHOOSE 1 THROUGH 6"<<endl;
    				cin.ignore(100,'\n');
    				cin.get();
    				break;
    		}
    
    	}
    	while(menuChoice!=6); //perform while user doesn't wish to exit program
    	cout<<endl;
    	cout<<"GOODBYE"<<endl;
    	cout<<endl;
    }
    
    void displayHeader()
    {
    	for(int index=0;index<=100;index++) //clear the screen
    		cout<<endl;
    	cout<<"Apartment Complex Manager"<<endl;
    	cout<<endl;
    }
    
    void displayMenu()
    {
    	displayHeader(); //display the program header
    	cout<<"Menu"<<endl;
    	cout<<endl;
    	cout<<"1. Add an apartment"<<endl;
    	cout<<"2. Delete an apartment"<<endl;
    	cout<<"3. Make a payment for an apartment"<<endl;
    	cout<<"4. Display information for all apartments"<<endl;
    	cout<<"5. Add one month rent amount to all apartments"<<endl;
    	cout<<"6. Quit"<<endl;
    	cout<<endl;
    	cout<<"Select an option: ";
    }
    
    void addApt(vector<apartment> currentApt)
    {
    	int build_num, apt_num, apt_type, aptPosition;
    	bool view;
    	string cust_name;
    	displayHeader(); //display the program header
    	cout<<"1. Add an apartment"<<endl;
    	cout<<endl;
    	cout<<"Please enter building number: ";
    	cin>>build_num; //receive the bldg no
    	cout<<"Please enter an apartment number (3 digits): ";
    	cin>>apt_num; //receive the apt no
    	aptPosition=searchForApt(currentApt, build_num, apt_num); //search for apt in the vectors
    	if(aptPosition!=-1) //perform the following if the apt already exists
    	{
    		cout<<endl;
    		cout<<"THIS APARTMENT ALREADY EXISTS IN THE SYSTEM"<<endl;
    		currentApt[aptPosition].display_info(); //displays the existing apt's record
    		cin.ignore(100,'\n');
    		cin.get();
    		return; //exit the current function
    	}
    	cout<<"Please enter the type of the apartment (1 for 1-bedroom, 2 for 2-bedroom, and 3 for 3-bedroom): ";
    	cin>>apt_type; //receive the amt of rooms
    	cout<<"Does the apartment have a lake view?(1 for yes, 0 for no): ";
    	cin>>view; //receive the type of view
    	cout<<"Please enter the customer name: ";
    	cin>>cust_name; //receive the co name
    	currentApt.push_back(apartment(build_num,apt_num,apt_type,view,cust_name));
    }
    
    void delApt(vector<apartment> currentApt)
    {
    	int build_num, apt_num, aptPosition;
    	displayHeader(); //display the program header
    	cout<<"2. Delete an apartment"<<endl;
    	cout<<endl;
    	cout<<"Please enter a building number: ";
    	cin>>build_num; //receive the bldg no
    	cout<<"Please enter an apartment number (3 digits): ";
    	cin>>apt_num; //receive the apt no
    	aptPosition=searchForApt(currentApt, build_num, apt_num); //searches for vector of the apt
    	if(aptPosition==-1) //perform the following if the apt wasn't found
    	{
    		cout<<endl;
    		cout<<"COULD NOT FIND APARTMENT IN LIST"<<endl;
    		cin.ignore(100,'\n');
    		cin.get();
    		return; //exit the current function
    	}
    	if(currentApt[aptPosition].getAmtDue()>0) //perform the following if rent is still owed
    	{
    		cout<<endl;
    		cout<<"THIS APARTMENT CANNOT BE DELETED. AMOUNT DUE IS $"<<currentApt[aptPosition].getAmtDue(); //displays the amt of rent still owed
    		cin.ignore(100,'\n');
    		cin.get();
    		return; //exit the current function
    	}
    	currentApt.erase(currentApt.begin()+aptPosition); //deletes the vector of the unwanted member
    	cout<<endl;
    	cout<<"APARTMENT DELETED";
    	cin.ignore(100,'\n');
    	cin.get();
    }
    
    int searchForApt(vector<apartment> currentApt, int build_num, int apt_num)
    {
    	for(unsigned int index=0;index<currentApt.size();index++) //perform the following for the apt vectors
    	{
    		if(currentApt[index].getBldNo()==build_num&&currentApt[index].getAptNo()==apt_num) //perform the following if member is found
    			return index; //return the member's position
    	}
    	return -1; //return -1 if the member wasn't found
    }
    
    void makePmt(vector<apartment> currentApt)
    {
    	int build_num, apt_num, aptPosition;
    	double amount;
    	displayHeader(); //display the program header
    	cout<<"3. Make a payment for an apartment"<<endl;
    	cout<<endl;
    	cout<<"Please enter a building number: ";
    	cin>>build_num; //receive the bldg no
    	cout<<"Please enter an apartment number (3 digits): ";
    	cin>>apt_num; //receive the apt no
    	aptPosition=searchForApt(currentApt, build_num, apt_num); //search for the apt vector to make pmt on
    	if(aptPosition==-1) //perform the following if the apt wasn't found
    	{
    		cout<<endl;
    		cout<<"COULD NOT FIND APARTMENT IN LIST"<<endl;
    		cin.ignore(100,'\n');
    		cin.get();
    		return; //exit the current function
    	}
    	cout<<endl;
    	cout<<"Amount due: $"<<currentApt[aptPosition].getAmtDue()<<endl; //displays the current amt due
    	cout<<"Enter the amount you want to pay: ";
    	cin>>amount; //receives the amt intended to pay for apt
    	currentApt[aptPosition].pay_rent(amount); //decreases the current amt due by amount paid
    }
    
    void displayInfo(vector<apartment> currentApt)
    {
    	displayHeader(); //display the program header
    	cout<<"4. Display information for all apartments"<<endl;
    	cout<<endl;
    	if(currentApt.size()==0) //perform the following there are no apt vectors
    	{
    		cout<<"NO APARTMENTS EXIST IN LIST";
    		cin.ignore(100,'\n');
    		cin.get();
    		return; //exit the current function
    	}
    	for(unsigned int index=0;index<currentApt.size();index++) //perform the following for the amt of members in the list
    		currentApt[index].display_info(); //display the members of the array and their respective info
    	cin.ignore(100,'\n');
    	cin.get();
    }
    
    void addRent(vector<apartment> currentApt)
    {
    	displayHeader(); //display the program header
    	cout<<"5. Add one month rent amount to all apartments"<<endl;
    	if(currentApt.size()==0) //perform the following if their are no apts in the list
    	{
    		cout<<endl;
    		cout<<"THERE ARE NO APARTMENTS IN THE LIST";
    		cin.ignore(100,'\n');
    		cin.get();
    		return; //exit the current function
    	}
    	for(unsigned int index=0;index<currentApt.size();index++) //perform the following for the amt of rented apts
    		currentApt[index].new_month(); //add one month's rent to each apt
    	cout<<endl;
    	cout<<"PROCESSED";
    	cin.ignore(100,'\n');
    	cin.get();
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If you want to modify the currentApt inside any of your functions you have to pass it by reference.
    Right now you modify a copy.
    e.g.
    Code:
    void addApt(vector<apartment> &currentApt)
    Kurt

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    2
    Quote Originally Posted by ZuK View Post
    If you want to modify the currentApt inside any of your functions you have to pass it by reference.
    Right now you modify a copy.
    e.g.
    Code:
    void addApt(vector<apartment> &currentApt)
    Kurt
    thanks thanks thanks thanks!!!!!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SVN Import Causes Crash
    By Tonto in forum Tech Board
    Replies: 6
    Last Post: 11-01-2006, 03:44 PM