2013-10-09 36 views
0

我正在創建一個詢問一些問題並顯示結果的民意調查。一個是計算工作和結婚的女學生的百分比。我一直得到百分比結果爲0.00,我不確定原因。下面是我的項目創建C程序計算百分比給我一個問題

char pollAnswer[0]; 
char Gender[0]; 
int i = 0; 
float female = 0; 
int enteredAge; 
float age = 0; 
char work[0]; 
char married[0]; 
float workMarried=0; 
float percentFemale; 
int numChildren; 
float childrenAge; 
int socialMedia; 
int twitter = 0; 
int facebook = 0; 
int google = 0; 
int linkedln = 0; 




do { 
    printf("1) Gender? (F/M)\n"); 
    scanf("%s", &Gender[i]); 

    if (Gender[i] == 'F') { 
     female++; 
    } 

    printf("2) How old are you?\n"); 
    scanf("%d", &enteredAge); 

    if (enteredAge <= 25) { 
     age ++; 
    } 

    printf("3) Do you work? (Y/N)\n"); 
    scanf("%s", &work[i]); 

    printf("4) Are you married? (Y/N)\n"); 
    scanf("%s", &married[i]); 

    if (work[i] == 'Y' && married[i] == 'Y') { 
     workMarried++; 
    } 


    //Need help with children part. 
    //printf("5) how many children do you have?"); 
    //scanf("%d", &numChildren); 

    printf("6) What is the social media you use the most?\n 1. Twitter\n 2. Facebook\n 3. Google+\n 4. Linkedln\n Social Media (1-4): "); 
    scanf("%d", &socialMedia); 

    if (enteredAge <= 25) { 
     if (socialMedia == 1){ 
      twitter++; 
     } 
     else if (socialMedia == 2){ 
      facebook++; 
     } 
     else if (socialMedia == 3){ 
      google++; 
     } 
     else linkedln++; 
    } 


    printf("Do you want to answer the poll? (Y/N)\n"); 
    scanf("%s", &pollAnswer[i]); 
} while (pollAnswer[i] == 'Y'); 


percentFemale = (workMarried/female); 

printf("What percent of female students work and are married? %f\n", percentFemale); 

//Code for average age of the children. 

printf("What is the favorite social media of the students with an age less than or equal to 25 years?\n"); 

if (twitter > facebook && twitter > google && twitter > linkedln) { 
    printf("Twitter\n"); 
} 

else if (facebook > twitter && facebook > google && facebook > linkedln) { 
    printf("Facebook\n"); 
} 

else if (google > twitter && google > facebook && google > linkedln) { 
    printf("Google+\n"); 
} 

else if (linkedln > twitter && linkedln > google && linkedln > facebook) { 
    printf("Linkedln\n"); 
} 

return 0; 
} 
+0

你試過調試嗎?像印刷工作已婚和女性的價值觀?或將其加載到調試器中? –

回答

2

你數組聲明是錯誤的所述部分的代碼,你正在創建0大小的數組,例如:

char pollAnswer[0]; 

創建足夠大的數組值存儲爲char pollAnswer[SIZE];然後索引可以從0SIZE - 1。只要你想scanf只有單個字符

scanf說法是錯誤的:

scanf("%s", &Gender[i]); 

正確的它:

scanf("%c", &Gender[i]); 

你得到0.00,因爲下面的代碼從來沒有得到機會執行你不存儲字符但使用錯誤格式字符串的地址:

if (work[i] == 'Y' && married[i] == 'Y') { 
     workMarried++; 
    } 

workMarried仍然0答案是0.00。

+1

+1。希望你不介意我編輯糾正一些錯別字 – simonc

+0

@simonc感謝糾正我的錯別字。其實它幫助我,我總是檢查我是否犯了英文錯誤。謝謝! –

1

負責這些聲明

char pollAnswer[0]; 
char Gender[0]; 
char work[0]; 
char married[0]; 

如果你逃跑格式說明之前scanf()

使用空間的問題,您需要的字符只有

char pollAnswer; 
char Gender; 
char work; 
char married; 

。 例如

scanf(" %c", &work); 
+0

你建議他不要使用數組,但我從他的所有代碼中得到了OP想要使用數組的想法。 –

+0

@GrijeshChauhan請再次查看代碼,你會發現爲什麼OP使用這些變量,你可以簡單地知道OP是否需要字符或數組,以及爲什麼OP試圖聲明數組。操作數NO比較數組與字符串和否增加索引。 – Gangadhar

+1

我相信因爲他不知道使用數組。順便說一句,我不知道,正如我所說我有印象,他想使用數組。當然,你的建議也是正確的,因爲技術上他不需要使用數組。 –