tags:

views:

193

answers:

6

Hi,

I'm new to C++. I want to make a char*. But I don't know how.
In Java is it just this:

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

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

Where I'm focusing on is to paste the integer after the string.


Edit:

I want to use a method that takes an argument char *, so if I try to pass a const char *, it gives an error.

I don't know witch terms I should use to search for this, so maybe this questions is already asked.

Thanks, Martijn

Solved!

The compiler was (is) broken. Thank you!

+2  A: 

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

PS. You can use strcpy to copy the content to new variable and then you will be able to change it.

Artem Barger
But then I get a `const char*` and I need `char*`. I tried with `const_cast<char*>(str)`, but it gives an error in the c++ libraries.
Martijn Courteaux
I suggest you to elaborate more of what you are trying to do here.
Artem Barger
try const_cast<char*>(str.c_str())
Holger Kretzschmar
The error I get when compiling is posted here: http://stackoverflow.com/questions/2289168/c-newbie-is-this-code-valid-or-is-it-the-compiler
Martijn Courteaux
A: 

char * means "pointer to a character".

You can create a pointer to a 'string' like this:

char* myString = "My long string";

Alternatively you can use std::string:

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

Notice the const at the beginning of the last example. This means you can't change the chars that are pointed to. You can do the same with the first example:

const char* = "My long string";
Seb Rose
A: 

Consider using stringstreams:

#include <iostream>
#include <sstream>

using namespace std;
int main ()
{
    int i = 10;

    stringstream t;

    t << "test " << i;

    cout << t.str();
}
IVlad
But it will return string and not char*.
Artem Barger
I think cout << const_cast<char *>(t.str().c_str()); will return the char * he needs, however that is pretty ugly. If the OP MUST have a char *, I suggest the sprintf solution posted by Rupert Jones.
IVlad
+4  A: 

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

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

or the related 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 character pointer (and you haven't said why you think you do), you can get one from a string by using its c_str() member function.

All this should be covered by any introductory C++ text book. If you haven't already bought one, get Accelerated C++. You cannot learn C++ from internet resources alone.

anon
Agree with everything except the last comment... there are some pretty excellent C++ resources and tutorials online, including "A Beginner's C++", "Learn C++ in 21 Days", "Parashift C++ FAQ Lite", and "Advanced C++ Lessons". Of course, practice is also needed.
Michael Aaron Safyan
Michael Aaron Safyan
+1  A: 

When working with char* maybe you wanna work with C directly. Or: Why don't you use std::string when working in C++. In case of C, you can also make use of the sprintf function:

char* s = // initialized properly
sprintf( s, "You: %d CPU: %d", player, cpu );
Rupert Jones
I'm very new. How do I need to initialize `s`?
Martijn Courteaux
For example with `char s[50]` for 49 characters (+ 0 character for string termination) or `char* s = new char[50]` for dynamic allocation.
Rupert Jones
A: 

It probably would have been for the best if C++ had overloaded the "+" operator like you show. Sadly, they didn't (you can though, if you want to).

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

  1. The itoa() routine. This is actually non-standard, but most compilers have it. The nice thing about it is that it returns a pointer to the string, so it can be used in functional-style programming.
  2. sprintf(). The second holdover from C, this routine takes a destination string, a format string, and a list of parameters. How many parameters there are, and how they are interpreted depend on how the "format" string parses. This makes sprintf both immensely powerful and immensely dangerous. If you use this approach, I can pretty much guarantee you will have crash bugs your first few tries.
  3. std::ostringstream. The C++ way. This has pretty much all the power of sprintf(), but is much safer. The drawback 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. That means at least three lines of code are required to do anything with an ostringstream. It is also really ugly, particularly if you try any special formatting.
T.E.D.