LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-09-2005, 04:42 PM   #1
twirl
Member
 
Registered: Aug 2005
Posts: 132

Rep: Reputation: 15
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>
#include <fstream>
#include <string>
#include <cstdlib>


using std::ofstream;
using std::ios;
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::cerr;

 
int main() {
  long pos;

 int i=0;
 int i1=0;
 int i2=0;

 char s1[80],s2[40],s3[90],s4[256],s5[90];  // set char size for input
 ofstream outClientFile("games.db", ios::app);    // Create file "games.db"
 
// exit program if unable to create program

 if ( !outClientFile ) { // overloaded ! operator
   cerr << "File could not be opened" << endl;
 exit( 1 );

 } // end if
  
 string whole,p1,p2,p3;                     // Create strings for spltting file into 3 parts.
 std::ifstream infile;                           // Open games.db to read
 

// Start Main Menu
 
 char choice='\0';//NULL;      // set choice to zero           
 bool finished=false;          
 while (finished != true)      // keep program to run till users wishes to exit   
 {

// Start Main Menu
  std::cout<<"MAIN MENU\n";
  std::cout<<"1. AddGame\n";
  std::cout<<"2. Options\n";
  std::cout<<"3. RunGame\n";
  std::cout<<"4. Exit\n";
  std::cout << "Please enter your choice: ";
  std::cin>>choice;
  std::cout<<"\n\n";
 
  // Start function for submenu
  switch(choice) 
  {
   case '1': std::cout << "1. Add Game To DB\n";
             std::cout << "2. Go back\n";
             std::cout << "Please enter your choice: ";
             std::cin>>choice;
 
             if(choice == '1') // check choice = 1
             {
             std::cout << "\n\nPlease enter full-path to game: ";
             std::cin.get();
             std::cin.getline (s1, 80); // Get input of path
             outClientFile << s1 << ";."; // add ; after each path
                          
                        
             std::cout << "\n\nPlease enter name of startup file: e.g ./start.sh ";
             std::cin.get();
             std::cin.getline (s2, 40); // Get input of startup file name
             outClientFile << s2 << ":";  // add after each startup file
			
             std::cout << "\n\nPlease enter game name: ";
             std::cin.getline (s3, 90); // Get input of game name
             outClientFile << s3 << "\n";
                
             if (outClientFile.is_open()) // check if games.db is open
              {
               outClientFile.close(); // close games.db 
              }
             }

             /* Just break if choice != 1 */
             
             break;
   case '2': std::cout<<"Options\n";
             std::cout<<"1. Edit Game Config\n";
             std::cout<<"2. Go back\n";
             std::cout << "Please enter your choice: ";
             std::cin>>choice;
             
             if(choice=='1') // check choice = 1
             {
             std::cout << "Please enter full path to config file to edit: ";  // ask user for path to config file
             std::cin.get();
             std::cin.getline (s4, 256);        // input goes in
              string cmd = string("pico ") + string(s2);
              system(cmd.data());      // open up pico to edit config
             }
             break;
 
    case '3': std::cout<<"Run Game";
             i=0;
             infile.open ("games.db", ios::in);  
        
             if (!infile) {
               std::cout << "Error with infile" << endl;
               return 0; // or maybe try to recover gracefully without bailng out
             } 

             while(getline(infile,whole)){
                 i++;
                 p2=whole.substr(whole.find(':')+1,whole.length());  
                 std::cout << i << ". " << p2 << endl;
             }            
      
             //outfile.close();           // close file games.db
             infile.close();
             infile.clear();

             std::cout << "Please select game you wish to run: " << endl;
             std::cin.get();
             std::cin.getline(s5, 100);
             i=1;
             infile.open ("games.db", ios::in); 
   
             if (!infile) {
               std::cout << "Error with infile" << endl;
               return 0; // or maybe try to recover gracefully without bailng out
             } 

             std::cout << "atoi(s5) = " << atoi(s5) << endl;
             while(getline(infile,whole)){
                 i++;
                 std::cout << "i = " << i << endl;
                 if(atoi(s5)!=i)
                     continue;
                 p1=whole.substr(0,whole.find(':'));  
             }   
                  
             outClientFile.seekp(0);    // Go to start of games.db file
             
             p1=s1;
             p2=s2;            
             strcpy (p1,s1);
             print "p1" 
             strcpy(p2,s2);
             print "p2"

             std::cout << "Changing directory to  <" << s1 << ">" << endl; 
             chdir(s1); // enter directory of path
             std::cout <<  "Executing system(" << s2 << ")" << endl; 
             system (s2);  // execute start script

             outClientFile.close();   // close file games.db
                         
                         // Here we would check 'choice' and probably move to another functi$
             std::cout<<"\n\n";
             break;
   
             // Here we would check 'choice' and probably move to another functi$
             std::cout<<"\n\n";
             break;

   case '4': //Finished so set finished 'true' to break out of loop
             finished=true;
             std::cout<<"Exiting....\n";
             break;
   default:  //If choice !=1/2/3 then do what it says here
             break;
 
     outClientFile.close();           // close file games.db
 
             //  end functions
  }
  choice='\0'; //NULL
 }
 return 1;

 
}
 
