1

Firstly, look at the following simple code.

int main(){
    char *name;
    cout << "Enter your name: ";
    cin >> name;
    cout << "Your name is: " << name;

    return 0;
}

The previous code gives me the following error warning: deprecated conversion from string constant to 'char*'.
but I have been solved the problem by:

const char *name;

After compile the code, I have another error no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' and 'const char*').

What the reason of the previous error, and how to solve it ?

Ninja
  • 48
  • 5
  • 3
    `char *` does not allocate any storage for the string to go in. You should use `std::string name;` instead. – Neil Kirk Oct 08 '14 at 00:43
  • Both versions (with and without `const`) have partly the same, partly different problems. But since you tagged the question 'c++': Why not use a std::string to avoid memory/pointer issues altogether? – Oguk Oct 08 '14 at 00:44
  • You won't get that warning from that code. I guess it's actually something like `char * name = "something";`. In any case, you can't write to a string constant or an uninitialised pointer, so use `std::string`. – Mike Seymour Oct 08 '14 at 00:46

1 Answers1

5

You haven't initialized any memory into which the string can be read. char * is a pointer to a location in memory where a string can be read, but the memory first has to be allocated using new or malloc.

However, in C++ there is another, better option: use std::string:

#include <string>

int main()
{
    std::string name;
    cout << "Enter your name: ";
    cin >> name;
    cout << "Your name is: " << name;

    return 0;
}

If you are set on using a c-string, you could do allocate memory and do something like the following:

int main()
{
    char name[MAX_SIZE];
    cout << "Enter your name: ";
    cin.get(name, MAX_SIZE);
    cout << "Your name is: " << name;

    return 0;
}

(Thanks to Neil Kirk for the improvements)

Brett Wolfington
  • 6,587
  • 4
  • 32
  • 51
  • I don't care about use `c` or `c++`, is there is no way to use c-string ? – Ninja Oct 08 '14 at 00:46
  • 1
    @Sunrise Yes but why do you want to? – Neil Kirk Oct 08 '14 at 00:47
  • @Sunrise: Yes, but it's more complicated and error-prone. Why would you want to? – Mike Seymour Oct 08 '14 at 00:47
  • It is just for practice, but please give me any way to use c-string works with my code. – Ninja Oct 08 '14 at 00:50
  • What if the input is more than 1024 characters? – Neil Kirk Oct 08 '14 at 00:52
  • @Sunrise If you tag a question [tag:c++], people will advise you based on the nature of C++ programming. If you insist on C, please see ["What is the simplest way of getting user input in C"](http://stackoverflow.com/questions/7831755/what-is-the-simplest-way-of-getting-user-input-in-c)...and you might realize that all the low-level concerns in managing that is what makes C++ advocates favor C++. Because there's a lot of room for error in C. You could of course use an entirely different language, but C++ has some interesting traits and advantages...including high C compatibility when needed. – HostileFork says dont trust SE Oct 08 '14 at 00:52
  • @Neil - Good catch. All the more reason to leave C behind! – Brett Wolfington Oct 08 '14 at 00:52
  • @Sunrise `char buffer[MAX_SIZE]; cin.get(buffer, MAX_SIZE);` – Neil Kirk Oct 08 '14 at 00:56