0

I'm working on assigning an input string from the user to a char* pointer variable because that is what the college has assigned for me to do. However, I find it impossible to do that because of this error:

cannot convert 'std::__cxx11::string*' {aka 'std::__cxx11::basic_string'} to 'char' in assignment

Relevant code:

int secretCode;
string inputString;
char * inputPointer;
cout << "Please enter a positive whole number for the secret key: ";
cin >> secretCode;
cout << "Enter a string to be encoded: ";
getline(cin, inputString,'\t');
inputPointer = &inputString;
cout << *inputPointer;
  • 2
    Use the string's `c_str()` or `data()` method to get a `const char*` to its constituent characters. – Etienne de Martel May 08 '22 at 22:39
  • Once it compiles, you will find another issue: [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – Yksisarvinen May 08 '22 at 22:41
  • The conversion method depends on the context. The easier case is calling a function that takes a `char*` parameter. The harder case is returning a `char*` from a function. Which scenario do you need? – JaMiT May 08 '22 at 22:41
  • Easier case: [API documentation](https://en.cppreference.com/w/cpp/string/basic_string/data). Harder case: [how to copy `char *` into a string and vice-versa](https://stackoverflow.com/questions/2564052/) – JaMiT May 08 '22 at 22:44
  • _cannot convert 'std::__cxx11::string*' {aka 'std::__cxx11::basic_string'} to 'char' in assignment_ lol, it's the assignment that's at fault in the first place, if you see what I mean. – Paul Sanders May 08 '22 at 23:50

0 Answers0