2011-07-13 35 views
0

掃描字符串下面是結構的c代碼。在這個程序中,同時掃描名稱程序的價值正在從這一點終止。並且它還需要2個值作爲名稱數組。它沒有給出任何運行時錯誤或警告。 u能告訴我什麼是這個節目對我的電子郵件ID ....使用結構

#include<stdio.h> 
#include<conio.h> 
struct student 
{ 
    int id; 
    char name[20]; 
    float per; 
} st; 
main() 
{ 
    clrscr(); 
    printf("\nenter the info of student"); 
    printf("\n=======================\n"); 
    printf("id:"); 
    scanf("%d:",&st.id); 
    printf("name :"); 
    scanf("%s :",st.name); 
    printf("per :"); 
    scanf("%f :",&st.per); 

    printf("\n id is: %d \n",st.id) ; 
    printf("\n name is: %s \n",st.name) ; 
    printf("\n per is: %1f \n",st.per) ; 
    getch(); 
    return 0; 
} 
+2

這是一個質量保證網站; 「請快速回答」和電子郵件ID無用 – sehe

+0

請提供您嘗試輸入的姓名的長度。它是超過19個字符?請考慮重寫您的問題,使其更具可讀性。 –

回答

0

妥善解決怎麼樣

scanf("%s",st.name);

當輸入名稱不要使用空格。 得到名稱用空格用fgets(..)

+1

&符號不是必需的。 –

+0

是的,你是對的。 10倍 –

1
scanf("%d:",&st.id); scanf("%s :",st.name); scanf("%f :",&st.per); 

任何原因包括格式字符串的:whitespaces

這裏閱讀:http://www.cplusplus.com/reference/clibrary/cstdio/scanf/

再試一次所有的人,但沒有:。例如。 scanf("%d",&st.id);

請記住,scanf("%s",st.name);最終可能會覆蓋它不應該覆蓋的內存。

1

您應該檢查來自scanf()調用的返回狀態,以便您可以確定哪個調用失敗。

  • 您是否記得將冒號添加到格式字符串要求的輸入數據中?

記住,任何不是白色的空間或scanf()格式的轉換符,預計在輸入和你正在尋求號碼和姓名後冒號。

另外,如果您打算輸入姓名,%s說明符不合適;它停在第一個空間。