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 09-11-2005, 12:02 PM   #16
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128

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.
 
Old 09-11-2005, 02:29 PM   #17
twirl
Member
 
Registered: Aug 2005
Posts: 168

Original Poster
Rep: Reputation: 15
Ok thankyou :-) im trying to find out how todo it as my book dont cover things like reading files line by line and seperating the lines :-(
 
Old 09-11-2005, 02:42 PM   #18
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
The getline function that you already use in places reads in a line.

To get the parts of the line, use something like:
Code:
string whole,p1,p2;
//initialize whole
p1=whole.substr(0,whole.find(':'));
p2=whole.substr(whole.find(':')+1,whole.length());
p1 contains everything before the first :, and p2 everything after.
 
Old 09-11-2005, 03:03 PM   #19
twirl
Member
 
Registered: Aug 2005
Posts: 168

Original Poster
Rep: Reputation: 15
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;
}
 
Old 09-11-2005, 03:08 PM   #20
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Read the lines in from the file into whole (looping) and then print out p2 each time with the proper number in front. Something like:
Code:
int i=0;
while(getline(infile,whole)){
    i++;
    p2=whole.substr(whole.find(':')+1,whole.length());  
    cout << i << ". " << p2 << endl;
}
Obviously, you'll need to open infile for reading first.
 
Old 09-11-2005, 03:34 PM   #21
twirl
Member
 
Registered: Aug 2005
Posts: 168

Original Poster
Rep: Reputation: 15
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;
}
 
Old 09-11-2005, 03:44 PM   #22
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
These lines of code are certainly in the wrong place. They were only intended as an example earlier:
Code:
            //initialize whole
             p1=whole.substr(0,whole.find(':'));
             p2=whole.substr(whole.find(':')+1,whole.length());
Also, nowhere do you open your game database for reading.
 
Old 09-11-2005, 06:49 PM   #23
twirl
Member
 
Registered: Aug 2005
Posts: 168

Original Poster
Rep: Reputation: 15
Hi,

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;
}
 
Old 09-11-2005, 07:19 PM   #24
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
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.)
 
Old 09-11-2005, 08:20 PM   #25
twirl
Member
 
Registered: Aug 2005
Posts: 168

Original Poster
Rep: Reputation: 15
Hi,

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;
}
 
Old 09-11-2005, 08:29 PM   #26
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
This is my quick bit of a fix here:
Code:
   case '3': cout<<"Run Game\n";
             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;
            }
Add " ifstream infile;" to the beginning of your main().
 
Old 09-11-2005, 08:35 PM   #27
twirl
Member
 
Registered: Aug 2005
Posts: 168

Original Poster
Rep: Reputation: 15
Hi,

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;
}
 
Old 09-11-2005, 08:55 PM   #28
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
OOps... at the beginning of the while loop, there is a reversed bracket... it should be {, not }.
 
Old 09-11-2005, 09:13 PM   #29
twirl
Member
 
Registered: Aug 2005
Posts: 168

Original Poster
Rep: Reputation: 15
Hi,

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 :-

Run Game1.
2. bf1942

You can not select anything to run the game.
 
Old 09-11-2005, 09:23 PM   #30
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Is there a blank line at the beginning of your games.db file? Also, have you added the code for input?
 
  


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
Why aint it reading file correctly? twirl Programming 11 09-13-2005 03:30 PM
How do i open a file user specifys? twirl Programming 1 09-06-2005 10:22 PM
firefox dos not open when run under user (opens only in root) saso Mandriva 8 06-15-2005 10:07 AM
trying to download PDF file -- it always OPENS 1kyle Linux - Software 1 07-24-2004 05:34 AM
what opens a .bz file? Chooco Linux - Newbie 15 06-18-2002 11:48 AM

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

All times are GMT -5. The time now is 03:38 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