편집 기록

편집 기록
  • 프로필 nowp님의 편집
    날짜2021.09.09

    C언어 온라인 오류 해결 부탁드립니다.


    #include <stdio.h>
    #include <string.h>
    
    void main()
    {
        struct Person {
            char name[50]; // 이름
            int age;
            char sex;
            char* namep; // 성을 가리키는 포인터
        } student;
    
        while (1) {
            printf("Provide your personal information:\n");
            printf("Name : ");
            //name의 입력값이 stop일 경우 종료하기
            scanf("%[^\n]", student.name);
            if (strcmp(student.name, "stop") == 0) break;
            rewind(stdin);
            //나이 입력
            printf("Age : ");
            scanf("%d", &student.age);
            rewind(stdin);
            //성별 입력
            printf("Sex(M/F) :");
            scanf("%c", &student.sex);
            rewind(stdin);
    
            student.namep = strtok(student.name, " ");
            student.namep = strtok(NULL, " ");
    
    
            printf("Your name is %s %s, your age is %d, your sex is %c\n\n", student.namep, student.name, student.age, student.sex);
    
        }
    }
    

    어떤 사람 이름 성 나이 성별 순으로 입력 받아서 성 이름 나이 성별로 출력하는 간단한 프로젝트인데 https://www.onlinegdb.com/online_c_compiler 에서 컴파일 했을 때

    1. 왜 rewind를 했음에도 불구하고 성별 입력이 무시 되는지 이해가 안 갑니다.
    2. 성별 입력 부분에 %c로 입력한 경우 첫번 째에는 제대로 작동하는데 다음 입력부터 name: age: 이런식으로 오류가 발생하는데 그 이유는 뭔가요?
  • 프로필 알 수 없는 사용자님의 편집
    날짜2021.09.08

    C언어 온라인 오류 해결 부탁드립니다.


    include

    include

    void main() { struct Person { char name[50]; // 이름 int age; char sex; char* namep; // 성을 가리키는 포인터 } student;

    while (1) {
        printf("Provide your personal information:\n");
        printf("Name : ");
        //name의 입력값이 stop일 경우 종료하기
        scanf("%[^\n]", student.name);
        if (strcmp(student.name, "stop") == 0) break;
        rewind(stdin);
        //나이 입력
        printf("Age : ");
        scanf("%d", &student.age);
        rewind(stdin);
        //성별 입력
        printf("Sex(M/F) :");
        scanf("%c", &student.sex);
        rewind(stdin);
    
        student.namep = strtok(student.name, " ");
        student.namep = strtok(NULL, " ");
    
    
        printf("Your name is %s %s, your age is %d, your sex is %c\n\n", student.namep, student.name, student.age, student.sex);
    
    }
    

    }

    어떤 사람 이름 성 나이 성별 순으로 입력 받아서 성 이름 나이 성별로 출력하는 간단한 프로젝트인데 https://www.onlinegdb.com/online_c_compiler에서 컴파일 했을 때

    1. 왜 rewind를 했음에도 불구하고 성별 입력이 무시 되는지 이해가 안 갑니다
    2. 성별 입력 부분에 % c 로 입력한 경우 첫번 째에는 제대로 작동하는데 다음 입력부터 name: age: 이런식으로 오류가 발생하는데 그 이유는 뭔가요??