2016-09-28 99 views
1

這個程序有什麼問題?我試圖找出2天以來,但沒有任何幫助! 字符串輸出只在字符串輸入後,選擇選擇後,默認字符串輸入是默認情況下,我猜的新行字符。 此外,如果我輸入字符串,而輸入選擇,它顯示默認名稱輸出。這裏是我的代碼:無法在gcc中正確輸入字符串ubuntu linux

#include<stdio.h> 
#include<string.h> 
#include<stdlib.h> 

struct marks { 
    char subject[15]; 
    int mark; 
}; 
struct student { 
    char name[10]; 
    int roll; 
    struct marks m[3]; 
}; 
void displayData(struct student); 
int displayChoice(); 
struct student getNewRecord(); 
int main() { 
    struct student s[5]; 
    int count = 0; 
    int choice; 
    do{ 
     choice = displayChoice(); 
     if(choice == 1){ 
      s[count] = getNewRecord(); 
      count ++; 
     } 
     else if(choice == 4) 
      break; 
     else 
      printf("Invalid choice"); 
    }while(1); 
} 
struct student getNewRecord() { 
    struct student temp; 
    printf("Enter your Name : "); 
    fgets(temp.name, 10,stdin ); 
    printf("Your name is : %s",temp.name); 
    return temp; 

} 
int displayChoice() { 
    int choice; 
    printf("\n\nPlease select your choice :\n"); 
    printf("1. Add new Record\n"); 
    printf("2. Display All data \n"); 
    printf("3. Remove last Record\n"); 
    printf("4. Exit the program\n"); 
    printf("What is your choice : \n"); 
    scanf("%d", &choice); 
    return choice; 
} 
void displayData(struct student s){ 
    printf("Your name : %s", s.name); 
} 

這裏有一些屏幕截圖: enter image description here

我不知道,並沒有關於什麼錯誤的想法。請幫我.. 在此先感謝..

+0

簡單的解決辦法:'字符啞; scanf(「%d%c」,&choice,&dummy);'您的'scanf'正在讀取整數值,並將''\ n''(輸入鍵盤按鈕的ASCII值)汽車保存到'stdin'緩衝區中。所以你必須在之前進行假脫機讀取新的東西。 – LPs

+0

它修復了第一個問題。當我輸入saugat的選擇它顯示輸出顯示你的名字是saugat ..我該如何解決它? – user3213732

+0

我不明白你的意思。你能詳細說明一下嗎?你的代碼應該輸出'printf(「你的名字是:%s」,temp.name);'輸入名字後。 – LPs

回答

0

你有一些問題到你的代碼:

  1. scanf函數離開'\n'焦炭引入stdin,並從標準輸入讀取的功能在下次調用退出立即到期那個角色。
  2. 您應該檢查返回值scanf以確保用戶輸入了有效的號碼。
  3. 不要使用帶有空參數的舊式函數聲明。如果不需要參數,請使用(void)
  4. 您無盡的主迴路應該照顧s陣列大小。

您可以添加一個空功能stdin像:

#include<stdio.h> 
#include<string.h> 
#include<stdlib.h> 

struct marks { 
    char subject[15]; 
    int mark; 
}; 

struct student { 
    char name[10]; 
    int roll; 
    struct marks m[3]; 
}; 

void displayData(struct student); 
int displayChoice(void); 
struct student getNewRecord(); 
void flush_stdin(void); 

int main(void) 
{ 
    struct student s[5]; 
    size_t count = 0; 
    int choice; 

    do{ 
     choice = displayChoice(); 

     if(choice == 1) 
     { 
      s[count] = getNewRecord(); 
      count ++; 
     } 
     else if(choice == 4) 
      break; 
     else 
      printf("Invalid choice"); 
    } 
    while ((count < sizeof(s)/sizeof(s[0])) && (choice != 4)); 
} 

struct student getNewRecord(void) 
{ 
    struct student temp; 

    printf("Enter your Name : "); 

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

    printf("Your name is : %s", temp.name); 

    flush_stdin(); 

    return temp; 
} 

int displayChoice(void) { 
    int choice; 

    printf("\n\nPlease select your choice :\n"); 
    printf("1. Add new Record\n"); 
    printf("2. Display All data \n"); 
    printf("3. Remove last Record\n"); 
    printf("4. Exit the program\n"); 
    printf("What is your choice : \n"); 

    if (scanf("%d", &choice) != 1) 
    { 
     choice = 99; 
    } 

    flush_stdin(); 

    return choice; 
} 

void displayData(struct student s) 
{ 
    printf("Your name : %s", s.name); 
} 

void flush_stdin(void) 
{ 
    int c; 
    while (((c = getchar()) != '\n') && (c != EOF)); 
} 
+1

對不起,我在名字上觸發,沒有讀取代碼。我的5ct:我更喜歡e,g,「drop」或類似的(「emtpy」也很好)。 – Olaf

+0

在什麼基礎上,祈禱告訴你,你是誰,不管你是誰,都低估了這個代碼?除了'main'被遺忘的回報之外,我們會很快進入挑剔的領域,而尼特並不是一個downvote的理由,或者它們是? – deamentiaemundi

+0

@deamentiaemundi我不在乎。仇恨會討厭;)。 – LPs