Old 10-09-2005, 05:08 PM   #2
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 48
Does the "p1=s1;" line not work? You can't use strcpy() on C++ string objects.
 
Old 10-09-2005, 05:45 PM   #3
twirl
Member
 
Registered: Aug 2005
Posts: 132

Original Poster
Rep: Reputation: 15
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
 
Old 10-09-2005, 06:31 PM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,393

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
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
 
Old 10-09-2005, 07:51 PM   #5
destuxor
Member
 
Registered: Oct 2005
Posts: 51

Rep: Reputation: 16
Quote:
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
What are you trying to do here?
Code:
             p1=s1;
             p2=s2;            
             strcpy (p1,s1);
             print "p1" 
             strcpy(p2,s2);
             print "p2"
AFAIK there is no "print" function in C/C++, and even if there was you need some parentheses. Perl will let you get away with this syntax, but not C++ (if you really, really want to use this syntax you could use the preprocessor with #define statements). You also need some semicolons in there, like this. Hope I didn't misunderstand what you're doing...
Code:
             p1=s1;
             p2=s2;            
             strcpy (p1,s1);
             printf("%s", p1);
             strcpy(p2,s2);
             printf("%s\n", p2);
 
Old 10-09-2005, 10:40 PM   #6
sind
Member
 
Registered: Jun 2005
Posts: 75

Rep: Reputation: 15
cout accepts a string object:

Code:
string s;

s = "Hello World";

cout << s << endl;
If you want to use printf, you need to get the character buffer that is managed by the string class:

Code:
printf("%s\n", s.c_str());
~sind
 
Old 10-09-2005, 10:56 PM   #7
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 58
replace
Code:
using std::ofstream;
using std::ios;
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::cerr;
with
Code:
using namespace std;
this wont solve any of your problems, as im sure others are helping with that. you dont need to specify every function you will be using, and this will eliminate that.
 
Old 10-10-2005, 12:31 AM   #8
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
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()).
 
Old 10-10-2005, 01:37 AM   #9
twirl
Member
 
Registered: Aug 2005
Posts: 132

Original Poster
Rep: Reputation: 15
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
 
Old 10-10-2005, 09:20 AM   #10
twirl
Member
 
Registered: Aug 2005
Posts: 132

Original Poster
Rep: Reputation: 15
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;
 string s;
 string n;
 string c;
 string r;
 p="Please enter full-path to game:";
 s="Please enter name of startup file: e.g ./start.sh";
 n="Please enter name of game:";
 c="Please type in name of config you wish to edit:";
 r="Please select game you wish to run:";
Now i have done that i get these compile errors with the following code :-

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;
             while(getline(infile,whole)){
                 i++;
                 std::cout << "i = " << i << endl;
                 if(atoi(r)!=i)
                     continue;
                 p1=whole.substr(0,whole.find(':'));  
             }   
                  
             outClientFile.seekp(0);    // Go to start of games.db file
             
                         
             std::cout << "Changing directory to  <" << s1 << ">" << endl; 
             chdir(p); // enter directory of path
             std::cout <<  "Executing system(" << s2 << ")" << endl; 
             system (s);  // execute start script
