ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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;
}
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).
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....
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;
}
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;
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?
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;
}
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.