-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
fast_integer_input.cpp
44 lines (38 loc) · 1.08 KB
/
fast_integer_input.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* @file
* @brief Read integers from stdin continuously as they are entered without
* waiting for the `\n` character
*/
#include <iostream>
/** Function to read the number from stdin. The function reads input until a non
* numeric character is entered.
*/
void fastinput(int *number) {
// variable to indicate sign of input integer
bool negative = false;
int c;
*number = 0;
// extract current character from buffer
c = std::getchar();
if (c == '-') {
// number is negative
negative = true;
// extract the next character from the buffer
c = std::getchar();
}
// Keep on extracting characters if they are integers
// i.e ASCII Value lies from '0'(48) to '9' (57)
for (; (c > 47 && c < 58); c = std::getchar())
*number = *number * 10 + c - 48;
// if scanned input has a negative sign, negate the
// value of the input number
if (negative)
*(number) *= -1;
}
/** Main function */
int main() {
int number;
fastinput(&number);
std::cout << number << std::endl;
return 0;
}