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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:36:03+00:00 2026-05-13T20:36:03+00:00

I have a program where user input is required, a user types in a

  • 0

I have a program where user input is required, a user types in a number 1-8 to determine how to sort some data, but if the user just hits enter a different function is performed. I get the general idea of how to do this and I thought what I had would work just fine but I’m having some issues when it comes to when the user just hits the enter key. Currently my code looks as follows:

//User input needed for sorting.    
fputs("Enter an option (1-8 or Return): ", stdout);
fflush(stdout);
fgets(input, sizeof input, stdin);

printf("%s entered\n", input);  //DEBUGGING PURPOSES

//If no option was entered:
if(input == "\n")
{
    printf("Performing alternate function.");
}
//An option was entered.
else
{
    //Convert input string to an integer value to compare in switch statment.
    sscanf(input, "%d", &option);

    //Determine how data will be sorted based on option entered.
    switch(option)
    {
        case 1:
        printf("Option 1.\n");
        break;

        case 2:
        printf("Option 2.\n");
        break;

        case 3:
        printf("Option 3.\n");
        break;

        case 4:
        printf("Option 4.\n");
        break;

        case 5:
        printf("Option 5.\n");
        break;

        case 6:
        printf("Option 6.\n");
        break;

        case 7:
        printf("Option 7.\n");
        break;

        case 8:
        printf("Option 8.\n");
        break;

        default:
        printf("Error! Invalid option selected!\n");
        break;
    }   
}

Now I’ve changed the if statement to try input == “”, input == ” “, and input == “\n” but none of these seems to work. Any advice would be greatly appreciated. Currently from what I can see, the initial if statement fails and the code jumps to the else portion and then prints the default case.

Just to be clear the variables I declared for this code are as follows:

char input[2];          //Used to read user input.
int option = 0;         //Convert user input to an integer (Used in switch statement).  
  • 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-13T20:36:04+00:00Added an answer on May 13, 2026 at 8:36 pm

    The problem is in how you’re doing the string comparison (if (input == "\n")). C doesn’t have a “native” string type, so to compare strings, you need to use strcmp() instead of ==. Alternatively, you could just compare to the first character of the input: if (input[0] == '\n') .... Since you’re then comparing char’s instead of strings, the comparison doesn’t require a function.

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

Sidebar

Ask A Question

Stats

  • Questions 388k
  • Answers 388k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer These are variation of the in-ribbon gallery, you are currently… May 15, 2026 at 12:29 am
  • Editorial Team
    Editorial Team added an answer Ok I found it. In my code behind i used… May 15, 2026 at 12:29 am
  • Editorial Team
    Editorial Team added an answer Add a new instance method to your Article model. class… May 15, 2026 at 12:29 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.