2011-04-16 14 views
0

我決定用C++編寫電話簿,並決定從文件中輸入名稱,地址和號碼。 所以我做了一個名爲contact的類,並聲明瞭公共變量名稱,地址和數字。 使用構造我初始化這些命名= 「NONAME」(串),數量= 0(INT),地址= 「無地址」(串) 現在我的主體去爲:無法從包含字符串變量的文件獲取輸入

int main(){ 

contact *d; 

d= new contact[200]; 

string name,add; 

int choice,modchoice;//Variable for switch statement 

int phno,phno1; 

int i=0; 
int initsize=0, i1=0;//i is declared as a static int variable 

bool flag=false,flag_no_blank=false; 


//TAKE DATA FROM FILES..... 
//We create 3 files names, phone numbers, Address and then abstract the data from these files first! 

fstream f1; 
fstream f2; 
fstream f3; 

string file_input_name; 
string file_input_address; 

int file_input_number; 


f1.open("./names"); 

while(f1>>file_input_name){ 

    d[i].name=file_input_name; 

    i++; 

} 
initsize=i; 


f2.open("./numbers"); 

while(f2>>file_input_name){ 

    d[i1].phonenumber=file_input_number; 
    i1++; 

} 


f3.open("./address"); 

while(f3>>file_input_address){ 

    d[i1].address=file_input_address; 

    i1++; 

} 

現在當我後來通過名稱搜索特定條目時,名稱顯示正確,但電話號碼作爲垃圾值返回並作爲「Noaddress」地址返​​回 我不明白爲什麼會發生這種情況... 如果您想查看整個代碼,請讓我知道....

這是我如何搜索一個特定的條目,返回名稱如果匹配,但返回垃圾電話號碼....

cout<<"\nEnter the name";//Here it is assumed that no two contacts can have same contact number or address but may have the same name. 
cin>>name; 

int k=0,val; 
cout<<"\n\nSearching.........\n\n"; 

for(int j=0;j<=i;j++){ 
    if(d[j].name==name){ 
    k++;    
    cout<<k 
     <<".\t" 
     <<d[j].name 
     <<"\t"<<d[j].phonenumber 
     <<"\t"<<d[j].address 
     <<"\n\n"; 
    val=j;     
    } 
} 

在此先感謝

回答

0

嘿傢伙我想通了這個問題.... 問題是,i1應該設置爲0後第二個循環 和輸入數字的文件應該是f2.open(「數字」),而不是名字....愚蠢的錯誤!

+0

'i1'? 'f2'?你有沒有考慮過*有意義的*標識符? – Johnsyweb 2011-04-28 13:02:11

2

當你的閱讀與手機號文件

f2.open("./numbers"); 

while(f2>>file_input_name){ 

d[i1].phonenumber=file_input_number; 
i1++; 

} 

您存儲的電話號碼串file_input_name在後來您使用不同的變種,file_input_number存儲數組d中的信息;

0

由於您使用的是C++而不是C,因此應該利用該語言附帶的內容。不要使用數組來存儲數據,請使用std::vector。這樣,您不必記住已經將多少東西放入矢量中,因爲您總是可以請矢量告訴您size()

如果我有三個文件的讀取我會是這樣的:

#include <vector> 
#include <string> 
#include <iostream> 
#include <fstream> 
#include <algorithm> 

using std::cin; 
using std::cout; 
using std::fstream; 
using std::string; 
using std::vector; 

class contact { 
public: 
    string name; 
    string address; 
    int phone; 
}; 

void print_contact(const contact &c) { 
    cout << "name " << c.name << " address " << c.address << " phone " << c.phone << "\n"; 
} 

int main(int argc, char **argv) 
{ 
    vector<contact> contacts; 

    string name; 
    string address; 
    int phone; 

    fstream f1("d:\\names.txt"); 
    fstream f2("d:\\phones.txt"); 
    fstream f3("d:\\addresses.txt"); 

    // note that I am using getline() here. 
    while (getline(f1, name) && f2 >> phone && getline(f3, address)) { 
    contact c; 
    c.name = name; 
    c.address = address; 
    c.phone = phone; 
    contacts.push_back(c); 
    } 

    for_each(contacts.begin(), contacts.end(), print_contact); 

    // for the Windows console window 
    cout << "Press return to continue ..."; 
    string s; 
    getline(cin, s); 
    return 0; 
}