#include<iostream>
#include<stdlib.h>
using namespace std;
int readInt()
{
    int cc=getc(stdin);
    for(;cc<'0'||cc>'9';)
    cc=getc(stdin);
    int ret=0;
    for(;cc>='0'&&cc<='9';)
{
    ret=ret*10+cc-'0';
    cc=getc(stdin);
}
return ret;
}
int main(void)
{
    int p=readInt();
    cout<<p<<endl;
    return 0;
}    

Dani AI

Generated

This function's intent is simple: scan standard input character by character until it finds digits, then accumulate those digits into an integer and return it. That explains 's comment about "fast input." 's blunt warning is useful: the implementation has real edge cases that make it unsafe in general use. points toward more idiomatic C++ input, which is the right direction for clarity and portability.

Main pitfalls to watch for:

  • no EOF handling: if end-of-file is reached before any digit appears, the skip loop never terminates. Always detect EOF and return an error or status instead of looping forever.
  • no sign support: negative numbers (and a leading '+') are ignored, so inputs like "-42" are parsed incorrectly.
  • no overflow checks: reading arbitrarily many digits will silently overflow the target int. Detect overflow while accumulating (compare against (INT_MAX - digit)/10) or use a larger type and validate.
  • portability/headers: mixing C I/O in C++ code requires correct headers (<cstdio>), and some fast tricks (e.g., getchar_unlocked) are nonportable.

Practical, safe checklist to improve it:

  1. read the character into an int and check for EOF before any character-range comparisons; return a status if EOF.
  2. handle an optional +/- sign and remember the sign when returning.
  3. while accumulating digits, check for overflow and abort or clamp if needed.
  4. prefer idiomatic C++ (std::cin), with ios::sync_with_stdio(false); cin.tie(nullptr); for most competitive cases; for extreme throughput implement a buffered reader using fread and pointer parsing (take care with EOF and sign handling).

Testing: try empty input, input that ends immediately, very long digit sequences, negative numbers, and non-digit separators. These cases expose the weaknesses that and rightly flagged.

Recommended Answers

All 3 Replies

Don't use that code. That code is terrible. What are you trying to do? Oh yeah did I mention DON'T USE THAT CODE.

This is the way to take input fast..

Just so you know, the proper C++ version of that (mostly C-style) code is this:

#include<iostream>
using namespace std;

int main(void)
{
  int p = 0;
  cin >> p;
  cout << p << endl;
  return 0;
}

That readInt function just reads an integer for the standard input stream, and it works like this:

int readInt()
{
    // <-- skip all non-numeric characters --
    int cc = cin.get();
    while(cc < '0' || cc > '9')
        cc = cin.get();
    // -- end -->
    // <-- read each digit of the number --
    int ret = 0;
    while(cc >= '0' && cc <= '9')
    {
        // take the current number, multiply by 10, and add the digit.
        ret = ret * 10 + ( cc - '0' );
        // read the next digit
        cc  = cin.get();
    }
    // -- end -->
    return ret;
}

That's about as much as it could be spelled out. I replaced the getc(stdin) with cin.get(), just because that's more in C++ style (cin is the C++ input stream, and stdin and its related functions is part of the old C legacy that C++ carries around, which is usually to be avoided except in special cases). Also, the code should not include the <stdlib.h> header, for two reasons: (1) because it's not a standard C++ header (it's a C header) and the C++ equivalent for it is <cstdlib>, and (2) because it's not the correct header for what is needed there, which is stdin and getc which are declared in the stdio.h (C) or <cstdio> (C++) header, not stdlib.

If you take into account the different changes that I have made to the code or that I have just pointed out, you will understand why NathanOliver calls this code "terrible". It's non-standard, badly written, and poorly formatted code. And if you really need to fastest possible method to read integers from the standard input stream, then I think that using scanf("%d",&p); should do it just fine, or even cin >> p; which is usually fast enough, since most of the overhead is coming from hard-drive access, not the actual function that does the conversion to integer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.