First, I've read some references and googled it. But I can't find any way to read input without pressing enter by not using getch()
. while getchar() cin>> and getline()
need enter to read the input. I'm practicing doing questions with online judges and they don't accept conio.h library. Is there any way to do that? Thanks.
Btw, my objective is to get each numbers pressed go into an array.
istream& getline (istream& is, string& str)
, usage:
#include<iostream>
int main(){
std::string output;
std::getline(std::cin, output);
}
One line, taken from std::cin
, is stored in output
. You can read each output
's char using output[i]
, where i
is number of char you want get, or use output.data()
, which returns same data, stored in char[]
.
Each of this methods lets you to read char
as a character. It seems you want to get a digit as a number, not as character.
char
is also number. Each letter has it's own code.
char code
'0'==48
'1'==49
'2'==50
'3'==51
'4'==52
'5'==53
'6'==54
'7'==55
'8'==56
'9'==57
As you can see, digits are beautiful set one after another, and digit more about n
has code more about also n
. How to use it? Simple: just take char
after char
, each reduced by 48
or '0'
for better readability, to get digits as numbers.