c - 无法在 C 中打印值

标签 c

嗨,我对 C 编程很陌生。我有下面的代码,该代码基本上通过终端从用户那里获取值并打印出这些值。所以我有一个获取函数和打印函数。不知何故,当我输入学生证后,程序停止并直接显示注册选项提示并打印出姓名和学生证。我尝试重新排列选项然后它起作用了。大家有什么想法吗?提前致谢

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

#define MAX_LEN 80

struct Student {
     char *name;               // point to a name string
     int id;                   // student number
     char *enroll;              // enrollment option: D or X
};

struct Student student1;

void getStudent(struct Student *s)
{
    printf("Type the name of the student: ");
    s->name = malloc(100);   // assume name has less than 100 letters 
    fgets(s->name, 100, stdin);

    printf("\nType the student numner: ");
    scanf("%d", &(s->id));

    printf("\nType the student enrollment option (D or X): ");
    scanf("%c", (s->enroll)); // some bug here
    return; 
}

void printStudent(struct Student s)
{
    char name[MAX_LEN];
    char enroll[MAX_LEN];
    int id;

    s.id = id;
    s.name = name;
    s.enroll = enroll;

    printf("Student Details: %d %s %s \n", s.id, s.name, s.enroll );
    return;
}

int main(int argc, char *argv[]){
    getStudent(&student1);
    printStudent(student1);
    return 0;
} 

最佳答案

请参阅代码中的更正。 http://ideone.com/BQjS1A

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

#define MAX_LEN 80

// ****  Add a constant for the size of name
#define MAX_NAME 100

struct Student {
     char name[MAX_NAME];    // ** Save the hassle using malloc
     int id;                   // student number
     char enroll;    // *** Just need a character - not a character pointer
};

struct Student student1;

void getStudent(struct Student *s)
{
    printf("Type the name of the student: ");
// *** Do not need malloc as using an array
//    s->name = malloc(100);   // assume name has less than 100 letters 
    fgets(s->name, MAX_NAME, stdin);

    printf("\nType the student number: ");  // ** Corrected a typo
    scanf("%d", &(s->id));  // *** You should check the return value here and take appropriate action - I leave that you the reader

    printf("\nType the student enrollment option (D or X): ");
   // *** Added a space in front of %c to read white space
    scanf(" %c", &(s->enroll)); //  scanf requires a charcter pointer here
//    return;   This is not needed
}

void printStudent(struct Student s)
{
// *** THIS CODE DOES NOT MAKE ANY SENSE
//    char name[MAX_LEN];
//    char enroll[MAX_LEN];
//    int id;

//    s.id = id;
//    s.name = name;
//    s.enroll = enroll;
// *** Need %c to print enroll
    printf("Student Details: %d %s %c \n", s.id, s.name, s.enroll );
 ///   return;  Not needed
}

int main(int argc, char *argv[]){
    getStudent(&student1);
    printStudent(student1);
    return 0;
} 

关于c - 无法在 C 中打印值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42735942/

相关文章:

c - *a = b 和 = b 的区别?

c - 过滤 "Smoothing"C 中的数字数组

c - 将二叉树保存到 C 中的文件中

c - Bash 扩展在 popen 中不起作用

C for 循环与 rolled out 循环有不同的效果

c - 结构体大小与类型大小

C:将字符串传递给函数时错误预期为 ')'

c - 用指针返回一个值(C 程序)

c - 双重自由或腐败的原因

c - 获取错误 : memory protection violation