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.
Well, to make the menu, read it in line by line, increment a counter, and print out the second half of the line. You can use substring functions to split the line as needed.
Thankyou Matir, ok iv added the code what you gave to my if statement in case 3, now how do i get it to split it from path:gamename so it puts the gamename into a nice menu please? My code is below :-
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
char s1[30],s2[40],s3[256]; // set char size to 30 and 256 for input
ofstream outfile("games.db", ios::app);
// 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 start file: ";
cin.get();
cin.getline (s1, 30); // Input goes in$
outfile << s1 << ":"; // add : after eachpath
cout << "\n\nPlease enter game name: ";
cin.getline (s2, 40);
outfile << s2 << "\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 (s3, 256); // input goes in
string cmd = string("pico ") + string(s2);
system(cmd.data()); // open up pico to edit config
}
// Here we would check 'choice' and probably move to another functi$
cout<<"\n\n";
break;
case '3': cout<<"Run Game";
if(choice=='3')
{
string whole,p1,p2;
//initialize whole
p1=whole.substr(0,whole.find(':'));
p2=whole.substr(whole.find(':')+1,whole.length());
}
// 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;
// end functions
}
choice='\0'; //NULL
}
return 1;
}
Ok, i think iv either got this wrong way round or im missing something, please bear with me as i am very new to c++ and i aint done any programming since 1997 and that was in turbo pascal. :-
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
char s1[30],s2[40],s3[256]; // set char size to 30 and 256 for input
ofstream outfile("games.db", ios::app);
// 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 start file: ";
cin.get();
cin.getline (s1, 30); // Input goes in$
outfile << s1 << ":"; // add : after eachpath
cout << "\n\nPlease enter game name: ";
cin.getline (s2, 40);
outfile << s2 << "\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 (s3, 256); // input goes in
string cmd = string("pico ") + string(s2);
system(cmd.data()); // open up pico to edit config
}
// Here we would check 'choice' and probably move to another functi$
cout<<"\n\n";
break;
case '3': cout<<"Run Game";
if(choice=='3')
{
string whole,p1,p2;
//initialize whole
p1=whole.substr(0,whole.find(':'));
p2=whole.substr(whole.find(':')+1,whole.length());
int i=0;
while(getline(infile,whole)){
i++;
p2=whole.substr(whole.find(':')+1,whole.length());
cout << i << ". " << p2 << endl;
}
// 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;
// end functions
}
choice='\0'; //NULL
}
return 1;
}
ok iv got it opening the file now, but im still having problems with getting it to read, i now get these compile errors, my code is below also thankyou for your help, it is much appreciated! :-
gameserverpro12.cpp: In function `int main()':
gameserverpro12.cpp:81: error: `infile' undeclared (first use this function)
gameserverpro12.cpp:81: error: (Each undeclared identifier is reported only
once for each function it appears in.)
gameserverpro12.cpp:81: error: `whole' undeclared (first use this function)
gameserverpro12.cpp:83: error: `p2' undeclared (first use this function)
gameserverpro12.cpp:94: error: jump to case label
gameserverpro12.cpp:86: error: crosses initialization of `std::string p2'
gameserverpro12.cpp:86: error: crosses initialization of `std::string p1'
gameserverpro12.cpp:86: error: crosses initialization of `std::string whole'
gameserverpro12.cpp:80: error: crosses initialization of `int i'
gameserverpro12.cpp:98: error: jump to case label
gameserverpro12.cpp:86: error: crosses initialization of `std::string p2'
gameserverpro12.cpp:86: error: crosses initialization of `std::string p1'
gameserverpro12.cpp:86: error: crosses initialization of `std::string whole'
gameserverpro12.cpp:80: error: crosses initialization of `int i'
gameserverpro12.cpp:106: error: syntax error at end of input
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
char s1[30],s2[40],s3[256]; // set char size to 30 and 256 for input
ofstream outfile("games.db", ios::app);
// 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 start file: ";
cin.get();
cin.getline (s1, 30); // Input goes in$
outfile << s1 << ":"; // add : after eachpath
cout << "\n\nPlease enter game name: ";
cin.getline (s2, 40);
outfile << s2 << "\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 (s3, 256); // input goes in
string cmd = string("pico ") + string(s2);
system(cmd.data()); // open up pico to edit config
}
// Here we would check 'choice' and probably move to another functi$
cout<<"\n\n";
break;
case '3': cout<<"Run Game";
if(choice=='3')
{
char buf[1024];
FILE *fp;
fp = fopen("games.db","r"); // open games.db for reading
if (fp == NULL)
{
fprintf(stderr, "Error opening file...");
exit(1);
}
int i=0;
while(getline(infile,whole)){
i++;
p2=whole.substr(whole.find(':')+1,whole.length());
cout << i << ". " << p2 << endl;
}
string whole,p1,p2;
//initialize whole
p1=whole.substr(0,whole.find(':'));
p2=whole.substr(whole.find(':')+1,whole.length());
// 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;
// end functions
}
choice='\0'; //NULL
}
return 1;
}
First off, where do you use buf[]? And you shouldn't use fopen if you're programming with streams. You should use an ifstream construct. And you need to declare all your variables at the beginning of functions. (Well, you should. And never declare them in a case statement.)
well iv now changed the code and getting these errors, code below also :-
g++ gameserverpro12.cpp
gameserverpro12.cpp: In function `int main()':
gameserverpro12.cpp:78: error: `infile' undeclared (first use this function)
gameserverpro12.cpp:78: error: (Each undeclared identifier is reported only
once for each function it appears in.)
gameserverpro12.cpp:78: error: syntax error before `}' token
gameserverpro12.cpp:92: error: case label `'4'' not within a switch statement
gameserverpro12.cpp:96: error: `default' label not within a switch statement
gameserverpro12.cpp: At global scope:
gameserverpro12.cpp:101: error: ISO C++ forbids declaration of `choice' with no
type
gameserverpro12.cpp:102: error: syntax error before `}' token
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int i=0;
char s1[30],s2[40],s3[256]; // set char size to 30 and 256 for input
ofstream outfile("games.db", ios::app);
string whole,p1,p2;
// 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 start file: ";
cin.get();
cin.getline (s1, 30); // Input goes in$
outfile << s1 << ":"; // add : after eachpath
cout << "\n\nPlease enter game name: ";
cin.getline (s2, 40);
outfile << s2 << "\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 (s3, 256); // input goes in
string cmd = string("pico ") + string(s2);
system(cmd.data()); // open up pico to edit config
}
// Here we would check 'choice' and probably move to another functi$
cout<<"\n\n";
break;
case '3': cout<<"Run Game";
if(choice=='3')
{
ifstream infile;
infile.open ("games.db", ios::app);
}
while(getline(infile,whole))}
i++;
p2=whole.substr(whole.find(':')+1,whole.length());
cout << i << ". " << p2 << endl;
}
//initialize whole
p1=whole.substr(0,whole.find(':'));
p2=whole.substr(whole.find(':')+1,whole.length());
// 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;
// end functions
}
choice='\0'; //NULL
}
return 1;
}
ok iv done as you suggested but im now getting these errors :-
g++ gameserverpro12.cpp
gameserverpro12.cpp: In function `int main()':
gameserverpro12.cpp:76: error: syntax error before `}' token
gameserverpro12.cpp:86: error: case label `'4'' not within a switch statement
gameserverpro12.cpp:90: error: `default' label not within a switch statement
gameserverpro12.cpp: At global scope:
gameserverpro12.cpp:95: error: ISO C++ forbids declaration of `choice' with no
type
gameserverpro12.cpp:96: error: syntax error before `}' token
My code :-
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int i=0;
char s1[30],s2[40],s3[256]; // set char size to 30 and 256 for input
ofstream outfile("games.db", ios::app);
string whole,p1,p2;
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 start file: ";
cin.get();
cin.getline (s1, 30); // Input goes in$
outfile << s1 << ":"; // add : after eachpath
cout << "\n\nPlease enter game name: ";
cin.getline (s2, 40);
outfile << s2 << "\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 (s3, 256); // input goes in
string cmd = string("pico ") + string(s2);
system(cmd.data()); // open up pico to edit config
}
// Here we would check 'choice' and probably move to another functi$
cout<<"\n\n";
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;
}
// 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;
// end functions
}
choice='\0'; //NULL
}
return 1;
}
Thankyou for your help it really is appreciated, ok i change the brace the other way and it compiled, but now when you enter the run game menu it looks like this :-
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.