1

I am trying to make a programming assignment in my school that requires me to create a dice game in C where the winner is determined based on which sum of 10 dice rolls (between the computer and the player) is higher. However I seem to have encountered a problem where my program would add the sum of the computer dice rolls to that of the user causing a guaranteed win to the user. The program must make use of the srand and rand function and I currently have no idea how to fix this due to this being my entry into programming. Below is the code I have made as I do not how to properly explain this and I have no clue the error could be. Thank you very much for any possible help as I cannot contact my professor at the moment

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

//Computer Dice Roll
Comp_Dice(int CP_Value, int CP_Sum){
    srand(time(0));
    for(int i = 0; i <10; i++){
    CP_Value = rand()%6+1;
    printf("%d ", CP_Value);
    CP_Sum += CP_Value;

    }
    printf("\nPlayer 1 Total: %d", CP_Sum); 
}
//End of Computer Dice Roll

//Player Data
Plyr_Dice(int Plyr_Value, int Plyr_Sum, int x){
    srand(x);
    for(int e = 0; e <10; e++){
    Plyr_Value = rand()%6+1;
    printf("%d ", Plyr_Value);
    Plyr_Sum +=Plyr_Value;
    
    }
    printf("\nPlayer 2 Total: %d", Plyr_Sum);
}
//End of Player Data

int main(){
    int x, CP_Value, CP_Sum, Plyr_Value, Plyr_Sum, sel;
    
    system("cls");
    
    do{ 
    system("cls");
    printf("\n\t\tDice Roll Game");
    printf("\n\t1.Play");
    printf("\n\t2.Exit");
    printf("\nSelect>>: ");
    fflush(stdin);
    scanf("%d" ,&sel);
    printf("\n\n\n");
    
    switch(sel){
        case 1: printf("Computer dice generated: ");
                Comp_Dice(CP_Value, CP_Sum);
    
                printf("\nPlayer dice has been generated please Input integer seed: ");
                scanf("%d", &x);
    
                Plyr_Dice(Plyr_Value, Plyr_Sum, x);
                if (Plyr_Sum>CP_Sum){
                printf("\nPlayer 2 Wins!\n");
                }else printf("\nPlayer 1 Wins!\n");
                break;
        case 2: printf("Thank you for playing");
                break;
        default: printf("invalid input please choose 1 or 2\n");
        }
        system("pause");
    }while(sel!=2);
return 0;   
}
Shigeo
  • 13
  • 3
  • 2
    You should use `srand` only *once*, in `main`. – Bob__ Aug 12 '20 at 13:59
  • Do you have any analysis about your problem or it's all about your assumption ?? – RafsanJany Aug 12 '20 at 14:00
  • Also, you may not be aware of that, in C, the function's arguments are passed by value. Whatever happens to those values (the local copies) doesn't reflect to the variables in the calling scope (`Plyr_Sum` in `main`, which is *not* initialized, *is not* the `Plyr_Sum` in `Plyr_Dice`). – Bob__ Aug 12 '20 at 14:08
  • Moreover, functions that don't return anything (even if they probably *should*) must be declared as `void`. – Bob__ Aug 12 '20 at 14:16
  • Shingo - you asked for help, but have not replied to any comments. Are you here? – ryyker Aug 12 '20 at 14:47
  • 1
    @ryyker yes I am here though I'm struggling because this is the first time I have used such a platform and have no idea how to reply due to me being unfamiliar with the terms, jargons and the likes. Also because I just literally a few seconds ago learned to reply to a specific user. I still need to know how to reply to an answer – Shigeo Aug 12 '20 at 14:58
  • 1
    @ryyker regarding the advise of using srand only once and specifically within the function main() I am still trying to find a way for it to be able to switch the seed between time(0) and x as the instructions of the homework require that the user has a different seed input. I have done as you suggested and initialized the values however my program now gives me an error that there is an invalid conversion between int and int when I call my function Plyr_Dice – Shigeo Aug 12 '20 at 15:05
  • @Shigeo - Thank you for commenting. If there is specific instruction to re-seed `srand()`, then by all means do that, it is just not typical to do so. Regarding _"...invalid conversion between int and int when I call my function Plyr_Dice._ If it pertains to something I have suggested you do, please comment under my answer more specific information, otherwise comment here again. By the way, when commenting under a post to the author, no @ is needed, i.e. if you comment under my answer below, you do not need to tag me, I will see it. – ryyker Aug 12 '20 at 15:12
  • 1
    @ryyker I have done as you said and switched my code – Shigeo Aug 12 '20 at 15:15
  • See edits to my answer. I think I have identified your concerns. – ryyker Aug 12 '20 at 15:39

2 Answers2

0

Your management of the function parameter is wrong. Plyr_Sum and CP_Sum cannot be integers if you want the functions to modify them.

The function prototypes should be:

  • void Comp_Dice(int CP_Value, int *CP_Sum), then don't forget to initialise CP_Sum in the main.

  • int Comp_Dice(int CP_Value), simply return the sum.

By the way you should use unsigned variables.

Guillaume Petitjean
  • 2,408
  • 1
  • 21
  • 47
0

If you address the things the compiler hints at when warnings are turned on, your code will be closer to running correctly.

Here is the list my compiler shows:

