Why am I not getting anything on screen?

Im trying to get a randomized slot machine to work. But I can seem to get the symbols to show up on screen. Anyone have any solutions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
      char matrix [3][3] = {{'X', 'O', 'P'}, {'X', 'O', 'P'}, {'X', 'O', 'P'}};
    int x,y;

    srand (time(0));
    for (x=0; x < 3; x++)
    {
        for (y=0; y < 3; y++)
        {
            matrix [x][y] = rand() % 3;
            cout << matrix [x][y];
            if (y==2)
            {
            cout << '\n';
            }
        }
    }
}
rand() % 3; is a non-printable character.

The program initialises the array with letters of the alphabet (which are of course printable), then uses a loop to replace each of these with a value in the range 0, 1, 2 (which are non-printable character codes) and attempts to display them.

See http://www.cplusplus.com/doc/ascii/ quote:
The standard ASCII table defines 128 character codes (from 0 to 127), of which, the first 32 are control codes (non-printable), and the remaining 96 character codes are representable characters


I've a rough idea of what you might be trying to do, but perhaps you could describe it in words, so that someone may be able to suggest a solution.
Is this by any chance what you are trying to achieve?
I advise using std::array over C-Style arrays. This example will require an C++11 enabled compiler. If you haven't done that so far, it is a good thing to learn.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <array>
#include <ctime>
#include <cstdlib>
#include <iostream>

int main()
{
    std::srand(time(nullptr));

    const std::size_t symbol_count = 3;

    const std::array<const char, symbol_count> symbols{ 'X', 'O', 'P' };

    // 3 slots
    std::array<std::array<char, symbol_count>, 3> slotmachine;

    for (auto& slot : slotmachine) {
        for (auto& symbol : slot) {
            symbol = symbols[std::rand() % symbol_count];
            std::cout << symbol;
        }
        std::cout << '\n';
    }
}

If there are things in this code you don't understand, feel free to ask.
Edit: Logically speaking, my code may be incorrect, in terms of the technicalities of a slotmachine...
Last edited on
Topic archived. No new replies allowed.