2016-09-28 98 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