2012-05-03 49 views
1

這是我的代碼。這是作業,但我已經完成了這一切。但搜索功能不起作用。我知道我在做錯誤的事情。用戶必須輸入名稱進行搜索,並顯示整個條目。有一個在空搜索功能它說的錯誤「錯誤C2120:‘無效’非法使用所有類型的」搜索功能不起作用

  // Awholenew world.cpp : Defines the entry point for the console application. 
      // 

      #include "stdafx.h" 
      #include<process.h> 
      #include<iomanip> 
      #include<stdio.h> 
      #include<string.h> 
      #include<fstream> 
      #include<sstream> 
      #include<iostream> 
      #include<conio.h> 
      #include<stdlib.h> 

      using namespace std; 

      struct main 
      { 
       char FName[20]; 
       char Last[20]; 
       char Address[30]; 
       long Phone; 
       int ID; 
       long CNIC; 
      }obj; 

      class Main 
      { 

      public: 
       virtual void init() 
       { 
        cin.getline(obj.FName,20); 
        cout << "Enter Name: "; 
        cin.getline(obj.Last,20); 

        cout << "Enter Address: "; 
        cin.getline(obj.Address, 30); 
        cout << "Enter Phone: "; 
        cin>>obj.Phone; 

       } 
       void view() 
       { 
        cout << "Name: " << obj.Last<< endl; 
        cout << "Address: " << obj.Address << endl; 
        cout << "Phone: " << obj.Phone << endl; 
        cout << "CNIC: " << obj.CNIC << endl; 
        cout << "ID: " << obj.ID << endl; 

       } 




       virtual void search() 
       { 
        char choice4; 
        char Target[20]; 
        int Found=0; 

        fstream fin; 
        if (fin.open("Main.txt", ios::in| ios::out) == NULL) 
         cout<<"File is empty" << endl; 
        else 
         cout<<"Enter Name: " << endl; 
        cin.getline(Target, 20); 

        while(!fin.eof() && Found ==0) 
        { 
         fin<< endl << obj.Last << endl <<obj.Address <<endl <<obj.Phone << endl << obj.CNIC << endl << obj.ID; 
         if (strcmp(Target, obj.Last) == 0) 
          Found =1; 
        } 
        if(Found) 
        { 
         Main::view(); 

        } 
        else if (!Found) 
         printf("**There is no such Entry**\n"); 
        fin.close(); 

       } 



       void display() 
       { 

        char BUFFER[100]; 
        ifstream fin("Main.txt"); 


         while (!fin.eof()) 
         { 
         fin.getline(BUFFER, 100); 
         cout << BUFFER << endl; 

         } 
       } 

      }; 

      class Teacher : public Main 
      { 
      public: 

      void tinit() 
      { 
       ofstream fin("Main.txt", ios::app); 
       Main::init(); 
       cout << "Enter CNIC of Teacher" << endl; 
       cin>>obj.CNIC; 
       fin<< endl << obj.Last << endl << obj.Address << endl << obj.Phone << endl << "Teacher CNIC: " << obj.CNIC << endl; 

      } 

      }; 

      class Student : public Main 
      { 
      public: 
       void sinit() 
       { 
       ofstream fin("Main.txt", ios::app); 
       Main::init(); 
       cout << "Enter ID of Student" << endl; 
       cin>>obj.Phone; 
       fin<< endl << obj.Last <<endl << obj.Address << endl << obj.Phone << endl << "Student ID" << obj.ID << endl; 
       } 

      }; 

      class Employee : public Main 
      { 
      public: 
       void einit() 
       { 

       ofstream fin("Main.txt", ios::app); 
       Main::init(); 
       cout << "Enter Employee CNIC" << endl; 
       cin>>obj.CNIC; 
       fin << endl << obj.Last <<endl << obj.Address << endl << obj.Phone << endl << "Employee CNIC: " << obj.CNIC << endl; 
       } 

      }; 
      int _tmain(int argc, _TCHAR* argv[]) 
      { 
       Main* myarr[100]; 
       Teacher* tearr[100]; 
       Student* starr[100]; 
       Employee* emarr[100]; 
       int var=0; 
       int choice; 
       char input; 

       start: 
       printf("===============MAIN MENU==============="); 
       printf("\n[1]Insert\n[2]Display All\n[3]Search\n[4]Exit\n"); 
       printf("======================================="); 
       printf("\n\nEnter Your Choice: "); 

       cin >> choice; 

       switch(choice) 
       { 

       case 1: 
      label: 
        cout<< "Enter Choice s/t/e" << endl; 
        cin >> input; 


       if (input == 't') 
        tearr[var]->tinit(); 
       if (input == 's') 
        starr[var]->sinit(); 
       if (input == 'e') 
        emarr[var]->einit(); 

       cout << " Enter another (y/n)? "; 
       cin >> input; 

       if (input == 'y') 
        goto label; 
       else 
        goto start; 
        break; 
       case 2: 
         myarr[var]->display(); 
         break; 
       case 3: 
        myarr[var]->search(); 
        break; 
       case 4: 
        cout << "Are you sure? y/n" << endl; 
        char in; 
        cin>>in; 
        if (in=='y') 
         getch(); 
        else goto start; 
        break; 
       default: 
        return 0; 
        break; 

       return 0; 
       } 
      } 

回答

0

的一個問題,我可以看到的是:

fstream fin; 
if (fin.open("Main.txt", ios::in| ios::out) == NULL) 

的功能的fstream ::打開聲明爲:

void open (const char * filename, 
      ios_base:openmode mode = ios_base::in | ios_base::out); 

,你可以看到它的返回類型爲void,所以你不能在使用它如果表達。

+0

所以我應該刪除if。所以我用什麼來代替它 – Moomal

0

我注意到的最顯着的一點是,它看起來像類型的結構「主」存儲在obj變量不在任何特定的類實例中。這意味着Main,Employee,Student或Teacher類型的每個對象都只能寫入其自己範圍之外的單個對象。所有員工,教師,學生,'主要'將分享您最後編寫的任何FName,Last,Address值。

你可能想要的是主要的每個實例有它自己的FName參數,最後,地址等..

 class Main 
     { 
      struct main 
      { 
      char FName[20]; 
      char Last[20]; 
      char Address[30]; 
      long Phone; 
      int ID; 
      long CNIC; 
      }obj; 

     public: 
      virtual void init() 
      { 
      ... rest of code here 
0

請減少您的帖子到主要問題。如果您只列出一個「代碼」塊中的所有內容。沒有人能夠理解你的所作所爲。

1:不使用 「轉到」(!!!!!)

2:你要高度重視爲真的標誌

3使用的,而不是整數BOOL:不使用主/主用於結構或類名。它實際上是爲mainmathod(在某些系統上)保留的,並且讓一個類和一個結構具有相同的名稱(忽略大小寫)是令人困惑的。

4:您正在訪問函數Main :: view();在您的搜索方法。但在哪個對象?通常你應該加載數據到一個對象並通過this-> view()來訪問它。

5:整個概念出現連線。你爲什麼要使用文本文件和多態類層次結構?該文本文件轉儲層次結構。也許你應該重新考慮你的班級概念。