[
 
Old 10-10-2005, 09:23 AM   #11
twirl
Member
 
Registered: Aug 2005
Posts: 132

Original Poster
Rep: Reputation: 15
Hi,

Please repleace the above code where it says s1 to p and s2 to s . Thankyou

twirl
 
Old 10-10-2005, 11:15 AM   #12
twirl
Member
 
Registered: Aug 2005
Posts: 132

Original Poster
Rep: Reputation: 15
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:
g++ gamepro10.cpp -o gamepro
gamepro10.cpp: In function `int main()':
gamepro10.cpp:140: error: cannot convert `std::string' to `const char*' for
argument `1' to `int atoi(const char*)'
gamepro10.cpp:144: error: cannot convert `std::string' to `const char*' for
argument `1' to `int atoi(const char*)'
gamepro10.cpp:153: error: cannot convert `std::string' to `const char*' for
argument `1' to `int chdir(const char*)'
gamepro10.cpp:155: error: cannot convert `std::string' to `const char*' for
argument `1' to `int system(const char*)'

My Code below :-

Code:
            r.c_str(); // convert string r to const char
            s.c_str(); // convert string s to const charactersSAXFunc

             std::cout << "atoi(r) = " << atoi(r) << endl;
             while(getline(infile,whole)){
                 i++;
                 std::cout << "i = " << i << endl;
                 if(atoi(r)!=i)
                     continue;
                 p1=whole.substr(0,whole.find(':'));  
             }   
                  
             outClientFile.seekp(0);    // Go to start of games.db file
             
                         
             std::cout << "Changing directory to  <" << p << ">" << endl; 
             chdir(p); // enter directory of path
             std::cout <<  "Executing system(" << s << ")" << endl; 
             system (s);  // execute start script
 
Old 10-10-2005, 11:41 AM   #13
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 58
Code:
std::cout << "atoi(r) = " << atoi(r) << endl;
the atoi() function takes in a 'const char*'. the parameter you have passed (r) is a string. you must convert it to a character array, as you tried i think, with the c_str(); method. you need to call the c_str() method of 'r' and assign its value to a character array... then you can use the above code for r. something like
Code:
const char * myChar[] = r.c_str();

Last edited by nadroj; 10-10-2005 at 11:45 AM.
 
Old 10-10-2005, 02:13 PM   #14
twirl
Member
 
Registered: Aug 2005
Posts: 132

Original Poster
Rep: Reputation: 15
Hi,

Thankyou for your reply. I have tried doing what you said and i now get these errors, my code is below them. :-

Quote:
g++ gamepro10.cpp -o gamepro
gamepro10.cpp: In function `int main()':
gamepro10.cpp:141: error: invalid initializer
gamepro10.cpp:142: error: redeclaration of `const char*myChar[1024]'
gamepro10.cpp:141: error: `const char*myChar[1024]' previously declared here
gamepro10.cpp:142: error: declaration of `const char*myChar[1024]'
gamepro10.cpp:141: error: conflicts with previous declaration `const
char*myChar[1024]'
gamepro10.cpp:142: error: invalid initializer
gamepro10.cpp:143: error: redeclaration of `const char*myChar[1024]'
gamepro10.cpp:141: error: `const char*myChar[1024]' previously declared here
gamepro10.cpp:143: error: declaration of `const char*myChar[1024]'
gamepro10.cpp:141: error: conflicts with previous declaration `const
char*myChar[1024]'
gamepro10.cpp:143: error: invalid initializer
gamepro10.cpp:145: error: cannot convert `std::string' to `const char*' for
argument `1' to `int atoi(const char*)'
gamepro10.cpp:149: error: cannot convert `std::string' to `const char*' for
argument `1' to `int atoi(const char*)'
gamepro10.cpp:158: error: cannot convert `std::string' to `const char*' for
argument `1' to `int chdir(const char*)'
gamepro10.cpp:160: error: cannot convert `std::string' to `const char*' for
argument `1' to `int system(const char*)'
gamepro10.cpp:172: error: jump to case label
gamepro10.cpp:141: error: crosses initialization of `const char*myChar[1024]'
gamepro10.cpp:176: error: jump to case label
gamepro10.cpp:141: error: crosses initialization of `const char*myChar[1024]'
linux:/home/andy #
----------------------------------------------------------------------------------------

My code :-
----------------------------------------------------------------------------------------

Code:
r.c_str(); // convert string r to const char
            p.c_str(); // convert string p to const char
            s.c_str(); // convert string s to const char
            
            const char * myChar[1024] = r.c_str();
            const char * myChar[1024] = p.c_str();
            const char * myChar[1024] = s.c_str();

             std::cout << "atoi(r) = " << atoi(r) << endl;
             while(getline(infile,whole)){
                 i++;
                 std::cout << "i = " << i << endl;
                 if(atoi(r)!=i)
                     continue;
                 p1=whole.substr(0,whole.find(':'));  
             }  
        
           outClientFile.seekp(0);    // Go to start of games.db file
             
                         
          std::cout << "Changing directory to  <" << p << ">" << endl; 
          chdir(p); // enter directory of path
          std::cout <<  "Executing system(" << s << ")" << endl; 
          system (s);  // execute start script
 
Old 10-10-2005, 02:55 PM   #15
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 58


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
            p.c_str(); // convert string p to const char
            s.c_str(); // convert string s to const char
this doesnt do anything (really). it returns a c-string of the string r/p/s.. but it doesnt assign it to anything. therefore it basically does nothing. my previous post was an attempt to guide you towards that.
are you writing those comments, or is this someone else's code??!!

Code:
            const char * myChar[1024] = r.c_str();
            const char * myChar[1024] = p.c_str();
            const char * myChar[1024] = s.c_str();
as i said, my post was an example and wasnt the solution to your program. 'myChar' was an EXAMPLE. dont name variables that in a program. name it something appropriate, relevant, and logical to its' purpose. have you read about declaring variables? or constants? you cant assign a value to a constant otherthan at the time you declare it. once you have a variable (myChar) you cant go and..kinda..re-declare it after its been declared (the 2nd and 3rd 'const char * myCha...' 's)
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C# convert char array to string exodist Programming 3 09-16-2008 08:06 AM
c++ : replacing a char in a string cb951303 Programming 17 06-06-2005 05:48 PM
C Problem---convert char to char* totti10 Programming 11 11-06-2004 11:32 AM
Convert C++ string to C char* nyk Programming 3 06-17-2004 08:15 AM
convert from char* to unsigned char* D J Linux - Software 2 02-20-2004 04:09 AM


All times are GMT -5. The time now is 08:20 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration