无法从包含字符串变量的文件中获取输入
Unable to take input from a file containing string variables
我决定用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;
}
相关文章:
- C++ - 忽略并从其他文件获取数据
- 为什么从文件获取图标时应用程序有时会崩溃?
- 如何在 c++ 窗口中将参数和返回的退出值传递到批处理文件/从批处理文件获取返回的退出值
- Qt .pro 文件获取 Qtkit 名称
- 提升属性树:如何使用xml文件获取子树的子级
- 从C 中的二进制文件获取整数值时
- 如何在Windows上绕过主机文件获取真实的IP地址
- 如何使用 CPP/C# 从 VHDX 文件获取文件(或文件系统)
- 从读取到对象指针数组的文件获取段错误
- 是否可以从IOS项目中的cpp文件获取文件路径
- 将数据从头文件获取到源文件中的非成员函数中
- 从两个不同的文件获取输入时,输入会发生变化
- 从文件 C++ 获取整数
- 通过在 C++ 中使用发布模式二进制文件获取崩溃原因
- C++ 将值从文件获取到数组中
- 使用HTTP上载文件.获取错误:-HttpSendReuest 12005
- 正在从外部文件获取输入
- 读/写同一个文件(获取gmon.out)
- 从二进制文件获取输入时发生访问冲突错误
- 从特定文件获取函数指针