2014-08-28 59 views
1

首先,我只想说我2周前没有听说过c,如果这给你一些想法我在哪里。无论如何,我正在努力编写我的第一个程序,允许用户做出选择并继续讲故事。在这里,我只会告诉你的代码:scanf(“%c”,x)和x = getchar都不在等待输入

#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循环和这样的工作很好。但是,如果我做按波浪号〜键,然后按回车,而不是从

scanf("%c", &xchoice); 

等待用户输入的程序简单地停止运行,终端只是追溯到显示我的当前目录。 (我正在为Windows编写Linux)。这显然不是预期的结果。此外,我也尝试插入一个循环,使getchar工作,直到输入被按下,并没有工作。我也尝试使用getchar以与上面的scanf语句相同的方式读取单个字符,但结果相同。我相信我错过了一些明显的东西......但我无法找到它。最后,我想知道是否有更好的方法在屏幕上显示大量文本(“djfasjlaksdj”);

非常感谢!

+0

你试过'scanf(“%c”,&xchoice);'? – haccks 2014-08-28 12:18:44

+3

'xchoice =='A'|| 'a''总是如此。你应该提高你的警告级别,因为我得到了一些很好的警告级别:*隐式声明函数'sleep'在C99 *中是无效的并且*使用逻辑'||'与恒定操作数* – chris 2014-08-28 12:20:01

+0

为什么克里斯?只是好奇...它不区分大小写吗?好吧,我会试试看,因为当我做到这一点时没有任何警告...... – 2014-08-28 12:20:58

回答

0

输入完毕后,按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. 
+0

刚刚重新编译成功,谢谢一堆:-) – 2014-08-28 12:30:21

+0

虽然这解决了这个问题,但它并不能解释为什么这些更改是必要的。 – dreamlax 2014-08-28 12:31:42

1

getchar()scanf小号读入一个字符并停止stdin阅读,但是当你输入一个选项,发送至少2个字符:您键入的选项和\n。您的代码只读取您的选项,并保留第二个电话的\n

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

而且,还有一点:你行:

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

不能按预期工作。你应该改变它:

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

getchar函数为标准输入读取单个字符。不幸的是,终端将不会发送任何字符到程序,直到您按下Enter ...所以这就是为什么您需要用户在〜getchar之后键入ENTER才能返回。然而,ENTER不被getchar读取,但被随后的输入读取。

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

附录。 在我看来,使用scanf(" %c"...)的建议并不好。图片如果用户写下“你好吗?”这样的字符串会发生什么?当你问一个字符...

0

只是scanf函数

fflush(stdin); 

好走之前刷新输入缓冲区。