2013-08-21 66 views
-1

我什至不知道如何使這項工作! 所以我把這個東西寫在了正確的地方,在它要求輸入的部分,我什麼都不能輸入!請幫忙T_T 我在做什麼是一個問題&答案時間程序,用戶只能在限定的時間內回答問題。 這裏是我的代碼C無法在scanf中輸入

#include <stdio.h> 
#include "engine.h" 

void level2(){ 
system("cls"); 


printf("Level 2 \n\n\n"); 
timer(); 
if (time_limit >=0) { 

     printf("BOO"); 
     } 
} 


int level1() { 
char answer; 
printf("Hello? \n\n"); 
scanf("%c",&answer); 

time_limit = 20; 
timer(); 



     } 
int rules() { 
time_limit = 15; 
system("cls"); 
printf(" Game Rules \n\n"); 
printf(" 1. You only have 5 seconds to guess what the correct answer for the question! \n"); 



} 
int credits() { 
int menu; 
printf(" Coded by : \n"); 
printf("Lady Dianne Vertucci"); 

} 
int main() { 
int menu; 
printf("TR \n "); 
printf("1. Play \n"); 
printf("2. Credits \n"); 
scanf("%d",&menu); 
switch (menu) { 
     case 1: 

      level1(); 

      break; 
      case 2: 
        credits(); 
        break; 
        default: 
          printf("Please choose a valid option 'tard"); 
          break; 
     } 
    getch(); 
    return 0; 
} 

這是engine.h

#ifndef _ENGINE_H_ 
#define _ENGINE_H_ 

static int time_limit = 0; 
extern int time_limit; 
static int score = 0; 
extern int score; 


int timer() { 




     while (time_limit !=0) 
     { 


     time_limit--; 
     printf("%02d\r",time_limit); 

     sleep(1000); 
     } 

} 

#endif 
+0

什麼是'time_limit'? – haccks

+0

還介紹一下'engine.h'頭文件。 – haccks

+0

已更新帖子 – xDianneCodex

回答

-3

使用 flushall(); scanf函數

+0

flushall()和fflush()僅針對輸出流定義。 – Medinoc

+0

它說未定義的參考 – xDianneCodex

0

在功能level1()之前,你應該改變

scanf("%c",&answer); 

scanf(" %c",&answer); 
    ^
     | 
    space 

這將進入一個char之後吃起來換行符\n並按下Enter鍵。

+0

定時器將不會啓動,直到我輸入東西... – xDianneCodex

+0

@xDianneCodex;希望這會起作用。 – haccks

+0

相同的結果... T_T – xDianneCodex

0

喚醒沉睡的頭

您的通話sleep(1000)休眠1000秒。懷疑你想要sleep(1)

也推薦@haccks建議使用" %c"