LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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-05-2005, 09:45 PM   #1
twirl
Member
 
Registered: Aug 2005
Posts: 168

Rep: Reputation: 15
Help needed with last part of code!


Hi,

PLEASE IGNORE MY OTHER POST WITH THE OTHER CODE AS THE ONE BELOW IS MORE COMPLETE AND CAN A MOD PLEASE DELETE IT? Thnakyou
--------------------------------------------------------------------------------------------

I have started getting into more advanced stuff what is above what i know, and i would really like some help to get this project finished. The problem i have is i do not know how to write a correct loop so it enters the correct path and executes the correct start file from games.db and does a count of what is stored in the file. Please help or if you wish to finish it off you are more than welcome. Also should i give up on c++ ?

Code:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
 
int main() {
  long pos;

 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
              break ;
             } 
    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;
             }                  
             outfile.close();           // close file games.db
             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(':'));  
             }   
                  
                 outfile.seekp(0);    // Go to start of games.db file
                 chdir(s1);
                 system (s2);  
                 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;

 
}
 
Old 10-06-2005, 03:32 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Re: Help needed with last part of code!

Why not continue in your original post(s)?

Quote:
Originally posted by twirl
Also should i give up on c++ ?
Only if you are already programming for 10 years in C++ and still can't solve this problem Else just take it slowly. Break your problem into small pieces and test them individually.
I.e. you have a problem with some string stuff, so write a seperate little program to test it out. Once you're familiar how it works, implement it in your main code. That way, problems are easier to detect as they're not obscured by other problems in your code.

No comment on your code for now (did not look at it).
 
Old 10-06-2005, 03:47 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
it's difficult to read.
you should break it into smaller functions.
say like, run(), edit(), choose() etc.
get each individual pieces working and then
fit it together.
your main should be releatively simple; calling lower-level
functions.
maybe something like:
Code:
while ( choice = select_action() ) {
    switch(choice)
       case 1: exit();
       case 2: edit();
  etc....
 
Old 10-06-2005, 05:45 AM   #4
twirl
Member
 
Registered: Aug 2005
Posts: 168

Original Poster
Rep: Reputation: 15
Hi,

Thankyou for your reply, ok i have made my code a bit neater so hopefully you can read it better now. All the rest of the code works apart from the part where i enter directory and execute the start file as that is the part that dont work, also i need to know how i can use the count function in that loop so it keeps a track record of whats stored in games.db file. Also how can i add a quit menu to each sub menu so the user presses Q and it will then exit out of the program please? Thankyou

Code:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
 
int main() {
  long pos;

 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);    // Create file "games.db"
 string whole,p1,p2,p3;                     // Create strings for spltting file into 3 parts.
 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<<"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') // check choice = 1
             {
             cout << "\n\nPlease enter full-path to game: ";
             cin.get();
             cin.getline (s1, 80); // Get input of path
             outfile << s1 << ";."; // add ; after each path
                          
                        
             cout << "\n\nPlease enter name of startup file: e.g ./start.sh ";
             cin.get();
             cin.getline (s2, 40); // Get input of startup file name
             outfile << s2 << ":";  // add after each startup file
			
             cout << "\n\nPlease enter game name: ";
             cin.getline (s3, 90); // Get input of game name
             outfile << s3 << "\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 << "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
              break ;
             } 
    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;
             }            
      
             outfile.close();           // close file games.db
             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(':'));  
             }   
                  
             outfile.seekp(0);    // Go to start of games.db file
             chdir(s1); // enter directory of path
             system (s2);  // execute start script
             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;

 
}
 
Old 10-06-2005, 06:29 AM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally posted by twirl
ok i have made my code a bit neater
Don't see much difference
Quote:
Originally posted by twirl
Also how can i add a quit menu to each sub menu so the user presses Q and it will then exit out of the program please? Thankyou
Code:
case '1': cout << "1. Add Game To DB\n";
             cout << "Q. Back to main menu\n";
             cout << "Please enter your choice: ";
             cin>>choice;

// catch choices here (use switch instead of if)
switch '1':
 ...
break;
switch 'Q':
switch 'q':
 ...
break;
 
Old 10-06-2005, 09:27 PM   #6
twirl
Member
 
Registered: Aug 2005
Posts: 168

Original Poster
Rep: Reputation: 15
Hi,

Thankyou for your reply, i have been thinking about how i can use the count function to count what is stored in games.db i was going do it by placeing numbers on each line, but then i will have a problem as my program reads the path and it will read them numbers aswell. So have you any idea how i can use the count function in my program so it knows what is stored in games.db file? Also i still have problems understanding how i can make it go into the correct path and execute the correct start file please help. <--- Maybe im just thick?

Thankyou

twirl
 
Old 10-07-2005, 01:20 AM   #7
twirl
Member
 
Registered: Aug 2005
Posts: 168

Original Poster
Rep: Reputation: 15
Hi,

Ok i have nearly got the changing directory and executing path working and have added couts to my code so i can see what is happening. The problem is when trying to run a game say bzflag what im using to test i get this :-

