char* input

Hi everyone,

is there any way to get as an input a chars array, WITH spaces?
i have tried cin.getline, but it has problem with the enter keyboard.
for example, i want to get "A 6" as an input, and than to print it.
both of these doesnt work.
thanks !
1
2
3
4
5
 char s[3];
 1.cin>>s;
~~~~~~~~~~~
char *s=NULL;
 2.cin.getline(s,3);
size of string "A 6" is 4 bytes. Also you are trying to get your line in null pointer.
1
2
3
constexpr size_t size = 128;
char buffer[size];
std::cin.getline(buffer, size);
is there another way to do that without size of bytes?
maybe char** or something?
is there another way to do that without size of bytes?
Elaborate your question. You yourself tried almost this in your (2). I just made it use proper buffer.
i have an array *arr as a member data, and i want to insert names to it,
so when i want to get the input like "a b c d e", it only get a.
so im asking if there is any way to get these inputs with the spaces..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

struct foo
{
    char* arr;

    foo() : arr(new char[16]) {}
    ~foo() { delete[] arr; }
};

int main()
{
    foo bar;
    std::cin.getline(bar.arr, 4); // If you need no more than 3 characters
    std::cout << bar.arr; 
}
a b c d e f
a b
http://coliru.stacked-crooked.com/a/0496006b693383e3
Last edited on
thank you!
Topic archived. No new replies allowed.