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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:18:50+00:00 2026-06-05T09:18:50+00:00

#define MAX_COMMAND_LEN 32 char command[MAX_COMMAND_LEN]; while (1) { if (fgets(command, MAX_COMMAND_LEN, stdin) == NULL)

  • 0
#define MAX_COMMAND_LEN 32

char command[MAX_COMMAND_LEN];
while (1) {
    if (fgets(command, MAX_COMMAND_LEN, stdin) == NULL) {
        perror("Error: standard function fgets has failed\n");
        break;
    }

    if (command[strlen(command) -1] != '\n') {
        printf("Error: command length must be less than or equal to 30 characters\n");
        continue;
    }
    else {
         printf("Error: command not found\n");
    }

}
quit();

I have couple of problems which I’m not able to handle:

  1. When I press Enter, it stops the loop and doesn’t print the command not found message.
  2. When I enter a command with a size bigger than 30 characters it prints both the command not found and the command length must be less than or equal to 30 characters messages.
  3. When I enter a 64 size command it prints twice the 30-length message.

I believe it divides the input to 30-length segments and input each one of them, how do I overcome it? I tried to flush stdin, it does not work. I want to get rid of the rest of the input. How do I overcome all these problems?

  • 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-05T09:18:52+00:00Added an answer on June 5, 2026 at 9:18 am

    For your second problem, it’s because fgets fetches the 31 (MAX_COMMAND_LEN, minus space for the terminating '\0' character) first characters, you notice it’s no newline and the next time around the loop fgets fetches the remaining characters.

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

Sidebar

Related Questions

#define HISTORY_SIZE 50 #define INPUT_SIZE 512 /*Max input size*/ char input[INPUT_SIZE]; /*Holding user input
Here is my code; <?php define (MAX_SIZE,500); function getExtension($str) { $i = strrpos($str,.); if
Done <?php define('FILE_NAME', 'list.dat'); define('MAX_BREAK', 30); function write($file, $ip, $time) { fwrite($file, $ip .
#include <stdio.h> #define MAXLENGTH 77 int getline(char[], int); main() { char in_line[MAXLENGTH]; int count,
So I've got this. #define MAX_MENU_OPTIONS 1 typedef struct _NEW_MENU_OPTION { char* name; int
I am doing the following in C #define MAX_DATA_SIZE 500; struct reliable_state { char
#define MAX 100 struct bs{ int ab; int ac; }be; struct s{ be b;
what I need is to match multiline preprocessor's statements such as: #define max(a,b) \
#include <iostream> #include <cmath> #define max(x,y) (x)>(y)? (x): (y) int main() { int i
#include <stdio.h> #include <stdlib.h> #define MAX 20 #define MAX_BASE 8 #define ROW 9 #define

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.