sh: /bzflag: No such file or directory
/usr/share/games//bzflag

for some reason my program aint adding the DOT before the game name, can someone please tell me where i have gone wrong in my code please?

Code:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
 
int main() {
  long pos;

 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);    // Create file "games.db"
 string whole,p1,p2,p3;                     // Create strings for spltting file into 3 parts.
 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<<"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') // check choice = 1
             {
             cout << "\n\nPlease enter full-path to game: ";
             cin.get();
             cin.getline (s1, 80); // Get input of path
             outfile << s1 << ";."; // add ; after each path
                          
                        
             cout << "\n\nPlease enter name of startup file: e.g ./start.sh ";
             cin.get();
             cin.getline (s2, 40); // Get input of startup file name
             outfile << s2 << ":";  // add after each startup file
			
             cout << "\n\nPlease enter game name: ";
             cin.getline (s3, 90); // Get input of game name
             outfile << s3 << "\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 << "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
              break ;
             } 
    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;
             }            
      
             outfile.close();           // close file games.db
             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(':'));  
             }   
                  
             outfile.seekp(0);    // Go to start of games.db file
             chdir(s1); // enter directory of path
             cout << s1; 
             system (s2);  // execute start script
             cout << s2; 
             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;

 
}
 
Old 10-07-2005, 03:01 AM   #8
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Suppose you run the program for the first time and you already have something in your database. There are some comments in red
Code:
    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;
             }            
      
             outfile.close();           // close file games.db
             //outfile is not even opened
             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(':'));  
             }   
                  
             outfile.seekp(0);    // Go to start of games.db file
             //outfile is not opened, you just closed it
             chdir(s1); // enter directory of path
             cout << s1; 
             //s1 not initialized
             system (s2);  // execute start script
             cout << s2; 
             //s2 not initialized
             outfile.close();   // close file games.db
             //outfile is not even opened
                         
                         // 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;
You must check the initialization of all variables. s1 and s2 probably still contain old values when you run your program and first add some games.

Another little problem that I saw already before
Code:
   default:  //If choice !=1/2/3 then do what it says here
             break;
 
     outfile.close();           // close file games.db
This outfile.close is outside any of the cases of the switch, but inside the switch.

Last edited by Wim Sturkenboom; 10-07-2005 at 04:01 AM.
 
Old 10-07-2005, 04:45 AM   #9
twirl
Member
 
Registered: Aug 2005
Posts: 168

Original Poster
Rep: Reputation: 15
Hi,

The thing is the correct info is getting stored into games.db and i can add many as i want without problems.

twirl
 
Old 10-07-2005, 05:05 AM   #10
twirl
Member
 
Registered: Aug 2005
Posts: 168

Original Poster
Rep: Reputation: 15
Hi,

Thankyou fore your reply, sorry to be annoying but i have 2 more questions what are :-

1. Where should i open my games.db file and close it?
2. How do i check the initialization of all variables ?
3. I have noticed im missing a deletion part in my code what should go under options so the user can delete a game from the db if they no longer have it installed, any idea how i can do that please? <-- silly me lol

Sorry for asking but my book never covered the 2nd part :-(

Thankyou

twirl

P.s it would be nice if i could get this finished as i would like to release it under the free licence (Opensource GPL) so everyone can use it who wishes to.
 
Old 10-07-2005, 05:44 AM   #11
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally posted by twirl
Hi,

Thankyou fore your reply, sorry to be annoying but i have 2 more questions what are :-

1. Where should i open my games.db file and close it?
2. How do i check the initialization of all variables ?
3. I have noticed im missing a deletion part in my code what should go under options so the user can delete a game from the db if they no longer have it installed, any idea how i can do that please? <-- silly me lol

Sorry for asking but my book never covered the 2nd part :-(

Thankyou

twirl

P.s it would be nice if i could get this finished as i would like to release it under the free licence (Opensource GPL) so everyone can use it who wishes to.
1)
open when you need it, close once done
Code:
//pseudo code for case '1'
get input from user
get other input from user
open
write input
write other input
close
2)
Go trough the code and check all the possible scenarios. Reset variables to an invalid value, so you can test for them. I'm not a C++ programmer so I can not really advice there (in C, I would set string pointers to NULL once not in use and store choices in integers and set to e.g. -1).
3)
One way of doing it is reading the whole file and write it back with the exception of the game that needs to be deleted. Easiest is probably to rename the original to something like games.db.backup. Read that one and write it back to games.db as described above.

Weekend, so off for 2 days
 
  


Reply



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
Code Conversion Help Needed fpfernando Programming 2 10-09-2005 11:36 PM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
Clarification needed in this reg exp/awk code mselvam Programming 3 07-09-2005 05:26 PM
Server Crashing.. Help? (Guru needed??) Part 2 ckr Mandriva 9 02-27-2005 12:15 PM
find a specific part of linux code mhmo Linux - General 1 03-04-2004 09:53 AM

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

All times are GMT -5. The time now is 03:58 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
Open Source Consulting | Domain Registration