nkoenigs
nkoenigs

Reputation: 1

scanf() is getting stuck on final use

I'm trying to use scanf to assign values to some integers but whichever scanner I put last will get suck as if it it wasn't seeing the new line. I've tried flushing the input buffer before every scan, but its hasn't helped. Do i need to malloc the pointers, even if they are being passed as the address of an existing variable out of function?

code:

void settings(int *x, int *y, int *l, int *m){
printf("Please enter the size of the game board (e.g. 3x6): ");
scanf("%dx%d",x,y);
printf("Please select the level of the game (0-9): ");
scanf("%d",l);
printf("Please enter the largest card value (1-99): ");
scanf("%d",m);
printf("Please enter the seed of the random number generator: (0-9999): ");
int s;
scanf("%d",&s);
srand(s);
return;}

Thanks

Upvotes: 0

Views: 116

Answers (1)

Shahar Lahav
Shahar Lahav

Reputation: 80

Actually, it should work. Pay attention that your pointers should be initialized before the call, they can't be NULL:

    int *x = (int *)malloc(sizeof(int));
    int *y = (int *)malloc(sizeof(int));
    int *l = (int *)malloc(sizeof(int));
    int *m = (int *)malloc(sizeof(int));


    settings(x,y,l,m);

No problem besides that...

Upvotes: 1

Related Questions