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

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

问题描述

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

 scanf("%c",&rChar);

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

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

 printf("请输入您要放置%d船的行
",length);scanf("%c",&rChar);

记住,它只发生在第一次之后.即使我在每次迭代后重新初始化变量rCharrcdir,这个问题仍然发生.我 99% 确定问题出在这个函数内,而不是在其中调用的任何函数中(因为 rChar 在每一行之后都保持不变,除了上面的 2 行之间).

提前感谢您的帮助.如果您对代码有任何疑问,我会尝试对其进行更多解释.

int Gen_Ship_Place(int length, int flag3, int PlayGrid[10][10]){int ShipPlaceFlag = 0;//坐标和方向国际r;字符 rChar;国际 c;内部目录;//此循环直到找到船舶位置while(ShipPlaceFlag == 0){//进入行printf("请输入您要放置%d船的行
",length);scanf("%c",&rChar);r = Conv_rChar_Int(rChar);//调整行r--;//输入列printf("请输入您要放置%d船的列
",length);scanf("%d",&c);//调整列C - ;//输入方向printf("请输入您希望您的 %d 艘船前往的方向

0 为北
1 为东
2 为南
3 为西
",length);scanf("%d",&dir);//检查船舶位置ShipPlaceFlag = Check_Ship_Place(length,dir,flag3,r,c,PlayGrid);//告诉玩家位置是否错误如果(ShipPlaceFlag == 0){printf("****您选择的位置和方向无效,请选择不同的坐标,这里是您当前的棋盘*****

");}别的{printf("****干得好,这是你当前的板子*****

");}//打印网格以便玩家可以检查它的下一步行动Print_Play_Grid(PlayGrid);}

解决方案

你的程序打印这个提示:

请输入您要放置您的 2 艘船的行

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

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

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

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
",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
",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

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.

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天全站免登陆