3

I just started to play C and I bump into this problem. Here's my code:

#include<stdio.h>
#include<conio.h>

struct person {
    int i;
    char name[100];
};

int main() {
    struct person p[2];

    clrscr();
    for(int i=0;i<2;i++) {
        printf("Enter i:\n");
        scanf("%d",&p[i].i);

        printf("Enter name:\n");
        gets(p[i].name);
    }
    for(int j=0;j<2;j++) {
        printf("ID: %d, Name: %c\n", p[j].i,p[j].name);
    }
    getch();
    return 0;
}

Here's a sample ouput:

enter image description here

The problem is, all char members are not being asked for a value.

UPDATE:

btw, I am using Turbo C++ version 3 compiler.

Loreto Gabawa Jr.
  • 1,996
  • 5
  • 21
  • 33

5 Answers5

4

You should print a string with %s; %c will interpret the pointer as a char. (Strictly, I believe the result is undefined behavior.)

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
3

Two things:

  1. You need to clear the input buffer to keep it from eating the newline.
  2. Secondly, you need to change the format string to %s.

Here's the corrected code:

int main() {
    struct person p[2];

    for(int i=0;i<2;i++) {
        printf("Enter i:\n");

        scanf("%d",&p[i].i);

        //  Flush input buffer
        int ch;
        while ((ch = getchar()) != '\n' && ch != EOF);


        printf("Enter name:\n");
        gets(p[i].name);
    }
    for(int j=0;j<2;j++) {
        printf("ID: %d, Name: %s\n", p[j].i,p[j].name);
    }
    getch();
    return 0;
}

%c expects a char, but you're trying to pass in a string. It's undefined behavior to have mismatching types.

Output:

Enter i:
1
Enter name:
asdf
Enter i:
2
Enter name:
zxcv
ID: 1, Name: asdf
ID: 2, Name: zxcv
Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • 1
    `fflush(stdin)` is undefined behaviour. `fflush` is defined only for output stream. [:)](http://stackoverflow.com/questions/2979209/using-fflushstdin) – another.anon.coward Jan 04 '12 at 17:43
  • Hi Mystical, for some strange reason when I accept your answer, a message pop-up and told me your answer was deleted. so what I did, I updated my question with your answer in it. thanks – Loreto Gabawa Jr. Jan 04 '12 at 17:54
  • @another.anon.coward pointed out the mistake I made. So I deleted to give me time to fix my answer. Although `fflush(stdin)` seems to work in MSVC, apparently it's undefined behavior by the standard. – Mysticial Jan 04 '12 at 17:58
  • I see. I really appreciated your help. – Loreto Gabawa Jr. Jan 04 '12 at 18:09
1

You probably need to clear the input buffer after typing in a number (you press return after the number, but you don't read that return)

You can read the input stream until you read a \n after calling scanf:

while( ch = getchar() != '\n' && ch != EOF);
Fox32
  • 13,126
  • 9
  • 50
  • 71
0

Use sscanf as scanf is deprecated. You can also use sscanf for reading in strings as well, not just numbers. Also, %c is for printing characters.

#include<stdio.h>
#include<conio.h>

struct person {
    int i;
    char name[100];
};

int main() {
    struct person p[2];

    clrscr();
    for(int i=0;i<2;i++) {
        printf("Enter i:\n");
        sscanf("%d", &p[i].i);

        printf("Enter name:\n");
        sscanf("%s", p[i].name);
    }
    for(int j=0;j<2;j++) {
        printf("ID: %d, Name: %s\n", p[j].i,p[j].name);
    }
    getch();
    return 0;
}
Brett McLain
  • 2,000
  • 2
  • 14
  • 32
0

You should either use %s instead. Expression p[j].name is a pointer to an array of chars, so you can't print it with %c.

Phonon
  • 12,549
  • 13
  • 64
  • 114