LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to convert string to char? (https://www.linuxquestions.org/questions/programming-9/how-to-convert-string-to-char-371314/)

twirl 10-11-2005 05:01 AM

Hi, I have tried to do it as you said and i get the following errors :-

Quote:

g++ gamepro10.cpp -o gamepro
gamepro10.cpp: In function `int main()':
gamepro10.cpp:148: error: cannot convert `std::string' to `const char*' for
argument `1' to `int atoi(const char*)'
gamepro10.cpp:152: error: cannot convert `std::string' to `const char*' for
argument `1' to `int atoi(const char*)'
gamepro10.cpp:161: error: cannot convert `std::string' to `const char*' for
argument `1' to `int chdir(const char*)'
gamepro10.cpp:163: error: cannot convert `std::string' to `const char*' for
argument `1' to `int system(const char*)'
gamepro10.cpp:175: error: jump to case label
gamepro10.cpp:144: error: crosses initialization of `const char*startf'
gamepro10.cpp:143: error: crosses initialization of `const char*directory'
gamepro10.cpp:142: error: crosses initialization of `const char*rungame'
gamepro10.cpp:179: error: jump to case label
gamepro10.cpp:144: error: crosses initialization of `const char*startf'
gamepro10.cpp:143: error: crosses initialization of `const char*directory'
gamepro10.cpp:142: error: crosses initialization of `const char*rungame'
My code below :-

-------------------------

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 * rungame = r.c_str();
            const char * directory = p.c_str();
            const char * startf = 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

            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;

 
}


jtshaw 10-11-2005 05:19 AM

There seams to be some confusion here.

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

The above lines don't change the types of r, p, and s. They simply return a c string version of r, p and s (a c string is a char *).

Anywhere you do something like this:
std::cout << "atoi(r) = " << atoi(r) << endl;

Needs to be changed to this:
std::cout << "atoi(r.c_str()) = " << atoi(r.c_str()) << endl;

jtshaw 10-11-2005 05:28 AM

Quote:

Originally posted by nadroj

Code:

            const char * myChar[1024] = r.c_str();
            const char * myChar[1024] = p.c_str();
            const char * myChar[1024] = s.c_str();


I don't think this is quite what you want here.... a c_str is a "char *" not a "char **" (or a char *[]).

Code:

const char *temp = str1.c_str();
The above should work just fine. Because c_str() is returning a pointer to a constant char* you shouldn't need to worry about the size of the string... just know you shouldn't be trying to modify it in that form.

twirl 10-11-2005 05:48 AM

Hi,

Thankyou for your reply. I am still having problems converting std::string to const char the errors i
get are below followed with my code.


Quote:

g++ gamepro11.cpp -o gamepro
gamepro11.cpp: In function `int main()':
gamepro11.cpp:160: error: cannot convert `std::string' to `const char*' for
argument `1' to `int chdir(const char*)'
gamepro11.cpp:162: error: cannot convert `std::string' to `const char*' for
argument `1' to `int system(const char*)'
gamepro11.cpp:174: error: jump to case label
gamepro11.cpp:145: error: crosses initialization of `const char*starfile'
gamepro11.cpp:144: error: crosses initialization of `const char*path'
gamepro11.cpp:143: error: crosses initialization of `const char*rungame'
gamepro11.cpp:178: error: jump to case label
gamepro11.cpp:145: error: crosses initialization of `const char*starfile'
gamepro11.cpp:144: error: crosses initialization of `const char*path'
gamepro11.cpp:143: error: crosses initialization of `const char*rungame'
My code

------------------------------------------------
Code:

