How to convert string to char?
Hi,
I am trying to convert a string into a char so i can then copy p1 into s1 and p2 into s2. The problem i get is these errors when compileing :- g++ gamepro8.cpp gamepro8.cpp: In function `int main()': gamepro8.cpp:148: error: no matching function for call to `strcpy(std::string&, char[80])' /usr/include/string.h:85: error: candidates are: char* strcpy(char*, const char*) gamepro8.cpp:150: error: `print' undeclared (first use this function) gamepro8.cpp:150: error: (Each undeclared identifier is reported only once for each function it appears in.) gamepro8.cpp:150: error: syntax error before string constant My code is below :- Code:
#include <iostream> |
Does the "p1=s1;" line not work? You can't use strcpy() on C++ string objects.
|
Hi,
Thankyou for your reply. I removed the strcopy and it seems to nearly compile with p1=s1; and p2=s2 the problem i have now is i get an error with print "s2"; I get this error :- g++ gamepro8.cpp gamepro8.cpp: In function `int main()': gamepro8.cpp:149: error: `print' undeclared (first use this function) gamepro8.cpp:149: error: (Each undeclared identifier is reported only once for each function it appears in.) gamepro8.cpp:149: error: syntax error before string constant Thankyou twirl |
how many times are you planning on pasting in the same lump of code... ??? i've seen what.. 10 times already? only give us the lines that are clearly wrong
|
Quote:
Code:
p1=s1; Code:
p1=s1; |
cout accepts a string object:
Code:
string s; Code:
printf("%s\n", s.c_str()); |
replace
Code:
using std::ofstream; Code:
using namespace std; |
You seam to be making a game (or attempting too). I suggest a smaller project before you kill yourself. ;)
The others have already said all you need to know. use cout, or get the actual char * string with string::c_str(). (e.g. mystring.c_str()). |
Hi,
Thankyou for your reply. No it aint a game, its a small program for people who run lan partys/game servers so they can edit/save etc configs/add games to the db/run games from a small menu. I get the path using cout what goes into s1 and i then get the execute directory with cout what is s2. So do i need to change chdir(s1); to chdir(p1); and system(s2); to system(p2); somehow? Thankyou twirl |
Hi,
I have changed my code to use string couts as sind suggested and have placed this at the top of my code :- Code:
string p; QUOTE]g++ gamepro10.cpp -o gamepro gamepro10.cpp: In function `int main()': gamepro10.cpp:137: error: cannot convert `std::string' to `const char*' for argument `1' to `int atoi(const char*)' gamepro10.cpp:141: error: cannot convert `std::string' to `const char*' for argument `1' to `int atoi(const char*)' gamepro10.cpp:150: error: cannot convert `std::string' to `const char*' for argument `1' to `int chdir(const char*)' gamepro10.cpp:152: error: cannot convert `std::string' to `const char*' for argument `1' to `int system(const char*)'[/QUOTE] My code below :- Code:
std::cout << "atoi(r) = " << atoi(r) << endl; |
Hi,
Please repleace the above code where it says s1 to p and s2 to s . Thankyou twirl |
Hi,
I have changed my code to the following useing c_str() the problem i get is when it reaches the atoi part of my card i get these errors, please adise thankyou :- Quote:
My Code below :- Code:
r.c_str(); // convert string r to const char |
Code:
std::cout << "atoi(r) = " << atoi(r) << endl; Code:
const char * myChar[] = r.c_str(); |
Hi,
Thankyou for your reply. I have tried doing what you said and i now get these errors, my code is below them. :- Quote:
My code :- ---------------------------------------------------------------------------------------- Code:
r.c_str(); // convert string r to const char |
:tisk:
the reply i gave was a simple example and not a solution to the error messages. you were supposed to read what i said (i guess you did, but..) and take that knowledge and apply it. i told you that you use the .c_str() function and assign its' return value to a const * char[]. Code:
r.c_str(); // convert string r to const char are you writing those comments, or is this someone else's code??!! :confused: Code:
const char * myChar[1024] = r.c_str(); you also dont need to specify the 1024, c++ is smart enough to figure it out (when you declare it this way). so you can omit the length/1024. with all respect, i dont think i can help, or want to, with this example.. as others have suggested.. until you grasp these basic concepts, you should read some tutorials or a book so you can gain more knowledge and experience before attempted a program like this. for example, to read a basic breakdown of the c_str() function, click here or that atoi function you were talking about earlier, click here |
All times are GMT -5. The time now is 09:56 AM. |