這個程序有什麼問題?我試圖找出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);
}
這裏有一些屏幕截圖:
我不知道,並沒有關於什麼錯誤的想法。請幫我.. 在此先感謝..
簡單的解決辦法:'字符啞; scanf(「%d%c」,&choice,&dummy);'您的'scanf'正在讀取整數值,並將''\ n''(輸入鍵盤按鈕的ASCII值)汽車保存到'stdin'緩衝區中。所以你必須在之前進行假脫機讀取新的東西。 – LPs
它修復了第一個問題。當我輸入saugat的選擇它顯示輸出顯示你的名字是saugat ..我該如何解決它? – user3213732
我不明白你的意思。你能詳細說明一下嗎?你的代碼應該輸出'printf(「你的名字是:%s」,temp.name);'輸入名字後。 – LPs