1 Replies - 1375 Views - Last Post: 18 March 2013 - 01:43 AM Rate Topic: -----

#1 needhelpest  Icon User is offline

  • New D.I.C Head

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

Multiple questions and issues around databases in C++

Posted 17 March 2013 - 06:33 PM

I have been messing with this program for past 8 hours and I do not know where to go further. I am very sorry that my code is in a foreign language, I tried to comment it in English to my best efforts.
I managed to debug it at last.but I have few problems.
1)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
2) How to use characters rather than numbers to make a menu in the program?
3)Am I really stupid? Answer honestly, please.
I will take a break now and get some sleep, will go through the replies in the morning, I hope it isnt aganist the rules.



  #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"); 
            int 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; 
               
    switch (valik)   {                   /*choice*/
               
              case 1:
                      viisjuurde(); 
                      break; 
              case 2:
                     viismaha(); 
                     break; 
              case 3: 
              
                     sisendvoivaljumine(); 
                     break; 
                     }  
                     }
    void viisjuurde() 
    { 
          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 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;
    
          
         switch (valik2) 
         { 
               
            
            case 1:
                     sisendvoivaljumine(); 
                     break; 
                     sisend(); 
                     break; 
            case 2:
                     valjumine(); 
                     break; 
            case 3: 
                     kogukuvand(); 
                     break; 
                     
                              
            
    } 
    }
    
      
     



Is This A Good Question/Topic? 0
  • +

Replies To: Multiple questions and issues around databases in C++

#2 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1345
  • View blog
  • Posts: 4,635
  • Joined: 19-February 09

Re: Multiple questions and issues around databases in C++

Posted 18 March 2013 - 01:43 AM

There should be no need to call the main function.

068	        main(); 




I think you could use seekg to move to the start of the file.

  ifstream filmike("andmebaas.txt"); 
  string line;

  // print 5 lines
  for(int i = 0; i < 5; i++) 
  {
    getline(filmike, line);
    cout << line << endl;
  }
  
  filmike.seekg (0, filmike.beg);

  // print 5 lines
  for(int i = 0; i < 5; i++) 
  {
    getline(filmike, line);
    cout << line << endl;
  }

  ...

  filmike.close();




getline
seekg

You could load all the data into a vector (so that it is in memory) making it is easier to process.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1