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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:32:20+00:00 2026-06-17T10:32:20+00:00

As my title says I’m a new programmer learning C. This is my second

  • 0

As my title says I’m a new programmer learning C. This is my second day programming and I need help fixing this problem in my code. I’m making a mad lib and while no errors come up the first scanf takes 2 lines rather than the 1 every other scanf uses.

Here’s the code:

#include <stdio.h>

int main()
{
    char verb[20];
    char loc[20];
    char noun1[20];
    char noun2[20];
    char adj[20];

/* The following is the part where you input words. It sets them as strings (named word1-5, as stated above)*/

    printf("Welcome to Mad Libs! \nAnswers can only be one word long. \nPlease enter a verb.\n");
    scanf("%s\n",verb);

    printf("Now enter a location!\n");
    scanf("%s\n",loc);

    printf("Now enter a noun!\n");
    scanf("%s\n",noun1);

    printf("Now enter another noun!\n");
    scanf("%s\n",noun2);

    printf("Now please enter an adjective!\n");
    scanf("%s\n",adj);

/* It all comes together here. The code is supposed to take the strings set previously and put them into this story. */
/* The dashes and various /n's are there to make the final Mad Lib easier to read */    

    printf("\n\nHere is your Mad Lib:\n------------------------------ \nHolly %s down to the %s.\nOnce she got there she bought some %s and ate it!\nAfterwards, Holly brought it home and let her %s eat it.\nHolly is a %s girl!\n------------------------------ \n",verb,loc,noun1,noun2,adj);
return 0;
}

It was made using a combination of Vi and Sublime Text 2 on Ubuntu.

Like I said, I’m not getting any errors when compiling and everything seems to be in order, the problem is that once I run it in the Terminal I have to enter the first answer (answer to “Welcome to Mad Libs! Answers can only be one word long. Please enter a verb.”) twice and it takes both of them.

Please try running it yourself (it should work in both OS X and Linux as a .c file) if you’re confused as to what I mean, I honestly don’t know how to describe the error very well. It makes me input the first answer twice and causes problems when showing the final mad lib.

  • 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-17T10:32:21+00:00Added an answer on June 17, 2026 at 10:32 am

    Just use scanf("%s", ...), not scanf("%s\n"). The \n doesn’t get there until later. (Oh, and this is also a good way to get buffer overflows, by the way, so you might consider using fgets, et. al.)

    The way it’s working right now is:

    1. You type a line and press Enter. scanf gets the line, but without the \n, so it’s still waiting.
    2. You type the next line and press Enter. scanf now has the \n at the end of the previous line and uses that string. The first line was read, the second line is now in the buffer, and scanf is awaiting another \n that already exists.
    3. GOTO 2

    And that has the effect of shifting all of the answers by one.

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

Sidebar

Related Questions

Just as the title says, I need help with javascript code that will open
Title says all. Sample code: ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); HashMap<String, Object>
As title says, mock code to demostrate my problem Driver Class import java.io.*; public
As the title says, will this code work and will it work in major
As title says I need help with onchange. I have select tag,and I need
As title says, i need to get this information regarding the screen state. I
As title says, i know that new throws an exception which can be caught,
As title says for some purpose I need to get the size of request/response
Basicaly title says it all, mine file looks like this: edit Product,Home > Opto-electronics
As title says, I'm developing a 'subscription' feature for a SharePoint site and need

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.