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

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

问题描述

我有这个功能(战舰游戏的一部分),在那里将通过它运行一次完全没有问题,但在后续执行,它会跳过:

  scanf函数(%C,&安培; rChar);

由于某些原因,rChar变成另一个值,而从上面code用户输入。
我试图把在的printf 显示所有在整个函数rChar值的语句。

功能 Conv_rChar_Int()用户输入转换成字符的整数值。但由于 rChar 不被传来传去为指针,值 rChar 保持,直到用户在整个同取代它的下一次迭代。 (与验证又一次的printf )。奇怪的是,它的权利在code,这些线路之间变化。从不提示用户输入 rChar

 的printf(请输入您想要将%d个船\\ n行,长度);
    scanf函数(%C,&安培; rChar);

请记住,这只是发生后的第一次。即使我重新初始化变量 rChar 研究 C DIR 每次迭代之后,这个问题仍然发生。
我99%肯定,问题是在这个函数中,而不是在任何的被在它所谓的(因为 rChar 的功能仍然是除2之间的每一个行后相同以上线路)。

感谢您提前帮助。如果您有关于code有任何问题,我会尝试更多的解释。

  INT Gen_Ship_Place(INT长,诠释Flag3相同的,诠释PlayGrid [10] [10]){
INT ShipPlaceFlag = 0;//坐标和方向
INT R;
焦炭rChar;
INT℃;
INT目录;//这个循环,直到船舶的位置被发现
而(ShipPlaceFlag == 0)
{
    //进入排
    的printf(请输入您想要将%d个船\\ n行,长度);
    scanf函数(%C,&安培; rChar);    R = Conv_rChar_Int(rChar);    //调整一行
    R--;
    //输入列
    的printf(请输入您想要将%d个船\\ n列,长度);
    scanf函数(%D,和C);    //调整列
    C - ;    //进入方向
    的printf(请输入您希望您的%d个船前进的方向\\ n执行\\ N0是北\\ n1的福祉东\\ N2是南\\ N3是西\\ n,长度);    scanf函数(%d个,&安培; DIR);    //检查船舶的位置
    ShipPlaceFlag = Check_Ship_Place(长度,DIR,Flag3相同的,R,C,PlayGrid);    //告诉球员,如果位置不对或不
    如果(ShipPlaceFlag == 0)
    {
        的printf(****您所选择的位置和方向是无效的,请选择不同的坐标,这里是你的现任董事会***** \\ n \\ n);
    }
    其他
    {
        的printf(****伟大的工作,这是你的现任董事会***** \\ n \\ n);
    }    //打印网格,以便玩家可以检查它的下一步行动
    Print_Play_Grid(PlayGrid);}


解决方案

您程序打印这样的提示:

 请输入您要放置2船行

和通话 scanf函数。键入 5 和preSS的回报。在 5 和换行符 \\ n :您可以在两个字符键入。 (或者,也许这是 \\ r 在Windows上)。这换行符坐在输入缓冲区直到 scanf函数下一次调用,它读取换行符并马上返回你无须进入更多的投入。

您可以 scanf函数由之前的%C 符,就像这样:

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

}

解决方案

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

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

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