This question already has answers here:
printf with std::string?
(9 answers)
Closed 9 years ago.
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 printfand 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;
Related
This question already has answers here:
How to convert a std::string to const char* or char*
(11 answers)
Closed 2 years ago.
I want to create sth like:
int main ()
{
string s{};
std::cout << "Enter CMD: \n";
getline(cin,s);
system(s);
}
But since I can use only const char on system, its not working at all, is there any different solution to this? mabye shellexecute?
You can use std::string::c_str().
system(s.c_str());
This question already has answers here:
How to read and write a STL C++ string?
(3 answers)
Closed 4 years ago.
I've recently learnt about std::substr() by searching on Google. I saw a code something like this:
std::string s = "This is an example string";
std::string s1 = s.substr(11, 7);
std::cout << s1 << std::endl;
Now if I try to take input using scanf() function (instead of using std::cin), the program crashes during runtime. Doesn't std::string support using scanf() function?
scanf() belongs to a family of C functions that, being part of the C language rather than C++, offers no direct support for std::string and works instead with null terminated character strings.
If you are using C++, you should generally prefer std::string over null terminated terminated character strings and the input/output library over printf()/scanf() library functions.
This question already has answers here:
Dynamically allocate user inputted string
(4 answers)
Closed 5 years ago.
I want to get unlimited chars, actually I created char*, I use cin for it, and I want to change it to string.
I don't know the length of the input which user enters, so I made this solution for myself.
Can someone tell me plz, how to get a char* without knowing the size of input and converting to string.
thanks.
Since it is C++. Just use std::string
If you really need to use char* look at this topic
Instead of using a char *, use the standard library.
#include <string>
#include <iostream>
int main()
{
std::string data;
std::getline(std::cin, data);
std::cout << data << '\n';
}
This will read a string of any length (at least, until a newline is entered, which will not be included in data) and then print it out.
You might wish to also check the state of std::cin to test if any errors occurred.
This question already has answers here:
How to construct a std::string with embedded values, i.e. "string interpolation"?
(8 answers)
Closed 2 years ago.
I am currently learning C++ and I cannot find how to create a string with a formatter to take multiple parameters such as using sprintf but for an unknown string length.
What I want do is something like
string myString = string.format("Hello, here is %s %s and another %s", par1, par2, par3);
I know in C there is the asprintf function where it works out how long the string will be and it malloc's the memory and you need to free it when finished, however, this doesn't seem to be available for C++, although sprintf is. Everything I've seen on google about asprintf seems to mostly focus on Linux, whereas I need cross platform.
Everything I've seen about C++ and string formatting you need to define a char array of fixed length and use sprintf, but I am not going to know the length of the string so I can't do this.
In addition to the existing excellent answer, you may consider the Boost Format library.
Example:
std::string myString = str(boost::format("Hello, here is %s %s an another %s") % par1 % par2 % par3);
Get the book The Standard C++ Library by Josuttis. It will give you the complete string interface and much, much more. You appear to be thinking C, not C++. You could of course use a C interface like sprintf() to load a
char[] and then copy that to a C++ string. That is usually a bad idea.
Two ways to do what you ask:
string myString("Hello, here is ");
myString += par1;
myString += " ";
myString += par2;
myString += " and another ";
myString += par3;
stringstream foo;
foo << "Hello, here is " << par1 << " " << par2 << " and another " << par3;
string myString(foo.str());
There are lots of answers.
As C++ strings get very long, you want to use the std::stringstream to build them. This allows you to write to a string as though it were a file in memory, and it is written to handle very large strings efficiently. The C function snprintf() returns the number of characters it would have written if passed a null pointer. So you need to call it twice, once to get the size, then allocate the buffer and call again to format. It's a good solution for strings which are expected to be quite short and with a defined format, but might get arbitrarily long, like a string containing someone's name.
Note that printf() formats are convenient and easy to use for basic output of integers, string, and reals, but they don't scale up to user-defined objects because there's no accepted way of writing a toString() method and destroying the string after the call. They also can't handle arrays of objects. There is an accepted convention that overloading << writes a text representation of an object to a stream.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Using strtok with a string argument (instead of char*)?
When using strtok() i do the following
char str[300];
while(infile) {
infile.getline(str,300);
char* token=strtok(str," ");
How can i use a string instead of the character array char str[300];
is there a way to use it to be like this,string str;
while(infile) {
infile.getline(str,300);
char* token=strtok(str," ");
I don't think you can, at least not without great care; strtok() modifies its argument, writing a \0 into it after every recognized token, and generally behaves like a function that's poorly behaved even for C, much less C++. My advice would be to look for a native C++ solution instead.
If you mean an std::string, you cannot, strtok only works with char*.
An easy solution could be that of strdup your string.c_str, and pass it to strtok.
string str;
while(infile)
{
getline(infile, str);
char* token=strtok(&str[0], " ");
}
Clean it ain't, but it will work.
EDIT: My mistake, this may not work in all circumstances.