| 
 
     
- 威望
 - 0  
 - 貢獻
 - 46  
 - 參與
 - 0  
 - GJ
 - 25  
 - 註冊時間
 - 2004-7-15 
 
  | 
小弟最近無聊寫了一個寫入輸出文字檔的程式 
發現如果寫入後不關閉程式,直接在讀取會讀取資料兩次 
在寫入讀取會變成三次~~但資料並沒有出錯.... 
雖然想過用指標好像可以解決~~但有沒有其他方法阿? 
 
#include <iostream> 
#include <string> 
#include <fstream> 
#include <vector> 
using namespace std; 
class person  
{private : 
string name; 
double hight; 
double weight; 
bool sex; 
public : 
person(string n,double h,double w,bool s); 
string getName(){return name;}; 
double getHight(){return hight;}; 
double getWeight(){return weight;}; 
bool getSex(){return sex;}; 
void setName(string n){name=n;}; 
void setHight(double h){hight=h;}; 
void setWeight(double w){weight=w;}; 
void setSex(bool s){sex=s;}; 
person (){};//為何一定要這行,否則第42行無法執行  
void output();}; 
void person::output() 
{ 
        cout << name << "        " << hight << "        " << weight << "        "<< sex << "        " <<endl; 
        if (sex) 
                cout << "boy" << endl; 
        else 
                cout << "girl" << endl; 
} 
person::person(string n,double h,double w,bool s) 
{        name=n; 
        hight=h; 
        weight=w; 
        sex=s;   }   
int main() 
{ 
    vector<person> list; 
        person temp;     
        string n; 
        double h,w; 
        bool s; 
    int z,i; 
 do//程式起點 
    { 
    cout<<"1.新增資料 
2.修改資料 
3.刪除資料 
4.將資料存入檔案 
5.將資料由檔案中讀出 
6.結束程式 
請輸入選項:"<<endl; 
    cin>> z; 
    switch(z) 
{ 
    case 1://如何將功能1的資料傳給功能2.3.4,並資料加入功能5中讀出的資料?  
{ 
        char sel; 
        do 
        { 
                cout << "請輸入姓名、身高、體重及性別:"; 
                cin >> n >> h >> w >> s; 
                temp = person(n,h,w,s); 
                list.push_back(temp); 
                cout << "是否繼續輸入:"; 
                cin >> sel; 
        }while (sel=='y');         
        for (i=0;i<list.size();i++) 
        { 
                cout << i << "        "; 
                list.output();//為何輸出時會出現布林變數1,再出現BOY?  
        } 
} 
break; 
    case 2://如何將功能1.5的資料修改,並傳給功能3  
{ 
    person file(n,h,w,s); 
        int l; 
        char sel; 
    for (i=0;i<list.size();i++) 
        { 
                cout << i << "        "; 
                list.output(); 
        } 
     
        cout << "請問要修改第幾筆資料:"; 
        cin >> i; 
    do 
{                    
    cout << "1.修改名稱 
"; 
        cout << "2.修改身高 
"; 
        cout << "3.修改體重 
"; 
        cout << "4.修改性別 
"; 
        cin >> l; 
        switch (l) 
{ 
        case 1: 
                cout << "目前名稱是:" <<  list.getName(); 
                cout << "修改成:"; 
                cin >> n; 
            list.setName(n); 
                break; 
        case 2: 
                cout << "目前身高是:" <<  list.getHight(); 
                cout << "修改成:"; 
                cin >> h; 
            list.setHight(h); 
                break; 
        case 3: 
                cout << "目前體重是:" <<  list.getWeight(); 
                cout << "修改成:"; 
                cin >> w; 
            list.setWeight(w); 
                break; 
        case 4: 
         cout<<"目前性別是:" <<  list.getSex(); 
         cout << "修改成:"; 
         cin >>s; 
         list.setSex(s); 
         break; 
 
        }         
        list.output(); 
        cout<<"是否繼續執行?"; 
        cin>> sel; 
}while (sel=='y'); 
} 
break; 
    case 3://如何完成功能1.2.3.5的應用  
{ 
    for (i=0;i<list.size();i++) 
        { 
                cout << i << "        "; 
                list.output(); 
        } 
    cout << "請問要刪除第幾筆資料:"; 
        cin >> i; 
        list.erase(list.begin()+i); 
        for (i=0;i<list.size();i++) 
        { 
                cout << i << "        "; 
                list.output(); 
        } 
}  
break; 
    case 4://如何將功能1.2.3.5的資料移入功能4,再將它存取 ;將全域變數寫入資料會無限寫入最後一筆資料  
{ 
    char file[10]; 
    cout <<"請輸入檔案名稱:"<<endl; 
    cin >> file; 
    ofstream fout(file); 
 for (i=0;i<list.size();i++) 
        { 
         fout << list.getName() <<"        "<<list.getHight() <<"        "<<list.getWeight() <<"        "<<list.getSex() <<"        "<<endl;                 
} 
} 
break; 
    case 5://如何將功能5的資料傳給功能1.2.3?  
{//功能4運行完再讀取相同檔案資料會重複讀取  
    char file[10]; 
    cout <<"請輸入檔案名稱:"<<endl; 
    cin >> file; 
    ifstream fin(file); 
    if (fin.fail()) 
    {cout << "file open failed 
";} 
        while(fin>>n) 
        { 
                 fin >> h >> w >> s; 
                 list.push_back(person(n,h,w,s)); 
     } 
     for (i=0;i<list.size();i++) 
         list.output(); 
}  
break; 
    case 6: 
         exit (0); 
break; 
} 
    }while(1);//如果功能1的性別錯誤如何避免無限迴圈?  
    system("pause");  
    return 0; 
} |   
 
 
 
 |