Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,951 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,693 people online right now. Registration is fast and FREE... Join Now!




Prob storing object into vector and finding it.

 
Reply to this topicStart new topic

Prob storing object into vector and finding it., objects, vectors, and bad pointers

godmoktail
17 May, 2008 - 08:36 AM
Post #1

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 17


My Contributions
Hello there. I want to start off by saying thank you in advance. I'm having a problem storing an object and then searching for it later on. I try to store the Person object which is created by the function addUser() into a vector (of Person pointers). I get an error when I try to return the Person temp from the addUser() function. The error says:
"Debug Assertion Failed
_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)".

I don't want to bombard you with tons of headers and implementation files, so I will put code just what I think is necessary. If you need more, please let me know.

Dquicken.h
CODE

#include "Person.h"
using namespace std;

#ifndef DQUICKEN_H
#define DQUICKEN_H

class Dquicken{
private:
    vector<Person*> users;                        // the users in this program

public:
    Dquicken();
    ~Dquicken();
    Person addUser();                            // returns a user to add to the program
    void displayMainMenu();                        // displays the main menu of the program
    void displaySplash();                        // displays the splash screen!!!
    void getNameAndPass(string&, string&);        // gets a username and password for and added file
    void ProgramLoop();                            // program continues running until user ends it
    Person findFile(string name, string how);    // find a person, whether by username or name
};

#endif


Dquicken.cpp
CODE

// THIS IS DQUICKEN.CPP
#include "Dquicken.h"
#include "StringReader.h"
#include <iostream>

Dquicken::Dquicken(){
    users.empty(); // what is the best way to initialize a vector?
    users.clear();
}

Dquicken::~Dquicken(){
    // get rid of all users
    for(size_t i = 0; i < users.size(); ++i){
        delete users[i];
    }
    users.empty();
    users.clear();
}

void Dquicken::displayMainMenu(){}

Person Dquicken::addUser(){
    string name = getString("name");

    // get information for that person
    cout << "Would you like to enter a username and password for the file?(y/n)---->";
    char yorn;
    cin >> yorn;

    Person temp = Person(name);

    if(yorn == 'y'){
        // get username and password
        cout << "\n\n";
        string nName, nPass;

        this->getNameAndPass(nName, nPass);
        temp.setPassword(nPass);
        temp.setUsername(nName);
    }else{
        cout << "\nOk. You can add that information or change it later if you  load the file.";
    }

    return temp;
}

void Dquicken::displaySplash(){
    cout << "\n\n\n\t\t\tWELCOME TO DeSOTO QUICKEN!\n\n\n"
        << "\tPress Alt and Enter to make the program go full screen\n\n\n\n\n\n\n\n\n"
        << "Created by Albert Espinal, Copyright 2008\n\n\n";
    system("pause");
}

void Dquicken::ProgramLoop(){
    displaySplash();
    system("cls");

    Person* currentFile = new Person();

    displayMainMenu();
    char menu_choice;

    cout << endl
        << "What would you like to do? (1-4)--->";
    cin >> menu_choice;

    while(menu_choice != '4'){
        // if the user wants to load a file
        if(menu_choice == '1'){
            //find the file
        }
        // if the user wants to add a file
        if(menu_choice == '2'){
            Person addMe = addUser();
            users.push_back( &addMe );
            cout << "\n\nUser added." << endl;
            cout << "\n";

            // pause before going back to main menu
            cout << "\n\n";
            system("pause");
        }
        // if the user wants to delete a file
        if(menu_choice == '3'){}

       // start over
        system("cls");
        displayMainMenu();
        cout << "What would you like to do? (1-4)--->";
        cin >> menu_choice;
    }

    // user wants to leave, so leave
    cout << "\nExiting.\n\n";
    delete currentFile;
    exit(1);
}




Also, this is not a homework assignment. This is my own personal project.

Thanks again. Peace.
User is offlineProfile CardPM
+Quote Post

godmoktail
RE: Prob Storing Object Into Vector And Finding It.
17 May, 2008 - 10:16 AM
Post #2

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 17


My Contributions
I've debugged my program several times, and I believe the error occurs when I try "delete currentFile;". Aren't I supposed to do some cleanup of dynamic objects?

I've debugged and saw that every time I try to delete a dynamic object, whether it was in a destructor for the Person class (deleting the dynamic object Date) or as I said above, the currentFile pointer, I get the Assertion Failed message.

I want to try to make this work with pointers. Please provide some insight.
Thanks again.
User is offlineProfile CardPM
+Quote Post

godmoktail
RE: Prob Storing Object Into Vector And Finding It.
18 May, 2008 - 11:49 PM
Post #3

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 17


My Contributions
Anyone have some insight? Maybe you need more code?

Thanks in advance
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:05AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month