scanf(“%c”,x)和x = getchar都不等待输入

时间:2014-08-28 12:17:26

标签: c linux gcc

首先,我只想说我2周前没有听说过c,如果这让你知道我在哪里。无论如何,我试图编写我的第一个程序,允许用户做出选择并继续讲故事。在这里,我将向您展示代码:

#include <stdio.h>

//Global variables for entire program

 char proceed;  //Used to confirm that user wishes to play
 int countdwn=5;  //Used in do-while loop to count down from red value
 char xchoice; //Used in ABC  Multiple Choice Selections

int main()
{
//Introduction sequence displays a block of text for user.

    puts("\nGreetings,\n");
    puts("I am glad that you made it this far!");
    puts("Now, you will discover if it was worth your while!");
    puts("In this program you will be provided with some information");
    puts("which you must use to make wise decisions...");
    puts("Whether you survive or not remains to be seen");
    puts("But whatever your outcome, you are soley responsible");
    puts("as you are the one making the choices... for good or ill!\n\n");

//In order for the user to continue, user must type a '~' followed by enter.

    puts("Are you ready to begin!?!?");
    puts("If so press \"~\" followed by enter/return---");
    proceed=getchar();

    if(proceed!='~')
    {
            puts("\nIt seems that you don't want to continue.");
            puts("The program will now close and return you to dos");
            puts("Once the program has closed the cursor will begin to");
            puts("blink again and then type \"exit\" to exit dos");
            sleep(7);
            puts("Program Closing in 5...");

    do
    {
            printf("%d\n", countdwn);
            sleep(1);
            countdwn--;
    }
    while(countdwn>0);
    return 0;
    }

/*************************************************************************/

    puts("\nYou are living in Japan during a time known as the");
    puts("\"Sengoku Jidia,\" or age of the warring states.");
    puts("You are a Ninja --- a secret, deadly warrior.");
    puts("What kind of missions will you take on?");
    puts("What kind of dangers will you face?");
    puts("Keep going to find out!\n");
    puts("Make a selection by typing the number associated");
    puts("with the selection you wish to make, followed by hitting enter.");
    puts("Then, the next choice will automatically appear below.\n");
    puts("a. To help in the attack on an enemy castle in 1558");
    puts("b. To defend your homeland from an enemy attack in 1581");
    puts("c. To serve as a bodygard to a powerful ruler in 1600");
    puts("Make your selection a, b, or c followed by enter.");

    scanf("%c", &xchoice);

    if(xchoice=='A' || 'a')
    {
            puts("\nYou stand alone, looking up at the walls of");
            puts("Sawayama Castle near the city of Hikone.");
            puts("Activity buzzes all around you. You're a mercenary");
            puts("ninja, fighting for whomever pays you the most for your");
            puts("services. Today, you're part of an army fighting under");
            puts("Rokkaku Yoshita the samurai leader of the Rokkaku clan.");
            puts("The dodo clan is supposed be your ally, but some have");
            puts("rebelled and taken control of one of your castles.");
            puts("You have no choice but to fight to regain your loss.\n");
            puts("You are one of 50 ninjas hired to take part in the");
            puts("seige. Your leader, Doshun, is already forming plans");
            puts("on how to get inside. Doshun is a clever man and a");
            puts("respected ninja. But as night approaches, you can't");
            puts("help feeling that the time to strike is now.\n\n");
            puts("You stare at the castle wall. You know you could get");
            puts("inside. Then you could spy on the enemy or set fires");
            puts("that would drive the enemy leader, Kuranosuke from");
            puts("hiding. But Doshun is your leader. He will have a plan,");
            puts("and it might be best to find out what it is.\n\n");
            puts("a. To try to get inside the castle walls alone.");
            puts("b. To wait for Dashun's plan.");

/*****************************************************************************/

            }


    return(0);
}

这是我到目前为止所写的全部内容,但我打算继续提供更多选项。你几乎可以看到它的发展方向。该程序编译时没有任何警告或错误。具有倒计时的do-while循环和这样的工作正常。但是,如果我按下tilde~键,然后按Enter键,而不是等待来自

的用户输入
scanf("%c", &xchoice);  

程序停止运行,终端只返回显示当前目录。 (我在Linux for Windows上写作。)这显然不是预期的结果。此外,我确实尝试插入一个循环来使getchar工作,直到输入被按下并且也没有工作。我也尝试使用getchar以与上面的scanf语句相同的方式读取单个字符,但结果相同。我确信我错过了一些明显的东西......但我无法找到它。最后,我想知道是否有更好的方法在屏幕上显示大量文字,除了一堆看跌期权(&#34; djfasjlaksdj&#34;);

非常感谢!

4 个答案:

答案 0 :(得分:3)

getchar函数读取stdin的单个字符。不幸的是,终端不会向程序发送任何字符,直到你输入...这就是为什么你需要用户在〜getchar返回后输入ENTER。但是,getchar不会读取ENTER,而是由后面的输入读取。

最好的办法是,因为我们有一个面向行的用户界面,所以要始终读取整行,然后将其解释为单个字符,命令或其他内容。读取单行而不冒溢出风险的最佳选择是使用fgets函数。

<强>附录 在我看来,使用scanf(" %c"...)的建议并不好。图像如果用户写了“你好吗?”这样的字符串会发生什么?当你要求一个角色时......

答案 1 :(得分:1)

您的getchar()scanf会读取一个字符并停止从stdin读取,但是当您输入一个选项时,您会发送至少2个字符:您输入的选项和{{1 }}。您的代码只读取您的选项,并留下\n进行第二次调用。

为了避免阅读行尾或任何其他空格或制表符,您应该使用\n(注意scanf(" %c", &xchoice)之前的空格)。

另外一件事:你的路线:

%c

无法按预期工作。你应该改为:

if(xchoice=='A' || 'a')

答案 2 :(得分:0)

输入并按ENTER键或打印输入字符串后,如果在输入缓冲区中放置了两个字符,则它们是:由于ENTER键而输入的字符和换行符。输入的字符被getchar()消耗,但换行符仍保留在输入缓冲区中。因此下一个\n会消耗scanf()。为了避免在%c -

之前尝试使用空格
scanf(" %c", &xchoice);  // Fix 1
       ^  <-Note the space here

if(xchoice=='A' || 'a')

评估始终为真。怎么样?您仅使用A检查输入。不是a。因此,如果xchoice=='A'失败,则'a'始终为真。所以试试

 if(xchoice == 'A' || xchoice == 'a')) // Fix 2.

答案 3 :(得分:0)

只需在scanf

之前刷新输入缓冲区
fflush(stdin);

很高兴。