serenus
serenus

Reputation: 133

c++ runtime issues

Alright, so I'm running into an issue where the state isn't being added into the 2d array that I have set up (allStates). Below is the code for the addState function that is being called. allStates is a 10x10 array that should be empty until this point.

void addState(string stateName){
        for (int row=0; row<10; row++){
            for (int col=0; col<10; col++){
                if (allStates[row][col].get_name().empty()){
                    allStates[row][col].set_values(stateName);
                    cout << allStates[row][col].get_name();
                    break;
                }
            }   
        }   
};

Below is the place in the code where the addState function is called.

while(!infile.eof() && infile.good()){
    infile >> command;
    cout << command;
    if (command == "addState"){
        string stateName;
        infile >> stateName;
        cout << "\n" << stateName;
        a.addState(stateName);
        cout << a.get_state(stateName).get_name();
    }
}

Any tips are greatly appreciated!

EDIT:

a is a type Area, which holds all of the functions including addState, deleteState, and the like. I was getting an error saying that I couldn't call those functions without an object, so I created the a variable to allow that.

allStates is initialized in the Area class with all of those functions.

EDIT2:

class Area{
State allStates[10][10];
public:
    void addState(string stateName){
        for (int row=0; row<10; row++){
            for (int col=0; col<10; col++){
                if (allStates[row][col].get_name().empty()){
                    allStates[row][col].set_values(stateName);
                    cout << allStates[row][col].get_name();
                    break;
                }
            }   
        }   
    };
    State get_state(string name){
        for (int row=0; row<10; row++){
            for (int col=0; col<10; col++){
                if (allStates[row][col].get_name() == name)
                    return allStates[row][col];
            }   
        }
    };
    void deleteState(string name){
        for (int row=0; row<10; row++){
            for (int col=0; col<10; col++){
                string newest;
                newest.clear();
                if (allStates[row][col].get_name() == name)
                    allStates[row][col].set_values(newest);
            }   
        }
    };
};

There are some more functions in the class, but since the addState function is called first, I'm not sure if there are any issues with the others.

Upvotes: 1

Views: 124

Answers (2)

SDEZero
SDEZero

Reputation: 363

allStates[row][col].get_name()

Did you value initialize allState? otherwise, i doubt there is no object in 2d array

Upvotes: 2

Rhomnousia
Rhomnousia

Reputation: 43

While I'm hoping to see more information on allstate, I can give you another tip you may or may not enjoy.

while(infile >> command) {
//code
}

When using a stream as a condition, it will check the stream for you and return bool as well as grabbing the value you're attempting to grab. It acts as both .eof() && .good(). It's also a good way to avoid reading in '\n' into an array,list, etc by accident.

Upvotes: 1

Related Questions