0
#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;
}    
3
Contributors
3
Replies
27
Views
2 Years
Discussion Span
Last Post by mike_2000_17
Featured Replies
  • 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 … Read More

0

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.

Edited by NathanOliver

1

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.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.