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

  • SEARCH
  • Home
  • 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 8877077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:22:43+00:00 2026-06-14T19:22:43+00:00

I have the following program written in C: The main problem with this program

  • 0

I have the following program written in C:

The main problem with this program is that if the input exceeds 80 characters when using fgets() function, the program just exits immediately. The other code is executed, however it does not wait for the user to press enter. It like simply ignores the getchar at the end.

How can I solve this problem please?

  • 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-14T19:22:44+00:00Added an answer on June 14, 2026 at 7:22 pm

    If the user input is longer than the 79 characters that fgets may read from stdin (it can read at most one less than its size parameter says, since it 0-terminates the buffer), the remaining input is left in the input buffer, hence the getchar() at the end immediately succeeds.

    To avoid that, you need to clear the input buffer if the input was too long.

    The problem is that if the input was short enough, you don’t know whether to clear the buffer or not. So check whether you actually got a newline read in by fgets,

    int len = strlen(password);
    if (password[len-1] == '\n') {
        // got a newline, all input read, overwrite newline
        password[len-1] = 0;
    } else {
        // no newline, input too long, clear buffer
        int ch;
        while ((ch = getchar()) != EOF && ch != '\n');
        if (ch == EOF) {
            // input error, stdin closed or corrupted, what now?
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this program that has a main function, inside which, I am
i have written the following code class Program { static void Main(string[] args) {
I have written the following C program. The output is 32. Why is this?
I'm just learning java and following a book. I have a program written via
I have written down the following program that uses the quicksort algorithm to sort
I have written the following program using Parallel Haskell to find the divisors of
I have the following program written in C: #include stdafx.h #include <Windows.h> void main()
I have following simple program: import std.stdio; int main(string[] argv) { writeln(Hello, world!); return
Hi I have the following program. When I compile on the terminal gcc main.c
I have the following console application written in VB.NET: Sub Main() Dim ie As

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.