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

Unable to take input from a file containing string variables

本文关键字:文件 获取 输入 变量 包含 字符串      更新时间:2023-10-16

我决定用c++制作一个电话簿,并决定从文件中输入姓名、地址和号码。因此,我创建了一个名为contact的类,并声明了公共变量name、address和number。我使用构造函数将它们初始化为name="noname"(string), 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++;
}

现在,当我稍后按名称搜索特定条目时,名称会正确显示,但电话号码作为垃圾值返回,地址为"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<<"nnSearching.........nn";
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
        <<"nn";
    val=j;                  
  }
}

Thanks in advance

当您读取带有电话号码的文件

f2.open("./numbers");
while(f2>>file_input_name){
d[i1].phonenumber=file_input_number;
i1++;
}

将电话号码存储在字符串file_input_name中,然后使用另一个变量file_input_number将信息存储在数组d中;

嘿,伙计们,我找到问题了....问题是i1应该在第二个循环后被设置为0接受输入数字的文件应该是f2.open("数字"),而不是名称....愚蠢的错误! !

既然您使用的是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;
}