So this is my code
string name;
cout <<"\n Enter Your name : \n";
cin >> name;
printf("%s" , name);
and for some weird reasons codeblocks crashes at this
why ?
also how could I fix it ?
thanks
So this is my code
string name;
cout <<"\n Enter Your name : \n";
cin >> name;
printf("%s" , name);
and for some weird reasons codeblocks crashes at this
why ?
also how could I fix it ?
thanks
You should compile with all warnings (e.g. g++ -Wall
). You'll get a useful warning. You want to use c_str
like this
printf("%s", name.c_str());
BTW, why use printf
and why do you forget a \n
at the end of the printf
format string? (or use fflush
)
Better code:
cout << name << endl;
If you need to pass your std::string
to a function that accepts / uses C-style strings (const char *
) for input, use .c_str()
. It returns a const char *
.
This is what you should do when needing to work with existing libraries, system calls, etc. For your own code, it is usually better to find a more C++ way of doing it.
In this case:
std::cout << name << std::endl;