这个程序有什么问题?我试图找出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