Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7016349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:46:23+00:00 2026-05-27T22:46:23+00:00

I just started to play C and I bump into this problem. Here’s my

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:46:24+00:00Added an answer on May 27, 2026 at 10:46 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the code, (don't kill me, i've just started to study javascript)...Firefox'errors console
I've just started playing with Play! framework, and stumbled on such problem: modules that
I've just started with the Play Framework and I'm running into some problems using
I just started to play around with Linq to entities and ran into an
I've just started to play with CodeIgniter 1.7.2 and imendialtly noticed that there is
Just started working with .NET and MVC(1). I'm having a problem wherein in my
I just started to play with android dev and java+eclipse is pretty new to
I just started to play around with Eclipse M2T JET2 . Now I noticed
I´ve just started to test, use and play with facebook apps. I´ve searched stackoverflow
I've just started having a proper play with Powermock and noticed that it slows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.