LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Please help find bug in my c++ program! (https://www.linuxquestions.org/questions/programming-9/please-help-find-bug-in-my-c-program-363770/)

twirl 09-15-2005 07:53 PM

Please help find bug in my c++ program!
 
Hi,

I have made a small program that makes life a bit easier for people who
run game servers and lan partys in linux. The problem is i am having
problem working out what loop in my program is wrong, and i dont think it
is reading games.db from the correct part. The program should read the
games.db from the begging where the path is and then enter that directory
and execute the start script. The games.db looks like this :-

path;./startscript:gamename e.g /home/twirl/bf1942;./start.sh:Bf1942

The problem is i cannot see what loop in my program aint working correctly
from case 3, as it should cd into the directory and execute the script and
should print out errors if something aint right but it aint doing either.
Please have a look at my code below and help a newbie to c++ out. All your
help is very much appreciated! All code below compiles without errors

Code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
 
int main()
{
 int i=0;
 char s1[80],s2[40],s3[90],s4[256],s5[90];      // set char size for input
 ofstream outfile("games.db", ios::app);
 string whole,p1,p2,p3;
 ifstream infile;
 
// Start Main Menu
 
 char choice='\0';//NULL;
 bool finished=false;
 while (finished != true)
 {
  cout<<"MAIN MENU\n";
  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')
            {
              cout << "\n\nPlease enter full-path to game: ";
                          cin.get();
                          cin.getline (s1, 80); // Input goes in
                          outfile << s1 << ";."; // add ; after each path
                         
                       
              cout << "\n\nPlease enter name of startup file: e.g
./start.sh ";
                          cin.get();
                          cin.getline (s2, 40); // Input goes in$
                          outfile << s2 << ":";  // add after each startup
file
                          cout << "\n\nPlease enter game name: ";
                          cin.getline (s3, 90);
                          outfile << s3 << "\n";
                if (outfile.is_open())
                          {
                          outfile.close();
                          }
                        }
            /* 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')
            {
              cout << "Please enter full path to config file to edit: "; 
// ask user for path to config file
              cin.get();
              cin.getline (s4, 256);        // input goes in
              string cmd = string("pico ") + string(s2);
              system(cmd.data());      // open up pico to edit config
            }
    case '3': cout<<"Run Game";
            i=0;
            infile.open ("games.db", ios::in); 
            while(getline(infile,whole)){
                i++;
                p2=whole.substr(whole.find(':')+1,whole.length()); 
                cout << i << ". " << p2 << endl;
            }                 
            cout << "Please select game you wish to run: " << endl;
            cin.get();
            cin.getline(s5, 100);
            i=1;
            infile.open ("games.db", ios::in); 
            while(getline(infile,whole)){
                i++;
                if(atoi(s5)!=i)
                    continue;
                p1=whole.substr(0,whole.find(':')); 
            }   
            i=0;
            infile.open ("games.db");
            while(getline(infile,whole)){
                i++;
                p3=whole.substr(0,whole.find(':')); 
            chdir(s1);
            system(s2); ( string(p1+" > system_errors").c_str() );
            }             
   
                       
                        // 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;
}


michaelk 09-15-2005 10:23 PM

I'm not a very good programmer but:
You open games.db files multiple times but it is never closed so it stays at EOF. Therefore the second and third while loops are never executed. You can use the infile.seekg command to reset the file back to the beginning each time but I would probably read in the file in to an array. The game selection would be the address of the array instead of having to use a loop. Split the strings the first time into different arrays or use a structure.

I see you are making some progress.

twirl 09-15-2005 11:06 PM

Hi,

Thankyou for your reply, i tried re-writing the strings into variables instead to try and make life easier but for some reason its just going in a loop and i have to exit out of my terminal :o I do hope to get this last part working, as i would like to put it on sourceforge with document under the free license.

Thankyou


All times are GMT -5. The time now is 10:03 AM.