-2

I am trying to read in an input from a user and I know my method needs a char* but is there anyway to make the input of cin able to be used by that char? (look at the comment at char* x.)

string y;
cout << "Enter your file: ";
cin >> y;

char * x = //here is where the string needs to go. If I type in the actual address it works, but I need it to work when the user just cin's the address//

string line,character_line;
ifstream myfile;
myfile.open (x);
while(getline(myfile,line))
{
    if (line[0] != '0' && line[0] != '1')
    {
        character_line = line;
    }

}
Jen
  • 255
  • 1
  • 3
  • 11

2 Answers2

1
char * x = y.c_str();

A simple Google would have provided the result :)

Makka
  • 234
  • 4
  • 13
0

You can simply use the c_str() method of the std::string class. This works:

#include <fstream>
#include <iostream>
#include <string>

int main(void) {
  std::string y;
  std::cout << "Enter your file: ";
  std::cin >> y;
  std::string line,character_line;
  std::ifstream myfile;
  myfile.open (y.c_str(), std::ifstream::in);
  while(getline(myfile,line))
  {
    if (line[0] != '0' && line[0] != '1')
    {   
      character_line = line;
    }   

  }
  return 0;
}
joelmeans
  • 122
  • 1
  • 1
  • 8