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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:25:54+00:00 2026-06-15T02:25:54+00:00

While I could use strings, I would like to understand why this small example

  • 0

While I could use strings, I would like to understand why this small example I’m working on behaves in this way, and how can I fix it ?

int ReadInput() {
    char buffer [5];
    printf("Number: ");
    fgets(buffer,5,stdin);
    return atoi(buffer);
}

void RunClient() {
    int number;
    int i = 5;
    while (i != 0) {
        number = ReadInput();
        printf("Number is: %d\n",number);
        i--;
    }
}

This should, in theory or at least in my head, let me read 5 numbers from input (albeit overwriting them).

However this is not the case, it reads 0, no matter what.

I understand printf puts a \0 null terminator … but I still think I should be able to either read the first number, not just have it by default 0. And I don’t understand why the rest of the numbers are OK (not all 0).

CLARIFICATION: I can only read 4/5 numbers, first is always 0.

EDIT:

I’ve tested and it seems that this was causing the problem:

main.cpp

scanf("%s",&cmd);
    if (strcmp(cmd, "client") == 0 || strcmp(cmd, "Client") == 0)
        RunClient();

somehow.

EDIT:

Here is the code if someone wishes to compile. I still don’t know how to fix

http://pastebin.com/8t8j63vj

FINAL EDIT:

Could not get rid of the error. Decided to simply add @ReadInput

int ReadInput(BOOL check) {
    ...
    if (check) 
       printf ("Number: ");
    ...

@ RunClient()

void RunClient() {
    ...
    ReadInput(FALSE); // a pseudo - buffer flush. Not really but I ignore 
    while (...) {                    // line with garbage data
        number = ReadInput(TRUE);
        ...
    }

And call it a day.

  • 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-15T02:25:55+00:00Added an answer on June 15, 2026 at 2:25 am

    fgets reads the input as well as the newline character. So when you input a number, it’s like: 123\n.

    atoi doesn’t report errors when the conversion fails.

    Remove the newline character from the buffer:

    buf[5];
    size_t length = strlen(buffer);
    buffer[length - 1]=0;
    

    Then use strtol to convert the string into number which provides better error detection when the conversion fails.

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

Sidebar

Related Questions

a while ago i could comment any code in php with netbeans like this:
I like the whole WMI concept, and I could really make use of it
Can I upload content(any type png/jpg) onto android market, which I could download while
I'm trying to understand how to use Type Converters after reading this answer to
I don't quite understand the example given from the man find , can anyone
I have some strings like this: i: 11;a:5: {s:2:id;s:4:1097;s:5:iName;s:12:Ovo de Andre;s:10:dropChance;s:4:2000;s:4:type; i:1;s:5: kName; s:12:
I would like to use the .NET FileSystemWatcher class to monitor a folder path
I have pattern/matcher lines that transform input Strings like this: 1 3 Hi [2
I have a JTextArea where lines are highlighted upon right-click. While I could achieve
After a while I figured out I could bind Shark to the process of

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.