LinuxQuestions.org
Review your favorite Linux distribution.
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 09-15-2005, 08:53 PM   #1
twirl
Member
 
Registered: Aug 2005
Posts: 168

Rep: Reputation: 15
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;
}
 
Old 09-15-2005, 11:23 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 19,415

Rep: Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103
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.

Last edited by michaelk; 09-15-2005 at 11:39 PM.
 
Old 09-16-2005, 12:06 AM   #3
twirl
Member
 
Registered: Aug 2005
Posts: 168

Original Poster
Rep: Reputation: 15
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 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
 
  


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
cant find program Circuit Monkey Linux - Newbie 3 03-25-2005 05:36 AM
help me find a few bug when compile program vanhelsing Programming 10 07-28-2004 04:01 PM
help me find a BUG in a little C++ program vanhelsing Programming 3 07-15-2004 09:35 AM
couldn't find program .....my ar$e munkie_poo Mandriva 3 05-24-2004 02:41 PM
Can 't find the bug Linh Programming 2 07-23-2003 04:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:22 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration