close

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<fstream>
#include<cctype>
#include<string> 
using namespace std;
class telwork
{
    public:
        string name;
        string tel;


  friend void addone(fstream &file)
  {
    char ch;
    string name;
    char tel[10];
    
    ch=cin.get();
    cout<<"ch = "<<(int)ch<<endl;
    
    file.seekp(0,ios::end);

    cout<<"enter name : ";
    getline(cin,name);
    
    cout<<"enter tel : ";
    cin.get(tel,10);

    
    file<<name<<',' <<tel<<endl;    
  }

  friend void lookup(fstream &file)
  {
    telwork who;
    char ch;
    ch=cin.get();
    
    string name;
    string tel;
    getline(cin,who.name);
    
    file.seekg(0,ios::beg); 
    
    while(!file.eof())
    {
        
        getline(file,name,',');
        getline(file,tel,'\n');
        
         bool b=name==who.name;
         cout<<"b = "<<b<<endl;
         if(name==who.name)
        {
            cout<<name<<"'s tel is "<<who.tel<<endl;
        }
        cout<<"    name = "<<    name<<endl;
        cout<<"who.name = "<<who.name<<endl;
        cout<<"    tel  = "<<tel <<endl;
       system("pause");
    }
  }
};    

int main(void)
{
    cout<<endl<<endl<<endl;
    int choice;
    fstream file("telbook.txt",ios::in|ios::out);
    if(!file)
    cout<<"file open fail"<<endl;
    else
      
    {
       do
       {
          cout<<"select function :(1)addone.  (2)lookup.  (3)end.  :";
          
          cin>>choice;
          if(choice==1)
             addone(file);
          else if(choice==2)  
            lookup(file);
          else if(choice>3)
            cout<<"num is too large , try again"<<endl;
       }while(choice!=3);
      cout<<"goodbye"<<endl;
    }
system("pause");
return 0;
}

arrow
arrow
    全站熱搜

    minsin 發表在 痞客邦 留言(0) 人氣()