JomsDev
JomsDev

Reputation: 116

I can't initialize a struct values in C++

I have this variable:

struct mat{
    int row;
    int column;
};
vector<bool> aux;

In the main function I initialize a vector of mat's by this way:

int main(){
    int n,r,c;
    char s;
    cin >> n;
    vector<mat> matrices = vector<mat> (27);
    aux = vector<bool> (32,false);
    for(int i=0; i<n;++i) {
        cin >> s >> r >> c;
        matrices[s-'A'].row = r;
        matrices[s-'A'].column = c;
        aux[s-'A'] = true;
}

But when I left the for loop, I call a function which writes the vector matrices in shell:

void writeMatrices(vector<mat>& matrices){
    for(int i = 0; i < aux.size(); ++i){
        if(aux[i]) {
            cout << char ('A'+i) << " Rows: " << matrices[i+'A'].row << " Columns: " << matrices[i+'A'].column << endl;
        }
    }
}

And I only obtain 0's.

Does anybody know why?

Upvotes: 0

Views: 170

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

Inside the for loop you used indices s-'A' for vector matrices

for(int i=0; i<n;++i) {
    cin >> s >> r >> c;
    matrices[s-'A'].row = r;
    matrices[s-'A'].column = c;
    aux[s-'A'] = true;

}

As I guess s had values in the range 'A' - 'Z'

However inside the function you used indices `i+'A' fo vector matrices

cout << char ('A'+i) << " Rows: " << matrices[i+'A'].row << " Columns: " << matrices[i+'A'].column << endl;

I think that indices in vector matrices must coinside with indices in vector aux. That is the body of the function should look as

void writeMatrices( const vector<mat> &matrices )
{
    for ( std::vector<bool>:size_type i = 0; i < aux.size(); ++i )
    {
        if( aux[i] ) 
        {
            cout << char ('A'+i) << " Rows: " << matrices[i].row << " Columns: " << matrices[i].column << endl;
        }
    }
}

I think that container std::map<char, mat> would be more suitable for your task.

Upvotes: 0

Edward
Edward

Reputation: 7090

The problem is that you are incorrectly adding the value of 'A' to your index when reading it back out. The code for writeMatrices should be this instead:

void writeMatrices(vector<mat>& matrices){
    for(int i = 0; i < aux.size(); ++i){
        if(aux[i]) {
            cout << char ('A'+i) 
                 // note that the index for matrices should be i, not i+'A' !
                 << " Rows: "    << matrices[i].row 
                 << " Columns: " << matrices[i].column 
                 << endl;
        }
    }
}

Used with this input data:

6
A 1 2
B 2 3
C 3 4
D 4 5
E 5 6
F 6 7

we now get this output:

A Rows: 1 Columns: 2
B Rows: 2 Columns: 3
C Rows: 3 Columns: 4
D Rows: 4 Columns: 5
E Rows: 5 Columns: 6
F Rows: 6 Columns: 7

Some error checking within your code would have enabled you to spot this problem sooner.

Upvotes: 1

Dennis
Dennis

Reputation: 2616

Your matrices collection has 27 elements. Your collection of bool (aux) has 32 elements, which is more than the size or your matrices collection. Your for loop is executing "n" times which could be anything depending on input. Your collection indexer is "s-'A'" which I assume you're inputting s like "A,B,C,D,..." All in all this is a very strange, haphazard and unreliable way to work with collections. You should instead have just 1 max size and 1 loop indexer variable and use it for everything. Or start with empty collections and use "push_back()" to add each element. You could also add "bool aux" as a member of your "mat" struct, and then you wouldn't need a separate "aux" collection.

There's also nothing in the code you've given which sets "r" and "c", so unless those are set in code that wasn't shown, you're only setting your rows and columns fields to the default value of r and c anyway.

Upvotes: 0

Related Questions