r.c_str(); // return a c string version of r
            p.c_str(); // return a c string version of p
            s.c_str(); // return a c string version of s
           
                       
            const char *rungame = r.c_str();
            const char *path = p.c_str();
            const char *starfile = s.c_str();

            std::cout << "atoi(r.c_str()) = " << atoi(r.c_str()) << endl;
            while(getline(infile,whole)){
                i++;
                std::cout << "i = " << i << endl;
                if(atoi(r.c_str())!=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

            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;


jtshaw 10-11-2005 05:56 AM

Ok, lets go over this one more time....

If you get an error that says this:
error: cannot convert `std::string' to `const char*' then it means that somewhere in your code (AKA the line in the error message) you are passing in a std::string instead of a c_string (or const char *).

Lets break down the first one.
gamepro11.cpp:160: error: cannot convert `std::string' to `const char*' for
argument `1' to `int chdir(const char*)'

On line 160 we have the following statement:
chdir(p); // enter directory of path
Now, the error says that argument 1 of int chdir(const char*) is the problem. As we see here, p is a std::string. chdir wants a const char *. As a result this needs to be changed to chdir(p.c_str());.

As far as the cross-initialization goes... you don't need the
Code:

const char *rungame = r.c_str();
const char *path = p.c_str();
const char *starfile = s.c_str();

because you aren't using them for anything (at least not in the above code you aren't).

jtshaw 10-11-2005 06:04 AM

As far as the last type of error goes (error: jump to case label)....
I believe this is the problem:
Code:

            // 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;

You have two breaks in the same case statement (that code is right before you finish case '3'. Looks like a simple copy paste problem....

twirl 10-11-2005 06:16 AM

Hi,

Thankyou for your reply i fixed the code by doing the following, and it now compiles. Thankyou all for your help :-

Code:

r.c_str(); // return a c string version of r
            p.c_str(); // return a c string version of p
            s.c_str(); // return a c string version of s
           
            {           
            const char *rungame = r.c_str();
            const char *path = p.c_str();
            const char *starfile = s.c_str();
            }
            std::cout << "atoi(r.c_str()) = " << atoi(r.c_str()) << endl;
            while(getline(infile,whole)){
                i++;
                std::cout << "i = " << i << endl;
                if(atoi(r.c_str())!=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.c_str()); // enter directory of path
            std::cout <<  "Executing system(" << s << ")" << endl;
            std::system(s.c_str());

            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;

 
}


twirl 10-11-2005 06:59 AM

Hi, how do i solve my problems, now i have got it to compile please?

1. I have to press enter for program to load
2. Dont ask for game name when adding game to games.db
3. When adding games to the games.db i have to press enter after each line
4. Its storing wrong information into games.db like Please enter full-path to game:;.Please enter name of startup file: e.g ./start$
5. When executing game it does :-

Run Game1. ;.Please enter name of startup file: e.g ./start.sh:Please enter name of game:
Please select game you wish to run:
atoi(r.c_str()) = 0
i = 2
Changing directory to <Please enter full-path to game:>
Executing system(Please enter name of startup file: e.g ./start.sh)
sh: Please: command not found


MAIN MENU
1. AddGame
2. Options
3. RunGame
4. Exit
Please enter your choice:


My code :-

---------------------------------------------------

Code:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>


using namespace std;

 
int main() {
  long pos;

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

 std::string p;
 std::string s;
 std::string n;
 std::string c;
 std::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:";

 ofstream outClientFile("games.db", ios::app);    // Create file "games.db"
 
 std::string line;  getline(std::cin, line);

// 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 << p << endl;
            std::cin.get();
            std::getline(std::cin, line); std::getline(std::cin, line);
            outClientFile << p << ";."; // add ; after each path
                         
                       
            std::cout << s << endl;
            std::cin.get();
            std::getline(std::cin, line); std::getline(std::cin, line);
            outClientFile << s << ":";  // add after each startup file
                       
            std::cout << n << endl;
            outClientFile << n << "\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 <<  c << endl;  // ask user for path to config file
            std::cin.get();
            std::getline(std::cin, line); std::getline(std::cin, line);
           
              string cmd = string("pico ") + string(c);
              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 << r << endl;
            std::cin.get();
           
            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
            }
                       
           
            {           
            const char *rungame = r.c_str(); // convert string r to const char*
            const char *path = p.c_str();    // convert string p to const char*
            const char *starfile = s.c_str();// convert string s to const char*
            }
            std::cout << "atoi(r.c_str()) = " << atoi(r.c_str()) << endl;
            while(getline(infile,whole)){
                i++;
                std::cout << "i = " << i << endl;
                if(atoi(r.c_str())!=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.c_str()); // enter directory of path
            std::cout <<  "Executing system(" << s << ")" << endl;
            std::system(s.c_str());

            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;

 
}


sind 10-11-2005 11:43 PM

I'll try not to rewrite you code for you, but I think highlighting the trouble areas should help. I've added line numbers to help explain.

Code:

      1 #include <iostream>
      2 #include <fstream>
      3 #include <string>
      4 #include <cstdlib>
      5
      6
      7 using namespace std;
      8
      9
    10 int main() {
    11  long pos;
    12
    13  int i=0;
    14  int i1=0;
    15  int i2=0;
    16
    17  std::string p;
    18  std::string s;
    19  std::string n;
    20  std::string c;
    21  std::string r;
    22  p="Please enter full-path to game:";
    23  s="Please enter name of startup file: e.g ./start.sh";
    24  n="Please enter name of game:";
    25  c="Please type in name of config you wish to edit:";
    26  r="Please select game you wish to run:";

    27
    28  ofstream outClientFile("games.db", ios::app);    // Create file "games.db"
    29
    30  std::string line;  getline(std::cin, line);
    31
    32 // exit program if unable to create program
    33
    34  if ( !outClientFile ) { // overloaded ! operator
    35    cerr << "File could not be opened" << endl;
    36  exit( 1 );
    37
    38  } // end if
    39
    40  string whole,p1,p2,p3;                    // Create strings for spltting file into 3 parts.
    41  std::ifstream infile;                          // Open games.db to read
    42
    43
    44 // Start Main Menu
    45
    46  char choice='\0';//NULL;      // set choice to zero         
    47  bool finished=false;
    48  while (finished != true)      // keep program to run till users wishes to exit 
    49  {
    50
    51 // Start Main Menu
    52  std::cout<<"MAIN MENU\n";
    53  std::cout<<"1. AddGame\n";
    54  std::cout<<"2. Options\n";
    55  std::cout<<"3. RunGame\n";
    56  std::cout<<"4. Exit\n";
    57  std::cout << "Please enter your choice: ";
    58  std::cin>>choice;
    59  std::cout<<"\n\n";
    60
    61  // Start function for submenu
    62  switch(choice)
    63  {
    64    case '1': std::cout << "1. Add Game To DB\n";
    65              std::cout << "2. Go back\n";
    66              std::cout << "Please enter your choice: ";
    67              std::cin>>choice;
    68
    69              if(choice == '1') // check choice = 1
    70              {
    71              std::cout << p << endl;
    72              std::cin.get();
    73              std::getline(std::cin, line); std::getline(std::cin, line);
    74              outClientFile << p << ";."; // add ; after each path
    75
    76
    77              std::cout << s << endl;
    78              std::cin.get();
    79              std::getline(std::cin, line); std::getline(std::cin, line);
    80              outClientFile << s << ":";  // add after each startup file
    81
    82              std::cout << n << endl;
    83              outClientFile << n << "\n";
    84
    85              if (outClientFile.is_open()) // check if games.db is open
    86              {
    87                outClientFile.close(); // close games.db
    88              }
    89              }
    90
    91              /* Just break if choice != 1 */
    92
    93              break;
    94    case '2': std::cout<<"Options\n";
    95              std::cout<<"1. Edit Game Config\n";
    96              std::cout<<"2. Go back\n";
    97              std::cout << "Please enter your choice: ";
    98              std::cin>>choice;
    99
    100              if(choice=='1') // check choice = 1
    101              {
    102              std::cout <<  c << endl;  // ask user for path to config file
    103              std::cin.get();
    104              std::getline(std::cin, line); std::getline(std::cin, line);
    105
    106              string cmd = string("pico ") + string(c);
    107              system(cmd.data());      // open up pico to edit config
    108              }
    109              break;
    110
    111    case '3': std::cout<<"Run Game";
    112              i=0;
    113              infile.open ("games.db", ios::in);
    114
    115              if (!infile) {
    116                std::cout << "Error with infile" << endl;
    117                return 0; // or maybe try to recover gracefully without bailng out
    118              }
    119
    120              while(getline(infile,whole)){
    121                  i++;
    122                  p2=whole.substr(whole.find(':')+1,whole.length());
    123                  std::cout << i << ". " << p2 << endl;
    124              }
    125
    126              //outfile.close();          // close file games.db
    127              infile.close();
    128              infile.clear();
    129
    130              std::cout << r << endl;
    131              std::cin.get();
    132
    133              i=1;
    134              infile.open ("games.db", ios::in);
    135
    136              if (!infile) {
    137                std::cout << "Error with infile" << endl;
    138                return 0; // or maybe try to recover gracefully without bailng out
    139              }
    140
    141
    142            {
    143            const char *rungame = r.c_str(); // convert string r to const char*
    144            const char *path = p.c_str();    // convert string p to const char*
    145            const char *starfile = s.c_str();// convert string s to const char*

    146            }
    147              std::cout << "atoi(r.c_str()) = " << atoi(r.c_str()) << endl;
    148              while(getline(infile,whole)){
    149                  i++;
    150                  std::cout << "i = " << i << endl;
    151                  if(atoi(r.c_str())!=i)
    152                      continue;
    153                  p1=whole.substr(0,whole.find(':'));
    154              }
    155
    156              outClientFile.seekp(0);    // Go to start of games.db file
    157
    158
    159              std::cout << "Changing directory to  <" << p << ">" << endl;
    160              chdir(p.c_str()); // enter directory of path
    161              std::cout <<  "Executing system(" << s << ")" << endl;
    162              std::system(s.c_str());

    163
    164              outClientFile.close();  // close file games.db
    165
    166                          // Here we would check 'choice' and probably move to another functi$
    167              std::cout<<"\n\n";
    168              break;
    169
    170              // Here we would check 'choice' and probably move to another functi$
    171              std::cout<<"\n\n";
    172              break;
    173
    174    case '4': //Finished so set finished 'true' to break out of loop
    175              finished=true;
    176              std::cout<<"Exiting....\n";
    177              break;
    178    default:  //If choice !=1/2/3 then do what it says here
    179              break;
    180
    181      outClientFile.close();          // close file games.db
    182
    183              //  end functions
    184  }
    185  choice='\0'; //NULL
    186  }
    187  return 1;
    188
    189
    190 }

Because you have used:

Code:

using namespace std;
You can get rid of the std::'s everywhere. Doing so will make your code more readable, IMO.

Lines 17-26;
Lines 71, 77, 82, 102, 130, 159:

Because each use of cout here is to print a static string to the screen, the use of string objects is unnecessary. It will cause a (very minor) increase in memory and processor usage, but more importantly in this case, I get the impression that you've confused yourself, and it makes the code more difficult for others to read. Because you don't need to modify what the prompts are saying, I'd suggest that you get rid of the strings and replace the cout lines with (for example):

Code:

cout << "Please enter full-path to game:" << endl;
Line 30:

This is the reason that you have needed to press enter after starting the program.

Lines 73, 79, 104:

Duplicate getlines; these are the reason that you have needed to press enter again after entering something.

Lines 74, 80, 83, 106, 147, 151, 160, 162:

In each of these lines, you've used your prompt string variables, where I think you meant to use line, whole, or some other string.

<edit>
Reading your code again, it looks as though you might be thinking that this:

Code:

std::cout << r << endl;
will take input from the user and put it into r. If this is what you were thinking, remember that cout is an output stream. You need to use cin with the >> operator, or getline()/cin.getline() to get input from the user.
</edit>

Lines 143-145:

These lines are unnecessary, because you don't use the pointers that are returned. If you did need to use something.c_str() a few times, then theoretically you could gain some speed by using pointers like this, rather than calling the function a number of times. You would gain very little speed though, and I'm not sure that the pointer returned by c_str() is guaranteed to be valid after the string changes.

I hope that helps you a bit. :)

~sind

twirl 10-12-2005 06:46 AM

Thankyou very much LINUX QUESTIONS and Sind And Everyone Else! My program now works they way it should and works quite well :D .

Anyone want to design a gui for it please?

Thankyou

twirl 10-12-2005 09:55 AM

Hi,

Ok i have ran into to snags in the program what are :-

1. If a user wishes to use parameters on the same line as ./start.sh -whatever -so -so here he/she cannot do that as it tends to have a char limit?

2.After running a program/game the program does not return to the menu like :-

Run Game1. bfsmd
2. Bf1942
Please select game you wish to run:
2
atoi(r.c_str()) = 2
i = 2
i = 3
Changing directory to </home/twirl/bf1942/>
Executing system(./start.sh)
./start.sh: using dynamically linked binary

Please help so i can fix this.

Thankyou

twirl

sind 10-13-2005 03:44 AM

Sounds like you'd better post the code again. :D Either that or host it somewhere and post a link.

Quote:

twirl wrote:
Anyone want to design a gui for it please?
Have a look at GTK or Qt; these are a couple of the more popular GUI libraries available. GTK has a C-style interface, whereas I believe Qt has a C++ interface (classes and so forth). I've dabbled a bit in GTK and haven't used Qt so I can't give you much more info than that.

Once again, I haven't used it but I remember reading that Glade (which uses GTK, IIRC) can be a quick-start for GUIs.

~sind

twirl 10-13-2005 07:11 AM

Hi,

Thankyou for your reply, here is my latest code with the problems above. Also how would i add a way so the user can delete what is stored in games.db?

2. Is it possible to add a way of showing the user what servers/games are running?

Thankyou

twirl

My code :-

Code:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>


using namespace std;

 
int main() {
  long pos;

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

 std::string p;
 std::string s;
 std::string n;
 std::string c;
 std::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:";

 ofstream outfile("games.db", ios::app);    // Create file "games.db"
 
 

// exit program if unable to create program

 if ( !outfile ) { // 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
 
  cout<<"1. AddGame\n";
  cout<<"2. Options\n";
  cout<<"3. RunGame\n";
  cout<<"4. Exit\n";
  cout << "Please enter your choice: ";
  cin>>choice;
  cout<<"\n\n";
 
  // Start function for submenu
  switch(choice)
  {
  case '1': cout << "1. Add Game To DB\n";
            cout << "2. Go back\n";
            cout << "Please enter your choice: ";
            cin >> choice;
 
            if(choice == '1') // check choice = 1
            {
            cout << p << endl;
            cin >> p;
            outfile << p << ";."; // add ; after each path
                       
            cout << s << endl;
            cin >> s;
            outfile << s << ":";  // add after each startup file
                       
            cout << n << endl;
            cin >> n;
            outfile << n << "\n";
               
            if (outfile.is_open()) // check if games.db is open
              {
              outfile.close(); // close games.db
              }
            }

            /* Just break if choice != 1 */
           
            break;
  case '2': cout<<"Options\n";
            cout<<"1. Edit Game Config\n";
            cout<<"2. Go back\n";
            cout << "Please enter your choice: ";
            cin>>choice;
           
            if(choice=='1') // check choice = 1
            {
            cout <<  c << endl;  // ask user for path to config file
            cin >> c;
           
           
              string cmd = string("pico ") + string(c);
              system(cmd.data());      // open up pico to edit config
            }
            break;
 
    case '3':cout<<"Run Game";
            i=0;
            infile.open ("games.db", ios::in); 
       
            if (!infile) {
              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()); 
                cout << i << ". " << p2 << endl;
            }           
     
            //outfile.close();          // close file games.db
            infile.close();
            infile.clear();

            cout << r << endl;
            cin >> r;
           
            i=1;
            infile.open ("games.db", ios::in);
 
            if (!infile) {
              cout << "Error with infile" << endl;
              return 0; // or maybe try to recover gracefully without bailng out
            }
                       
           
            {           
            const char *rungame = r.c_str(); // convert string r to const char*
            const char *path = p.c_str();    // convert string p to const char*
            const char *starfile = s.c_str();// convert string s to const char*
            }
            cout << "atoi(r.c_str()) = " << atoi(r.c_str()) << endl;
            while(getline(infile,whole)){
                i++;
                cout << "i = " << i << endl;
                if(atoi(r.c_str())!=i)
                    continue;
                p1=whole.substr(0,whole.find(':')); 
            } 
                 
            outfile.seekp(0);    // Go to start of games.db file
           
                       
            cout << "Changing directory to  <" << p << ">" << endl;
            chdir(p.c_str()); // enter directory of path
            cout <<  "Executing system(" << s << ")" << endl;
            system(s.c_str());

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

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

 
}



All times are GMT -5. The time now is 04:37 PM.