1

I'm making a simple game in c, and am having troubles passing and receiving information from my function t.

I want the code to essentially run the t function, then print the resulting score. Then, unless one of the scores is greater than three and two greater than the other score, I want it to run t again and print the result.

right now it is running the t function once then getting stuck.

Any help or ideas would greatly appreciated, or if I left out information leave a comment.

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

void t(int *score1,int *score2)
{
    int r,p;
    r=rand() % 140;
    if(r>30)
    {
        *score1=score1+1;
    }
    if(30>r)
    {
        *score2=score2+1;
    }
    if(r==30)
    {
    *score2=score2+1;
    }
}

void main(int argc, char *argv[])
{
    srand(time(NULL));
    int score1 =0;
    int score2 =0;
    int x =0;
    while(x==0)
    {
        t(&score1, &score2);
        printf("%d\n",score1);
        printf("%d\n",score2);

        if(score1 > 3 && score1==score2+2)
        {
            printf("Player 1 Wins\n");
            x++;
        }
        if(score2 > 3 && (score2==score1+2))
        {
            printf("Player 2 Wins\n");
            x++;
        }
    }
    return;
}
chouaib
  • 2,763
  • 5
  • 20
  • 35
Kydos Red
  • 23
  • 4
  • `atoi()` parses `const char *`, not `int *`. it is intended to parse ascii code digits. also you don't need to run `srand(time(NULL))` multiple times. move it to previous line of while loop. – ymonad Aug 12 '14 at 01:34
  • I changed what you said. I am just not getting any good values for score1 and score2. They create strange values. what am i doing wrong? – Kydos Red Aug 12 '14 at 01:40
  • If you learn to properly indent and format your code, it makes issues with looping much easier to trace. You might try it here. – Ken White Aug 12 '14 at 01:42
  • the `score1` and `score2` inside void t(){...} is a pointer, which value is usually a memory address. so you have to change it to `*score1=*score1+1` or `*score+=1` – ymonad Aug 12 '14 at 01:43

2 Answers2

1

Explanation: Don't invoke srand(time(NULL)) inside your t function. srand() creates a seed based on time, but if you invoke it everytime, within a short period of time. See srand() — why call it only once?

Code:

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

void t(int *score1,int *score2){
    int r,p;
    r=rand() % 140;

    printf("Random r: %d\n",r);
    if(r>30){
       (*score1)++;
    }
    else {
       (*score2)++;
    }
}

void main(int argc, char *argv[]){

int score1 =0;
int score2 =0;
int x =0;

srand(time(NULL));

while(x==0){
    t(&score1, &score2);
    printf("Score 1: %d\n",score1);
    printf("Score 2: %d\n",score2);

    if(score1 > 3 && score1==score2+2){
        printf("Player 1 Wins\n");
        x++;
    }

    if(score2 > 3 && (score2==score1+2)){
        printf("Player 2 Wins\n");
        x++;
    }
}

return;
}

Sample Output:

Random r: 138
Score 1: 1
Score 2: 0
Random r: 2
Score 1: 1
Score 2: 1
Random r: 103
Score 1: 2
Score 2: 1
Random r: 26
Score 1: 2
Score 2: 2
Random r: 20
Score 1: 2
Score 2: 3
Random r: 12
Score 1: 2
Score 2: 4
Player 2 Wins
Community
  • 1
  • 1
rurouni88
  • 1,165
  • 5
  • 10
0

this works :

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

void t(int *score1,int *score2)
{
    int r,p;
    r=rand() % 140;
    printf("%d\n", r);
    if(r>30)
    {
        *score1+=1; //increase score1 and not pointer of score1
    }
    else
    {
        *score2+=1; //increase score2 and not pointer of score2
    }

}

void main(int argc, char *argv[])
{
    srand(time(NULL));
    int score1=0, score2=0;
    int x =0;
    while(x==0)
    {
        t(&score1, &score2);
        printf("%d ",score1);
        printf("%d\n",score2);

        if(score1 > 3 && score1==score2+2)
        {
            printf("Player 1 Wins\n");
            x++;
        }
        if(score2 > 3 && (score2==score1+2))
        {
            printf("Player 2 Wins\n");
            x++;
        }
    }
    return;
}
chouaib
  • 2,763
  • 5
  • 20
  • 35