-1

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.

user3411184
  • 71
  • 1
  • 10
  • 4
    Online judges have 3 types of input: argc/argv (no need to read that, it's direct), standard in (line based) and text file(also line based). That's why you don't need a "conio.h library" . BTW there's no standard conio.h so the judges wouldn't even know which one you used. `std::getline` as the name suggests is the standard getline. – MSalters Aug 22 '14 at 11:00
  • oh I see. I think the question is line based. So there's no way to get each numbers as an single input? ex. 123 become arr[3] = {1,2,3}; since getline return 123 as 1 number only. – user3411184 Aug 22 '14 at 11:05
  • Well, you already mentioned `cin >>`. That's still line-base in the sense that it will need a line end on input, and it won't read past that line end, but you can read an entire number at once. If you're reading floating point numbers, `cin>>` will even read `123.45` – MSalters Aug 22 '14 at 11:10
  • What's `arr`'s elements' type? – GingerPlusPlus Aug 22 '14 at 11:24

2 Answers2

1

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.

GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52
  • the input will be A and B. 1 – user3411184 Aug 22 '14 at 11:46
  • You can write `int arr[]=new int[nr_of_digits]; /*using array here*/ delete[] arr;` – GingerPlusPlus Aug 22 '14 at 11:50
  • yes when I used getch(); it also returned int so I've reduced it by 48 too. But about the getline(); Pardon me, but I still don't understand how to get each number by using getline(cin,input). when you type "123 45" 'enter'. the array will be [1,2,3,-16,4,5]. (32-48 for space). thanks – user3411184 Aug 22 '14 at 12:07
  • @user3411184 `still don't understand how to get each number by using getline(cin,input)` - **I'm not sure what you want to achieve**... If you don't want to have all digits in 1 array, simply when you find delimiter start writing into another destination. – GingerPlusPlus Aug 22 '14 at 17:41
0

To read numbers, simply use std::cin, I don't understand why you don't want to press Enter to confirm passed number.

When you read char after char, if you want to make possible correcting partially entered number, you have to write additional code - you have to handle Backspace, arrows, Delete, and possibly Home, End, Shift behavior when combined with arrows... That's a lot of work.

GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52
  • that's because I need '1' '2' '3' as seperated numbers to be put into an array. if I use cin>>input and the input is 123. it will only return '123' – user3411184 Aug 22 '14 at 11:38
  • 1
    Imagine you were to play a game of snake and had to press enter every time you changed direction. I don't understand why there is no standard way to do this in C++. – nwp Aug 22 '14 at 11:49
  • @nwp Yeah, that's pretty annoying, but there is also no Standard way of moving text cursor anywhere, changing color of text and it's background, so I don't think there is someone who want write snake without this :) Of course, there are libaries doing it, but some of them also contains something like `getch()`, and this may be portable solution ;) – GingerPlusPlus Aug 22 '14 at 11:54
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Syon Aug 22 '14 at 12:13
  • @Syon yeah, question is quite unclear, I wrote this answer before I read some comments, and I was thinking that he want have few numbers from `stdin` in array of in example `int`s. **Not clarifying question isn't nice.** – GingerPlusPlus Aug 22 '14 at 17:24