Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
fast_integer_input.cpp File Reference

Read integers from stdin continuously as they are entered without waiting for the \n character. More...

#include <iostream>
Include dependency graph for fast_integer_input.cpp:

Functions

void fastinput (int *number)
 
int main ()
 

Detailed Description

Read integers from stdin continuously as they are entered without waiting for the \n character.

Function Documentation

◆ fastinput()

void fastinput ( int *  number)

Function to read the number from stdin. The function reads input until a non numeric character is entered.

11  {
12  // variable to indicate sign of input integer
13  bool negative = false;
14  register int c;
15  *number = 0;
16 
17  // extract current character from buffer
18  c = std::getchar();
19  if (c == '-') {
20  // number is negative
21  negative = true;
22 
23  // extract the next character from the buffer
24  c = std::getchar();
25  }
26 
27  // Keep on extracting characters if they are integers
28  // i.e ASCII Value lies from '0'(48) to '9' (57)
29  for (; (c > 47 && c < 58); c = std::getchar())
30  *number = *number * 10 + c - 48;
31 
32  // if scanned input has a negative sign, negate the
33  // value of the input number
34  if (negative)
35  *(number) *= -1;
36 }
Here is the call graph for this function:

◆ main()

int main ( void  )

Main function

39  {
40  int number;
41  fastinput(&number);
42  std::cout << number << std::endl;
43  return 0;
44 }
Here is the call graph for this function:
fastinput
void fastinput(int *number)
Definition: fast_integer_input.cpp:11
std::cout
std::endl
T endl(T... args)
std::getchar
T getchar(T... args)