Build Status (prog.prj - Debug)
 main29.c - 6 warnings
  16, 1    warning: function does not return a value 
  29, 1    warning: function does not return a value 
  49, 27    warning: variable 'CP_Value' may be uninitialized when used here 
      33, 20    note: initialize the variable 'CP_Value' to silence this warning
  49, 37    warning: variable 'CP_Sum' may be uninitialized when used here 
      33, 28    note: initialize the variable 'CP_Sum' to silence this warning
  54, 27    warning: variable 'Plyr_Value' may be uninitialized when used here 
      33, 40    note: initialize the variable 'Plyr_Value' to silence this warning
  54, 39    warning: variable 'Plyr_Sum' may be uninitialized when used here 
      33, 50    note: initialize the variable 'Plyr_Sum' to silence this warning
 Link play.exe
 "c:\play\prog.exe" successfully created.
Build succeeded.

Some other suggestions:

As per C standard, it is undefined behavior to use fflush(stdin).

Place srand() in the beginning of main(){, and call only once during execution. More on proper use of srand() here` here

To change the value of a variable passed as an argument in any function, it is not the variable itself that is passed, it is the address of that variable.

Change the prototype of Comp_Dice to:

Comp_Dice(int CP_Value, int *CP_Sum){

Then call it as shown:

Comp_Dice(CP_Value, &CP_Sum);//& is the `address of` operator  

Change prototype of Plyr_Dice to:

void Plyr_Dice(int Plyr_Value, int Plyr_Sum, int x);
^^^^  

Same with Plyr_Dice

In your first call to Comp_Dice, CP_Value is not initialized. True for several other variables as well. Plyr_Value, Plyr_Sum,... Initialize all of them, even if you believe they will be assigned values in first use. Its is just good practice to initialize.

EDIT to address specific questions in comments on code.

Following is your code modified to run. I have commented out a few things that do not work on my system, and things that I have identified are wrong. It may need some debugging, but it should run.

//Computer Dice Roll
void Comp_Dice(int CP_Value, int *CP_Sum){
    srand(time(0));
    for(int i = 0; i <10; i++){
    CP_Value = rand()%6+1;
    printf("%d ", CP_Value);
    *CP_Sum += CP_Value;

    }
    printf("\nPlayer 1 Total: %d", *CP_Sum); 
}
//End of Computer Dice Roll

//Player Data
void Plyr_Dice(int Plyr_Value, int *Plyr_Sum, int x){
    srand(x);
    for(int e = 0; e <10; e++){
    Plyr_Value = rand()%6+1;
    printf("%d ", Plyr_Value);
    *Plyr_Sum +=Plyr_Value;
    
    }
    printf("\nPlayer 2 Total: %d", *Plyr_Sum);
}
//End of Player Data

int main(void){
    int x, CP_Value=0, CP_Sum, Plyr_Value=0, Plyr_Sum, sel;
    
    //system("cls");//okay, but does not work on my system.
    
    do{ 
    //system("cls");
    printf("\n\t\tDice Roll Game");
    printf("\n\t1.Play");
    printf("\n\t2.Exit");
    printf("\nSelect>>: ");
    //fflush(stdin);// undefined behavior
    scanf("%d" ,&sel);
    printf("\n\n\n");
    
    switch(sel){
        case 1: printf("Computer dice generated: ");
                Comp_Dice(CP_Value, &CP_Sum);
    
                printf("\nPlayer dice has been generated please Input integer seed: ");
                scanf("%d", &x);
    
                Plyr_Dice(Plyr_Value, &Plyr_Sum, x);
                if (Plyr_Sum>CP_Sum){
                printf("\nPlayer 2 Wins!\n");
                }else printf("\nPlayer 1 Wins!\n");
                break;
        case 2: printf("Thank you for playing");
                break;
        default: printf("invalid input please choose 1 or 2\n");
        }

        //system("pause");//okay, but does not work on my system.
    }while(sel!=2);
    return 0;   
}
ryyker
  • 22,849
  • 3
  • 43
  • 87
  • I have done as you said and switched my the variable types of the function of CP_Dice as you have suggested and had used & to call the variable CP_Sum I have also changed the the initialization type of Plyr_Dice function to void. However within the line where I call Plyr_Dice in main it states _invalid conversion between int to int [-fpermissive]. Here is the line of where I called Plyr_Dice 'Plyr_Dice(Plyr_Value, &Plyr_Sum, x);' I apologize for improper use of terms in my explanation. – Shigeo Aug 12 '20 at 15:24
  • @Shigeo - See additional edit in my answer. I edited your code according to my suggestions, and commented out a few things, run it and let me know if it works for you. Pay attention to how the argument pointers are updated in the functions. That is key here. – ryyker Aug 12 '20 at 15:38
  • It managed to make the program run however now I encountered multiple bugs, the sum for both the CP_Sum and Plyr_Sum have reached to values of up to 6000 and my if statement for determining the winner is fixed at stating that the CPU wins regardless of it having a lower values than the player – Shigeo Aug 12 '20 at 15:43
  • @Shigeo - Honestly, I can help you with that, but in all respect, do you really want me to? The tips, suggestions and content already provided should really help you to step through the code to find where the bug exists. Are you running in debug mode, where you can step through the code? You can also insert `printf()` statements where needed to help identify values that may be wrong. – ryyker Aug 12 '20 at 15:49
  • 1
    You have helped quite a lot, thank you I am going to try and learn to fix this bug on my own and use the tips presented as guide and references for current and future problem I may encounter. – Shigeo Aug 12 '20 at 15:53