2 Replies - 1343 Views - Last Post: 23 March 2013 - 08:02 PM Rate Topic: -----

#1 needhelpest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 12-January 13

Enhanced database program,still with issues.

Posted 18 March 2013 - 01:30 AM

I finished my yet another tour of debugging and now I am at loss once more.


The problem is when I open the program and press any buttons, it always takes me to the whole movie database display, even though it is in the 2nd menu.

I am sorry with bothering you with silly homework questions, and I even have one more. How to display first 5 movies(5 lines) from the file out to program.) How to print the first 5 lines(the first 5 lines from the txt fail) and then the previous 5 again?

My rather pathetic try is listed below.Example: At first you have Memento , Godfather. You print the next five Godfather II , Shawshank Redemption, Leon, A Bittersweet Life,Se7en.Now previous 5:Memento, Godfather,Godfather II, Shawshank Redemption, Leon.

  #include <iostream> 
    #include <string> 
    #include <fstream> 
     
    using namespace std; 
     
    void sisend(); /*input*/
    string valik(); /*choice*/
    void kogukuvand(); /* the whole display of the database */
    void valjumine();    /* program quit*/ 
    void sisendvoivaljumine(); /*quiting with output to file or not*/
    void viisjuurde(); /* input further 5 lines from the file */
    void viismaha();    /* input previous 5 lines from the file*/
    
    
     
      
      
    int main() 
    { 
         
          
          
           
             system ("title Filmide andmebaas"); /*Movie database*/
             system ("cls"); 
            char valik; 
             cout << "Vajuta klahv 'j',kui tahad naha jargmist 5 kirjet."<< endl; /* j to get extra 5*/
             cout << "Vajuta klahv 'e',kui tahad naha eelmist 5 kirjet."<< endl;  /* e for previous 5 lines */
             cout << "Vajuta klahv 'v',kui tahad lopetada programmi taitumist."<< endl; /* v for the output */
         cin >> valik; 
               
                     
               
              if(valik=='j')
                     {
                      viisjuurde(); 
                       
                     } 
                   
              else  if(valik=='e')
                     {
                      viisjuurde(); 
                       
                     } 
              else  if(valik=='v')
                    
                     sisendvoivaljumine(); 
                      
                     
    }
    void viisjuurde() 
    { 
          ifstream filmike("andmebaas.txt"); 
          int n,  
          sum = 5; 
          string film; 
          string vaadatud; 
         int aasta; 
         long int hinne; 
          system ("CLS"); 
          cout << "Kogu filmide andmebaas"<<endl; 
         cout << "Film" << "  " << "Aasta" << "  " <<"Hinne"<< "  " << "Vaadatud" << endl; 
            cout << "------------------------------" << endl; 
             while (filmike>> film >> aasta >> hinne >> vaadatud)  
      {  
      sum += n;
            cout << film << "  " << aasta << "  " << hinne << "  " << vaadatud << endl;  
      } {
          system ("pause");  
          cin.get(); 
          main(); 
           
    }  
    }
      void viismaha() 
    { 
          ifstream filmike("andmebaas.txt"); 
          int n, sum = -5;
      while (filmike >> n) {
          sum += n;
      }
          string film; 
          string vaadatud; 
         int aasta; 
         long int hinne; 
          system ("CLS"); 
          cout << "Kogu filmide andmebaas"<<endl; 
         cout << "Film" << "  " << "Aasta" << "  " <<"Hinne"<< "  " << "Vaadatud" << endl; 
            cout << "------------------------------" << endl; 
             while (filmike>> film >> aasta >> hinne >> vaadatud)  
      { 
            cout << film << "  " << aasta << "  " << hinne << "  " << vaadatud << endl;  
      } 
          system ("pause");  
          cin.get(); 
          main(); 
           
      } 
    void sisend() /* user input */
    { 
         string nimi; 
         int aasta; 
         long int hinne; 
         string vaadatud; 
         ofstream lisafilm("andmebaas.txt", ios::app); 
         cout <<"Sisestage filmi nimi." << endl;  /*Enter film's name */
         cin >> nimi; 
         string film;
          
         cout << "Sisestage filmi ilmumisaasta." << endl;  /* Enter film's year */
         cin >> aasta;     
          
         cout << "Sisestage filmi hinne." << endl;  /* Enter film's rating */
         cin >> hinne; 
           
         cout << "Kas olete filmi juba vaadanud?" << endl;  /* Has the user seen the movie already*/
         cout << "Vastake 'Jah' voi 'Ei'" << endl; 
         cin >> vaadatud; 
    cout << film << "  " << aasta << "  " << hinne << "  " << vaadatud << endl;  
     
    lisafilm<< nimi << "  " << aasta << "  " << hinne<< "  " << vaadatud << endl;  
    lisafilm.close();  
    main();
    
     cout << "Aitah kasutamast andmebaasi." << endl; 
     system("pause"); 
     cin.get (); 
    } 
     
     
    
     void valjumine() /*quiting without output */
    {  
     system("CLS"); 
     cout << "Aitah kasutamast andmebaasi." << endl; 
     system("pause"); 
     cin.get (); 
      } 
    void kogukuvand()  /* the whole movie database*/
    { 
          ifstream filmike("andmebaas.txt"); 
          string film; 
          string vaadatud; 
         int aasta; 
         long int hinne; 
          system ("CLS"); 
          cout << "Kogu filmide andmebaas"<<endl; 
         cout << "Film" << "  " << "Aasta" << "  " <<"Hinne"<< "  " << "Vaadatud" << endl; 
            cout << "------------------------------" << endl; 
             while (filmike>> film >> aasta >> hinne >> vaadatud)  
      { 
            cout << film << "  " << aasta << "  " << hinne << "  " << vaadatud << endl;  
      } 
          system ("pause");  
          cin.get(); 
          main(); 
           
      } 
    void sisendvoivaljumine() 
    { 
         int valik2; 
         system ("CLS"); 
         cout << "Vajuta klahv 'j',kui tahad salvestada kirje ja v2ljuda programmist." << endl;   /* output new movie to file and quit*/
         cout << "Vajuta klahv 'e',kui ei taha salvestada kirje ja v2ljuda programmist." << endl;  /*just quit, without any output to file*/
         cout << "Vajuta klahv 'k',kui tahad naha kogu filmide andmebaasi." << endl;  /* display the whole movie database*/
         cin >> valik2;
    
          
         if(valik2=='j')
                     {
                      sisend(); 
                       
                     } 
                   
              else  if(valik2=='e')
                     {
                      valjumine(); 
                       
                     } 
              else  if(valik2=='k')
                    
                     kogukuvand(); 
                     
                              
            
    
    }
           

