无法从包含字符串变量的文件中获取输入

时间:2011-04-16 08:00:19

标签: c++ string

我决定用c ++制作电话簿,并决定从文件中输入姓名,地址和号码。 所以我创建了一个名为contact的类,并声明了公共变量的名称,地址和编号。 使用构造函数我将这些初始化为name =“noname”(字符串),number = 0(int),address =“no address”(string) 现在我的主体是:

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++;

}

现在,当我稍后按名称搜索特定条目时,名称正确显示,但是phoneumber作为垃圾值返回,地址为“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;                  
  }
}

提前致谢

3 个答案:

答案 0 :(得分:2)

当您使用电话号码阅读文件时

f2.open("./numbers");

while(f2>>file_input_name){

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

}

您将电话号码存储在字符串file_input_name中,但随后使用不同的var file_input_number将信息存储在数组d中;

答案 1 :(得分:0)

嘿伙计们我发现了问题.... 问题是第二次循环后i1应该设置为0 并且输入数字的文件应该是f2.open(“数字”)而不是名字....愚蠢的错误!!

答案 2 :(得分: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;
}