Hi guys im trying to make a program that will work as " Slot Machine " and im stuck in one part that i can not resolve. Im trying to verify predetermined char in random array so i can know if some of the three char appear three times or more. I want to do that so i can deteremined how much player is going to win. Im new to programming so i would appreciate any help that i can get. Here is a part of my code that i need help with.

srand ( time(0));

    char sign[3]; 
    sign [0] = '$';
    sign [1] = 'X';
    sign [2] = 'O';
    
    for (char i = 0; i < 3; ++i) 
    { 
        char r  = rand() % 3;
        char s  = rand() % 3;
        char d  = rand() % 3;
            
        cout << "___ ___ ___\n";
        cout << " "<<sign[r]<< " | " << sign[s] << " | " << sign[d] << " | " << "\n"; 
        cout << "___|___|___|\n";


        
        }

Im trying to verify predetermined char in random array so i can know if some of the three char appear three times or more.

i would probably write a function that will return the number of occurances the desired char appears in your array. then you can test for this condition and handle it accordingly:

int get_occurances(char array[], int& size, char& target)
{
     int counter = 0;      

     for(int i=0; i<size; i++)
     {
          if(array[i] == target)
          {
               counter++;
          }
     }

     return counter;
}

or you can just use the cout() function from the <algorithm> library:

#include <algorithm>

int counter = 0;

counter = count(&array[0], &array[size], target);

//handle the case of the desired char occuring more than 3 times
if(target > 3)
{
     //do whatever you wanted to do here
}

Ty for your help and excuse my stupid question but i dont know where to put in this code. Like i said before im really fresh in programming.

I think what you want to see is if the combination of (r,s,d) results in some winning combination? Is that correct. For example if R = $ and S = $ and D = $, then you would win some large amount? If so then you can simply do something like this:

for (char i = 0; i < 3; ++i) 
{ 
        char r  = rand() % 3;
        char s  = rand() % 3;
        char d  = rand() % 3;
 
        cout << "___ ___ ___\n";
        cout << " "<<sign[r]<< " | " << sign[s] << " | " << sign[d] << " | " << "\n"; 
        cout << "___|___|___|\n";

         if(r == s && s == d && d == '$') { //if r = $ and s = $ and d = $
              cout << "JACKPOT!!!!\n";
         }else if(r == s && s == d && d == 'X'){ // if r == X and s == X and d == X
              cout << "You won $500 \n";
        }else if(r == s && s == d && d == 'O') //  if r == O and s == O and d == O
              cout << "You won $100 \n";
        } else{
              cout << "You won nothing :(\n";
        }
}

You can create a function to automate and remove the redundancy if you need to.

This article has been dead for over six months. Start a new discussion instead.