1

I'm getting an access violation when trying to use std::cin. I'm using a char* and it's not allowing me to input my data.

void Input(){
while(true){
    char* _input = "";
    std::cin >> _input; //Error appears when this is reached..
    std::cout << _input;
    //Send(_input);
kapa
  • 77,694
  • 21
  • 158
  • 175
Troy
  • 11
  • 2
  • possible duplicate of [Segmentation Fault With Char Array and Pointer in C on Linux](http://stackoverflow.com/questions/1773079/segmentation-fault-with-char-array-and-pointer-in-c-on-linux) – iammilind Jul 07 '11 at 05:33

3 Answers3

1

You didn't provide a buffer for cin to store the data into.

operator>>(std::istream&, std::string) will allocate storage for the string being read, but you're using operator>>(std::istream&, char*) which writes to a caller-provided buffer, and you didn't provide a writable buffer (string literals are not writable), so you got an access violation.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
1
char* _input = ""; // note: it's deprecated; should have been "const char*"

_input is pointer pointing to a string literal. Inputting into it is an undefined behavior. Either use

char _input[SIZE]; // SIZE declared by you to hold the enough characters

or

std::string _input;
iammilind
  • 68,093
  • 33
  • 169
  • 336
0

Try this:

char _input[1024];
std::cin >> _input;
std::cout << _input;

Or better:

std::string _input;
std::cin >> _input;
std::cout << _input;
Donotalo
  • 12,748
  • 25
  • 83
  • 121