C:函数在代码中跳过用户输入 [英] C: function skips user input in code

查看:19
本文介绍了C:函数在代码中跳过用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个功能(战舰游戏的一部分)的问题,它会完美地运行一次,但在随后的执行中,它会跳过:

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);

由于某种原因,rChar 在没有用户输入上述代码的情况下变成了另一个值.我尝试在整个函数中放入显示 rChar 值的 printf 语句.

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.

Conv_rChar_Int() 函数将用户输入的 Char 转换为整数值.但是因为 rChar 没有作为指针传递,所以 rChar 的值始终保持不变,直到用户在下一次迭代中替换它.(使用 printf 再次验证).奇怪的是,它在这些代码行之间发生了变化.并且从不提示用户输入 rChar.

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
",length);
    scanf("%c",&rChar);

请记住,它只发生在第一次之后.即使我在每次迭代后重新初始化变量 rCharrcdir,这个问题仍然会发生.我 99% 确定问题出在这个函数中,而不是在其中调用的任何函数中(因为 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
",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
",length);
    scanf("%d",&c);

    //adjust column
    c--;

    //enter direction
    printf("please enter the direction you want your %d ship to go
with
0 being north
1 being east
2 being south
3 being west
",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*****

");
    }
    else
    {
        printf("****great job, here is your current board*****

");
    }

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

}

推荐答案

你的程序会打印这个提示:

Your program prints this prompt:

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

并调用scanf.您键入 5 并按回车键.您输入了 两个 字符:5 和换行符 .(或者它可能是 Windows 上的 .)该换行符位于输入缓冲区中,直到下一次调用 scanf,它读取换行符并在没有你的情况下立即返回需要输入更多信息.

and calls scanf. You type 5 and press return. You have typed in two characters: the 5 and a newline character . (Or maybe it's 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.

您可以通过在 %c 说明符之前放置一个空格来使 scanf 在读取单个字符时跳过换行符(和其他空格),如下所示:

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);

这篇关于C:函数在代码中跳过用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