我決定用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;
}
}
在此先感謝
'i1'? 'f2'?你有沒有考慮過*有意義的*標識符? – Johnsyweb 2011-04-28 13:02:11