TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
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:

Go to the source code of this file.

Functions

void fastinput (int *number)
 
int main ()
 

Detailed Description

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

Definition in file fast_integer_input.cpp.

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.

Definition at line 11 of file fast_integer_input.cpp.

11 {
12 // variable to indicate sign of input integer
13 bool negative = false;
14 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}

◆ main()

int main ( void )

Main function

Definition at line 39 of file fast_integer_input.cpp.

39 {
40 int number;
41 fastinput(&number);
42 std::cout << number << std::endl;
43 return 0;
44}
void fastinput(int *number)