Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

c - i tried adding gets(name) to code but for some reason its not working?

#include <stdio.h>
int main()
{
    int x = 0, y = 0, z = 0, d = 0, vote, n, b_date, b_month, b_year, p_date, p_month, p_year, f_year, i;
    char name[1000];
    char add[1000];
    printf("Enter Today's date:");
    scanf("%d/%d/%d", &p_date, &p_month, &p_year);
    printf("Enter the number of voters:");
    scanf("%d", &n);
    for (i = 1; i <= n; i++) {
        printf("Enter Name of voter %d:", i);
        gets(name);
        `HERE IS THE PROBLEM FOR SOME REASON I CANT ENTER NAME WHILE CODE IS RUNNING it skips it` printf("
Enter Adress of voter %d:", i);
        gets(add);
        printf("Enter Date of Birth of voter %d:", i);
        scanf("%d/%d/%d", &b_date, &b_month, &b_year);
        f_year = p_year - b_year;
        if (f_year >= 18) {
            printf("Cast Your Vote
");
            printf("1)Vote for A
");
            printf("2)Vote for B
");
            printf("3)Vote for C
");
            printf("4)NOTA
");
            printf("Enter your choice:");
            scanf("%d", &vote);
            switch (vote) {
            case 1:
                x++;
                printf("Votes for A =%d
", x);
                printf("Votes for B=%d
", y);
                printf("Votes for C=%d
", z);
                printf("Votes for NOTA=%d
", d);
                break;
            case 2:
                y++;
                printf("Votes for A=%d
", x);
                printf("Votes for B=%d
", y);
                printf("Votes for C=%d
", z);
                printf("Votes for NOTA=%d
", d);
                break;
            case 3:
                z++;
                printf("Votes for A=%d
", x);
                printf("Votes for B=%d
", y);
                printf("Votes for C=%d
", z);
                printf("Votes for NOTA=%d
", d);
                break;
            case 4:
                d++;
                printf("Votes for A=%d
", x);
                printf("Votes for B=%d
", y);
                printf("Votes for C=%d
", z);
                printf("Votes for NOTA=%d
", d);
                break;
            default:
                printf("Invalid Choice
");
            }
        }
        else if (f_year < 18) {
            printf("Sorry!You are not eligible to vote.
");
        }
    }
    if (x > y && x > z && x > d) {
        printf("A won with %d votes", x);
    }
    else if (y > x && y > z && y > d) {
        printf("B won with %d votes", y);
    }
    else if (z > x && z > y && z > d) {
        printf("C won with %d votes", z);
    }
    else if (d > x && d > y && d > z) {
        printf("Nobody won the election!");
    }

    return 0;
}

THE CODE IS FOR A VOTTING MACHINE, when code is running i cant enter name it just completely skips it (SORRY FOR BAD ENGLISH).

question from:https://stackoverflow.com/questions/65869992/i-tried-adding-getsname-to-code-but-for-some-reason-its-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...