2013-03-18 44 views
0

過去8小時我一直在搞這個程序,我不知道該往哪裏走。我非常抱歉我的代碼是用外語寫的,我試着用英語對它進行評論以盡我所能。 我設法調試它,但我有幾個問題。 1)如何打印前5行(txt失敗的前5行),然後是前5個?我的可憐嘗試列在下面。例如: 起初你有Memento,教父。你打印下五個教父II,肖申克救贖,萊昂,一個苦樂參半的人生,Se7en.Now前5:紀念品,教父,教父II,肖申克救贖,萊昂 2)如何使用字符而不是數字來製作菜單該程序? 3)我真的很愚蠢嗎?請誠實地回答。 我現在休息一下,睡一會兒,早上會通過答覆,希望它不會違反規則。C++中的數據庫問題

#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; 



} 
} 
+0

作業標籤已折舊。刪除。 – Casey 2013-03-18 01:22:51

+2

@Casey,也許你的意思是*棄用*? – Shoe 2013-03-18 03:20:34

+0

@Jeecy Yeah,那'我'是偷偷摸摸的。 :P – Casey 2013-03-18 11:22:19

回答

1

一些提示:

  1. 縮進,並根據共同約定格式化你的代碼。它使您閱讀和調試(以及其他所有人)變得更加容易。

  2. 您正在遞歸調用main()。不要那樣做。改爲使用菜單開關周圍的循環。

  3. 從文件中讀取行的一種很好的故障保護方法是使用getline()。與istringstream相結合,從線讀取各個值:

    ifstream infile("somefile.txt"); 
    string line; 
    while (getline(infile, line)) // read infile until eof 
    { 
        istringstream iss(line); // create stream from line data 
        string a, b, c, d; 
    
        iss >> a >> b >> c >> d; // read just as from cin 
        cout << a << "," << b << "," << c << "," << d << endl; 
    } 
    
  4. 如果你讀成整數,你需要驗證讀取成功(除非您確信該文件在點包含的數字你在哪裏讀):

    int n; 
    if (iss >> n) 
    { 
        // read OK 
    } 
    else 
    { 
        // read failed 
        infile.clear(); 
        string junk; 
        infile >> junk; 
        cout << "ERROR: bad data was:" << junk << endl; 
    } 
    
  5. 要使用菜單字符,你可以使用char數據類型。這是一個好主意,因爲字符輸入不會失敗:

    char choice; 
    cin >> choice; 
    switch (choice) 
    { 
        case '1': 
        { 
         // do something 
         break; 
        } 
        case 'A': 
        { 
         // do something 
         break; 
        } 
    } 
    
  6. 當心遞歸調用自己,當你選擇該選項。由於菜單可以一遍又一遍地重複選擇,無限地,您最好使用循環重複菜單。

想要閱讀上述主題。檢查http://www.cplusplus.com。 當您調試時,確保您確切知道程序在每個點執行的是哪一行。添加清除調試輸出來幫助你。