This post has been edited by needhelpest: 18 March 2013 - 01:44 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Enhanced database program,still with issues.

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,423
  • Joined: 25-December 09

Re: Enhanced database program,still with issues.

Posted 18 March 2013 - 08:01 AM

One of the first things I see wrong is the calling of main(). In a C++ program main() can't be called by any function including main(). You need to rework your code to remove these calls. It also looks like you don't truly understand C/C++ function return semantics. When your function ends it returns to the calling function, most of your functions are being called from main() so they will automatically return back to that function. You probably need to use a loop in main() so the program can ask the next question.

Next as to the actual problem I suggest you run the program thru your debugger, set a breakpoint and single step thru the program watching the variables and program flow. You should be able to discern where your program is malfunctioning by this close examination.

If you are unable to discern the problem come back with a little more information. You need to tell us exactly what you inputted into your program, exactly what your program produced and what you expected your program to produce. Since this program is written for a language other than English, which is usually not a problem, it will be extremely helpful if you point out, using line numbers the locations you are talking about. For example what line number does "Menu 2" start?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Enhanced database program,still with issues.

Posted 23 March 2013 - 08:02 PM

Since you are using C++, if your problem is just to print the first 5 lines in a fille, then to print them again, as you have stated above ... that is really very simple to do in C++

There are several ways one might do this.

A very simple way could be like this:

const string fileName = "myData.txt";
ifstream fin( fileName.c_str() );
if( fin )
{ 
   int i = 0:
   string lines[ 5 ]; // get an array to hold 5 C++ strings
   while( i < 5 && getline( fin, lines[ i ] ) )
   {
      cout << lines[ i ] << endl; // first pass of 5 lines ...
      ++ i ;
   }
   fin.close();
   for( i = 0; i < 5; ++ i ) lines[ i ] << endl; // 2nd pass printing same 5 lines
}
else cout << "Error opening file " << fileName << end;



If you want to rewind the file back to the beginning you would not need to save the lines in an array of C++ strings
You could just read the file forward ... line by line, for 5 lines ( while fin is good ) and print each line on both passes.

This post has been edited by David W: 23 March 2013 - 08:07 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1