C: function skips user input in code

2019-01-12 12:13发布

I am having a problem with this function (part of a Battleship game) where it will run through it once perfectly fine, but in subsequent executions, it skips:

    scanf("%c",&rChar);

For some reason, rChar turns into another value without user input from above code. I have tried putting in printf statements showing the value of rChar all throughout the function.

The function Conv_rChar_Int() converts the Char the user inputs into an integer value. But because rChar isn't being passed around as a pointer, the value of rChar remains the same throughout until the user replaces it on the next iteration. (Verified yet again with printf). The weird thing is, it changes right in between these lines of code. and never prompts the user for rChar.

    printf("please enter the row you want to place your %d ship in\n",length);
    scanf("%c",&rChar);

Remember, it only happens AFTER the first time. Even if I reinitialize the variables rChar, r, c, and dir after every iteration, this problem still happens. I am 99% certain the problem is within this function and not in any of the functions that gets called within it (because rChar remains the same after every single line except between the 2 lines above).

Thanks for help in advance. If you have any questions about the code I'll try to explain it more.

int Gen_Ship_Place(int length, int flag3, int PlayGrid[10][10]){
int ShipPlaceFlag = 0;

//coordinates and direction
int r;
char rChar;
int c;
int dir;

//this loops until a ship location is found
while(ShipPlaceFlag == 0)
{
    //enters row
    printf("please enter the row you want to place your %d ship in\n",length);
    scanf("%c",&rChar);

    r = Conv_rChar_Int(rChar);

    //adjusts row
    r--;
    //enter column
    printf("please enter the column you want to place your %d ship in\n",length);
    scanf("%d",&c);

    //adjust column
    c--;

    //enter direction
    printf("please enter the direction you want your %d ship to go\nwith\n0 being north\n1 being east\n2 being south\n3 being west\n",length);

    scanf("%d",&dir);

    //checks ship placement
    ShipPlaceFlag = Check_Ship_Place(length,dir,flag3,r,c,PlayGrid);

    //tells player if the location is wrong or not
    if(ShipPlaceFlag == 0)
    {
        printf("****the location and direction you have chosen is invalid please choose different coordinates, here is your current board*****\n\n");
    }
    else
    {
        printf("****great job, here is your current board*****\n\n");
    }

    //prints grid so player can check it for their next move
    Print_Play_Grid(PlayGrid);

}

2条回答
闹够了就滚
2楼-- · 2019-01-12 12:41

When the user presses enter, that is also a character which will be in the input buffer. You'll need to read past that.

//prints grid so player can check it for their next move
Print_Play_Grid(PlayGrid);
while (fgetc(stdin)!='\n') { }
查看更多
贼婆χ
3楼-- · 2019-01-12 12:46

Your program prints this prompt:

please enter the row you want to place your 2 ship in

and calls scanf. You type 5 and press return. You have typed in two characters: the 5 and a newline character \n. (Or maybe it's \r on Windows.) That newline character sits in the input buffer until the next call to scanf, which reads the newline character and returns right away without you needing to enter more input.

You can make scanf skip past newlines (and other whitespace) when reading a single character by putting a space before the %c specifier, like this:

scanf(" %c", &c);
查看更多
登录 后发表回答