jeudi 21 février 2019

Cycling though variables

I have got this code working so far but right now it's only storing the last variable when cycles through in the multi-dimensional array.

#include <iostream>
#include <functional>
using namespace std;

int main() 
{
    const int columns = 5, rows = 5;
    int menuSelection;
    string word1 = "sloan", word2 = "horse", word3 = "eqrit", word4 = "house", word5 = "water";
    string table[rows][columns];

    for (auto &var : { ref(word1), ref(word2), ref(word3), ref(word4), ref(word5) })
    {
        for (int i = 0; i < rows; ++i) 
        {
            for (int j = 0; j < columns; ++j) 
            {
                string test = var;
                table[i][j] = test[i];
            }
        }
    }

    for (int i = 0; i < columns; ++i)
    {
        for (int j = 0; j < rows; ++j)
        {
            std::cout << table[i][j] << ' ';
        }
        std::cout << std::endl;
    }
}

The output is:

w w w w w 
a a a a a 
t t t t t 
e e e e e 
r r r r r 

I would like it do display different words on each row of the array's output:

s l o a n
h o r s e 
e g r i t    
h o u s e    
w a t e r

Aucun commentaire:

Enregistrer un commentaire