C ++: How to build lines / char *

I am new to C ++. I want to make char* , but I don't know how.
In Java, it is only like this:

 int player = 0; int cpu = 0; String s = "You: " + player + " CPU: " + cpu; 

How can i do this? I need a char* .

I focus on inserting an integer after a string.

+7
c ++ string char
source share
6 answers

You almost certainly don't want to deal with char *, if you can help him - you need a C ++ class std :: string:

 #include <string> .. string name = "fred"; 

or the associated stringstream class:

 #include <sstream> #include <string> #include <iostream> using namespace std; int main() { int player = 0; int cpu = 0; ostringstream os; os << "You: " << player << " CPU: " << cpu; string s = os.str(); cout << s << endl; } 

if you really need a pointer to a character (and you didn’t say why you think you think), you can get it from a string using the member function c_str ().

All this should be covered by any introductory textbook in C ++. If you have not bought it yet, get Accelerated C ++ . You cannot learn C ++ from Internet resources only.

+27
source share

If you are working with C ++ just use std::string . If you work with char* , you probably want to work directly with C. In the case of C, you can use the sprintf function:

 char* s = // initialized properly sprintf( s, "You: %d CPU: %d", player, cpu ); 
+6
source share

Just call s.c_str( ); . Here you can see more.

PS. You can use strcpy to copy the contents to a new variable, and then you can change it.

+2
source share

char * means "character pointer".

You can create a pointer to a string:

 char* myString = "My long string"; 

Alternatively, you can use std :: string:

 std::string myStdString("Another long string"); const char* myStdString.c_str(); 

Note the constant at the beginning of the last example. This means that you cannot change the characters you point to. You can do the same with the first example:

 const char* = "My long string"; 
0
source share

Consider using strings:

 #include <iostream> #include <sstream> using namespace std; int main () { int i = 10; stringstream t; t << "test " << i; cout << t.str(); } 
0
source share

It would probably be better if C ++ overloaded the + operator, as you show. Unfortunately, they did not (you can if you want).

There are three methods for converting integer variables to strings in C ++; two inherit from C and one new to C ++.

  • itoa () . This is actually non-standard, but most compilers have it. The best part is that it returns a pointer to a string, so it can be used in functional style programming.
  • sprintf () . The second hold from C, this procedure takes the destination string, format string, and parameter list. How many parameters are and how they are interpreted depends on how the string "format" is parsed. This makes sprintf very powerful and extremely dangerous. If you use this approach, I can pretty much guarantee that you will have glitches in the errors of your first attempts.
  • std :: ostringstream . C ++ path. This has almost all the powers of sprintf (), but is much safer. The downside here is that you have to declare it, and it is not a string, so you still have to convert it to one when you are done. This means that at least three lines of code are needed to do something with ostringstream. This is also very ugly, especially if you try some special formatting.
0
source share

All Articles