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 8544401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:32:56+00:00 2026-06-11T12:32:56+00:00

I am reading from stdin students in a structure array. After the details are

  • 0

I am reading from stdin students in a structure array. After the details are introduced for one student, i ask for another student details. if the choice is Y, i’ll add the new student, if the choice is N, break. But what if the choice is simply ENTER? How can i detect the new line character? I tried with getchar(), but it skips the first reading from stdin.When i debug it doesn’t stops to the first line test=getchar(), it stops to the second one.

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

struct student
{
char name[20];
int age;
};

int main()
{
struct student NewStud[5];
char test;
int count=0;
for(count=0;count<5;count++)
{
    printf("Enter the details for %s student: ",count>0?"another":"a");
    printf("\nName : ");
    scanf("%s",NewStud[count].name);
    printf("\nAge : ");
    scanf("%d",&NewStud[count].age);
    printf("Would you like to continue? (Y/N)");
    test=getchar();
    if(test=='\n')
    {
        printf("Invalid input. Would you like to continue? (Y/N)");
        test=getchar();
    }
    while(tolower(test) !='n' && tolower(test) != 'y')
    {
        printf("Invalid input.Would you like to continue? (Y/N)");
        test=getchar();
    }
    if(tolower(test) == 'n')
    {
        break;
    }
    if(tolower(test) == 'y')
    {
        continue;
    }
}


getch();
}
  • 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-06-11T12:32:57+00:00Added an answer on June 11, 2026 at 12:32 pm

    The problem is that scanf() leaves a newline character in the input stream, you have to consume it before you’ll get “valid” data in getchar().

    Ex:

    scanf("\n%s",NewStud[count].name);
    getchar();
    printf("\nAge : ");     
    scanf("%d",&NewStud[count].age);
    getchar();
    printf("Would you like to continue? (Y/N)");
    test=getchar();   // Now this will work
    

    Check out this link for more info. It’s for fgets, but it’s the same problem with getchar()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does stdin have any EOF? For example, if I start reading from stdin using
I wanted to compare reading lines of string input from stdin using Python and
Possible Duplicate: Does reading from stdin flush stdout? C++ Standard guarantees that all data
Im reading in values from stdin and I want to keep reading the file
I'm using the readline from gcc -lreadline for reading from stdin. Later, I want
Given input, which shows tag assignments to images, as follows (reading this from php://stdin
How would I go about reading a string from stdin and formatting as such
I'm having problems redirecting stdio of another program using subprocess module. Just reading from
I have a C program who is reading from stdin. And i will test
I have a java program in which I am reading from stdin BufferedInputStream bis

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.