1

i'm trying to make a slot machine type thing and i wanted to assign the randomly generated numbers to certain symbols like 1 = cherry, 2 = bell and so on so i could print out the results in symbol form at the end.

i tried putting the symbols as strings in an array and assigning the numbers to the elements in the array in each slot functions but it didn't work out... is there a way to do this?

here's the code i've written so far, minus the array attempts. any suggestions would be helpful! :D

EDIT: here's an example of what i've tried doing on one of the slots but it keeps saying i need a cast to assign the integer from a pointer (i've tried searching online but idk how to do this)

char * slotOne(int randOne, const char *symbols[]) 
{ 
    randOne = rand() % 4 + 1; 

    if (randOne = 1)
    {
        randOne = *symbols;
    }
    if (randOne = 2)
    {
        randOne = *(symbols+1);
    }
    if (randOne = 3)
    {
        randOne = *(symbols+2);
    }
    else
    {
        randOne = *(symbols+3);
    }
    return randOne; 

}

this is the part of my main function where i've tried declaring the string array:

int main() 
{ 
    int x, one, two, three;
    const char *symbols[4] = {"bell", "orange", "cherry", "horseshoe"};

    srand(time(NULL)); 

    one = slotOne(x);
    two = slotTwo(x);
    three = slotThree(x); 

    printf("%s - %s - %s\n", one, two, three); 

    //...

} 

not sure if %s or %c is the right type too...

bachtick
  • 49
  • 6
  • 3
    Welcome to SO! Can you create an [array of strings](https://stackoverflow.com/a/1088667/530160), and use the random number to index that array? – Nick ODell Nov 14 '20 at 01:54
  • You don't need all three of functions `slotOne()`, `slotTwo()`, and `slotThree()` — any one of them suffices for all three. Well, it is not clear why you pass an argument into the function; it is simply used as a local variable that is overwritten (ignoring the value passed). You should probably use a single function: `static int slotNumber(void) { int value = rand() % 4 + 1; return value; }` (where the local variable isn't absolutely necessary, but it makes debugging easier — you can set a breakpoint on `return` to see what will be returned). You then call `one = slotNumber();` etc. – Jonathan Leffler Nov 14 '20 at 03:55
  • @JonathanLeffler i should've said above but this is "homework" and we were supposed to use more than 3 functions in the code but thank you for your suggestions! i'll try using them after finishing this – bachtick Nov 14 '20 at 12:33
  • @NickODell thanks! i'm a beginner so i'm not really sure what you mean by using the random number to index the array? the link to the thread of string arrays was very helpful though! – bachtick Nov 14 '20 at 12:45
  • @chux-ReinstateMonica i've posted it :)) – bachtick Nov 14 '20 at 12:46

1 Answers1

0

At least these problems:


Code is assigning = when it should compare ==.

// if (randOne = 1)
if (randOne == 1)

The last if () { ... } else { ... } will cause one of the 2 blocks to execute. OP wants an if () { ... } else if () { ... } else { ... } tree.

// Problem code
if (randOne = 3) {
    randOne = *(symbols+2);
} else {
    randOne = *(symbols+3);
}

Suggest

if (randOne == 1) {
    randOne = *symbols;
} else if (randOne == 2) {
    randOne = *(symbols+1);
} else if (randOne == 3) {
    randOne = *(symbols+2);
} else {
    randOne = *(symbols+3);
}

Also research switch.

switch (randOne) {
  case 1:
    randOne = *symbols;
    break;
  case 2:
    randOne = *(symbols+1);
    break;
  case 3:
    randOne = *(symbols+2);
    break;
  default:
    randOne = *(symbols+3);
    break;
}

Or consider a coded solution:

randOne = *(symbols+(randOne-1));

But code needs to return a pointer to a string not an int and has no need to pass in randOne as a parameter.

const char * slotOne(const char *symbols[]) { 
    int randOne = rand() % 4; 
    return symbols[randOne];
}

Calling code also needs to adjust to receive a const char *, not an int.

// int one;
// one = slotOne(x);
const char *one = slotOne(symbols);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256