首先,我只想说我 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 循环等工作正常。但是,如果我确实按波浪号 ~ 键,然后按 enter,而不是等待来自
的用户输入scanf("%c", &xchoice);
程序只是停止运行,终端只是返回显示我的当前目录。 (我在 Linux for Windows 上写作。)这显然不是预期的结果。我也确实尝试插入一个循环以使 getchar 工作,直到按下 enter 并且这也不起作用。我还尝试使用 getchar 以与上述 scanf 语句相同的方式读取单个字符,但结果相同。我确信我遗漏了一些明显的东西......但我就是找不到它。最后,我想知道除了一堆 puts("djfasjlaksdj"); 之外,是否有更好的方法在屏幕上显示大量文本;
非常感谢!
最佳答案
getchar
函数读取标准输入的单个字符。不幸的是,在您按下 enter 之前,终端不会向程序发送任何字符...所以这就是为什么您需要用户在 ~ 之后键入 ENTER 以便 getchar
返回。但是,getchar
不读取 ENTER,而是由后面的输入读取。
因为我们有一个面向行的用户界面,所以最好的办法是始终读取整行,然后将其解释为单个字符、命令或其他任何内容。读取单行而不冒溢出风险的最佳选择是使用 fgets
函数。
附录。
我认为使用 scanf("%c"...)
的建议并不好。想象一下,如果用户写了一个像“你好吗?”这样的字符串会发生什么?当您要求单个字符时...
关于c - scanf ("%c", x) 和 x=getchar 都没有等待输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25548647/