Distorts
Distorts

Reputation: 21

Case/Switch in C not working

I am making a game for school this is a basic slot machine it will randomly generate numbers and converts the numbers to chars in a separate array. This seems to not be working as the statement is completely ignored. It doesn't give the right output and some time they will just go to blanks.

Output: http://distorts.me/imgs/files/132ca7cc1da6fc459de3e498d8e12b9f.png

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
/*
George Mason
Slots
Date Started: 4/25/16
Date Finished:
Dr K.
*/
void loadScreen();
void spacer();
void tabSpacer();
void clearScr();
int printMachine(char*, int);
int randomNum(int*, int);
int convertNum(int*, char*, int);
int rand_int();
void game();
int main(){
    srand(time(NULL));
    int stop;
    loadScreen();
    clearScr();
    game();
    scanf("%d", &stop);
}
void game(){
    int tokens = 5, randomNums[9], x, y = 9;
    char randomChars[9], userInput;
    randomNum(randomNums, 9);
    convertNum(randomNums, randomChars, 9);
    printMachine(randomChars, 9);

}
int printMachine(char* randomChars, int y){
    printf("-------------\n");
    printf("| %c | %c | %c | \n", randomChars[0], randomChars[1], randomChars[2]);
    printf("| %c | %c | %c | \n", randomChars[3], randomChars[4], randomChars[5]);
    printf("| %c | %c | %c | \n", randomChars[6], randomChars[7], randomChars[8]);
    printf("-------------");
}
int randomNum(int* randomNums, int y){
    int x,a = 0, b = 9;
    for(x = 0; x < y; x++){
        randomNums[x] = ((rand() % (b-a+1)) + a);
    }
}
int convertNum(int* randomNums, char* randomChars, int y){
    int x;
    for(x = 0; x < 9; x++){
            switch(randomNums[x]){
                case 1:
                     randomChars[x] = '@';
                     break;
                case 2:
                     randomChars[x] = '#';
                     break;
                case 3:
                     randomChars[x] = '$';
                     break;
                case 4:
                     randomChars[x] = '+';
                     break;
                case 5:
                     randomChars[x] = '&';
                     break;
                case 6:
                     randomChars[x] = '*';
                     break;
                case 7:
                     randomChars[x] = '?';
                     break;
                case 8:
                     randomChars[x] = '!';
                     break;
                case 9:
                     randomChars[x] = '~';
                     break;
                default:
                     randomChars[x] = 'e';
                     break;
          } 
    }
}
void loadScreen(){
     int x;
     for(x = 0; x < 3; x++){
         spacer();
     }
     tabSpacer();
     printf("Please wait 5 seconds while we load the saved data.\n");
     tabSpacer();
     printf("   If there is no saved data one will be created.");
     sleep(5);
}
void spacer(){
     int x;
     for(x = 0; x < 3; x++){
           printf("\n");
     }
}
void tabSpacer(){
     printf("\t    ");
}
void clearScr(){
     system("cls");
}

Upvotes: 0

Views: 137

Answers (1)

Himanshu
Himanshu

Reputation: 4395

You have forgot to keep break; after every case so it will run all cases i.e from case 1 to case 9.
At the end it will save randomChars[x] = '~'; (case 9).

case 1:
    randomChars[x] = '@';
    break;
case 2:
    randomChars[x] = '#';
    break;
    .
    .
    .
    .

case 7:
    randomChars[x] = '?';
    break;
case 8:
    randomChars[x] = '!';
    break;
case 9:
    randomChars[x] = '~';
    break;

EDIT:

  • why your function return type is int when you are not returning any value. change int to void.

  • function game() is not declared before main()

  • I am getting Output like this:
    enter image description here

Upvotes: 5

Related Questions