2015-11-08 33 views
-3

所以我的代碼不斷破碎後,我的if if函數結束大括號。 它與返回EXIT_SUCCES有什麼關係,我用錯了嗎?代碼中斷後,如果其他功能在C

這裏是我的代碼:

// recursive.c 
// lynda 

#include <stdio.h> 
#include <stdlib.h> 
/* 
checks for traffic light if else example 
*/ 
void processColor(char c); 
void checkTraficLight(void); 
int main(void){ 
    checkTraficLight(); 
    return EXIT_SUCCESS; 
} 

void checkTraficLight(void){ 
    printf("what is the light? r, y, g: \n"); 
    char color; 
    scanf("%c", &color); 
    processColor(color); 
} 

void processColor(char c){ 
    if (c == 'r') { 
     printf("color is red"); 
    } else if (c == 'y'){ 
     printf("color is yellow"); 
    } else if(c == 'g'){ 
     printf("color is green"); 
    } else { 
     printf("U entered an invalid color"); 
    } 
} 

[從評論更新:]

但事實是,它停止它打印出我想要它打印出來讓我們說之前,我輸入「r」它應該打印出「顏色是紅色」,而是它只是說「(lldb)並停止,它不打印出」顏色是紅色的「

+1

你能更具體

printf("color is red\n"); 

或明確沖洗stdout?程序不會「打破」。他們要麼崩潰,要麼不按預期行事。請提供錯誤消息或一些示例輸出。 – Arc676

+1

「破」是什麼意思? – Jameson

+0

你的程序運行良好,可能是你想在你的程序中添加循環。 –

回答

0

我已經在代碼塊13.12上運行了你的代碼,假設它是它的工作沒有錯誤也沒有崩潰。

你的代碼的結構和第一行的註釋讓我想要寫一個遞歸函數。如果是這樣,你應該提供關於該功能目標的更多細節。

+0

這不知怎麼回答這個問題,儘管我可能有套房作爲評論。 – alk

0
printf("color is red"); 

printf()打印到標準輸出(又名stdout)。 stdout默認是行緩衝,所以只要不打印新行,它的內容就不會寫出來。

由於顯示的代碼不打印任何新行,因此不會打印出任何內容。

要解決此追加後的新行應該是什麼印刷:做

printf("color is red"); 
flush(stdout);