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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:30:58+00:00 2026-05-30T13:30:58+00:00

In my C program, I call fgets() twice to get input from the user.

  • 0

In my C program, I call fgets() twice to get input from the user. However, on the second call of fgets() (which is in a function), it doesn’t wait for the input to be taken, it just skips over it as if it didn’t even ask for it. Here is my code (shortened down a bit):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ARE_EQUAL 0

void rm_nl(char *c, int s);
float ctof();
float ftoc();

int main()
{   
    char str[2];                                     // Setting vars
    float result;

    printf("Type 'C' or 'F': ");                     // Prompt

    fgets(str, 2, stdin);                            // <-- First fgets
    rm_nl(str, 2);                                   // rm_nl() removes the newline 
                                                     // from input
    printf("\n");

    if(strcmp(str, "C") == ARE_EQUAL || strcmp(str, "c") == ARE_EQUAL)
    {
        result = ctof();                         // Compares strings and calls
        printf("%.2f\n", result);                // function conditionally
    }
    else
    {
        result = ftoc();
        printf("%.2f\n", result);
    }

    return 0;
}

float ctof()                                          // One of the two functions
{                                                     // (they are almost the same) 
    char input[64];
    float fahr, cels;                                 // Local vars

    printf("Type in a Celsius value: ");             // Prompt

    fgets(input, 64, stdin);                               // <-- Second fgets
    rm_nl(input, sizeof(input));

        // Yadda yadda yadda
}


// Second function and rm_nl() not shown for readability

This program would output something like:

Type 'C' or 'F': (value)

and then…

Type a Celsius value: 57.40 (I don't type this)

(Program terminates)

It fills in the 57.40 without me even typing it! What should I do differently?

  • 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-30T13:31:00+00:00Added an answer on May 30, 2026 at 1:31 pm
    fgets(str, 2, stdin);
    

    You’re providing too little space for fgets. You only allow it to read one character (since 2 includes the 0-terminator).

    The newline will always be left in the input buffer so the next stdio operation will read it.

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

Sidebar

Related Questions

I have a python program which takes input from stdin. Now, I've to write
I am trying to get the program to call up the current date, add
My objective is to write a program which will call another executable on a
From my bash shell I would like to call a program n times with
What is the simplest way to call a program from with a piece of
If I call finalize() on an object from my program code, will the JVM
How might you call a Lisp program from a Rails application?... For example, allow
I have a program which does a system call: latex somefile.latex This runs ok,
How do I call shell commands from inside of a Ruby program? How do
I have a program that call a function with undefined arguments, like this: #include

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.