对不起,这很长,但我不知道我应该怎么做才能解决这个问题
对于输出的 0,我猜测它是另一个文件的内容,但在 0 中我只是不知道如何摆脱它们
这是我的代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Users
{
int user_id;
string fname;
string lname;
char gender;
int age;
int phone;
string address;
};
struct Contacts
{
int user_id;
int contact_with;
int contact_start;
int contact_end;
int distance;
};
void discard_line(ifstream &in)
{
char c;
do{
in.get(c);
}while(c != '\n');
}
void users(ifstream &userFile, int size, struct Users user[])
{
cout << "-----------------------------------------------------\n";
cout << "UserID\tFname\t Lname\tGender\tAge\tPhone\tAddress" << endl;
cout << "-----------------------------------------------------\n";
for(int i = 0; i < size; i++)
{
cout << user[i].user_id << "\t" << user[i].fname << "\t" << user[i].lname << "\t" << user[i].gender << "\t" << user[i].age << "\t" << user[i].phone << "\t" << user[i].address << endl;
}
//total number of users
}
void contacts(ifstream &contactfile, int size, struct Contacts contact[])
{
cout << "-----------------------------------------------------\n";
cout << "UserID\tCon/With\tDuration(s)\tDistance(cm)" << endl;
cout << "-----------------------------------------------------\n";
for(int i = 0; i < size; i++)
{
int duration = contact[i].contact_end - contact[i].contact_start;
cout << contact[i].user_id << "\t" << contact[i].contact_with << "\t\t" << duration << "\t\t" << contact[i].distance << endl;
}
}
int main()
{
int option;
const int SIZE = 1000;
int index = 0;
Users user[SIZE];
Contacts contact[SIZE];
ifstream userFile("users.txt");
ifstream contactFile("contacts.txt");
if(!userFile)
{
cout << "The file is not found" << endl;
exit(1);
}
if(!contactFile)
{
cout << "The file is not found" << endl;
exit(1);
}
discard_line(userFile);
discard_line(contactFile);
while(!userFile.eof())
{
userFile >> user[index].user_id >> user[index].fname >> user[index].lname >> user[index].gender >> user[index].age >> user[index].phone >> user[index].address;
index++;
}
while(!contactFile.eof())
{
contactFile >> contact[index].user_id >> contact[index].contact_with >> contact[index].contact_start >> contact[index].contact_end >> contact[index].distance;
index++;
}
users(userFile,index,user);
contacts(contactFile,index,contact);
return 0;
}
输出是这样的:
-----------------------------------------------------
UserID Fname Lname Gender Age Phone Address
-----------------------------------------------------
1001 Ray Dixon M 46 9364652 Lokia
1002 Bryan Green M 18 9579302 Drekena
1003 Justin Dixon M 33 9353533 Lokia
1004 Lester Byrd M 45 9534695 Nasilai
1005 Santos Larson M 53 9093177 Vunuku
1006 Bryan Cobb M 42 9905139 Narocivo
1007 Eddie Watson M 20 9610408 Nabua
1008 Wesley Barton M 27 9801864 Nasigatoka
1009 Victor Mason M 50 9855386 Nukutubu
1010 Ellis Cobb M 24 9389406 Narocivo
1011 Diana Ross F 27 9940148 Vunuku
1012 Amanda Carter F 43 9506743 Nasilai
1013 Maria Edwards F 53 9798534 Narocivo
1014 Maria Jenkins F 34 9352516 Lomanikoro
1015 Louise Davis F 55 9812126 Nasilai
1016 Sandra Sanders F 29 9369570 Tavuya
1017 Bonnie Roberts F 40 9689234 Nukui
1018 Melissa Harris F 29 9321235 Drekena
1019 Marilyn Parker F 56 9409221 Nukui
1020 Bonnie Lopez F 43 9342939 Nasigatoka
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
我正在尝试删除 0,但我不知道该怎么做。但是,当我取出任一文件的读数时,它会显示没有 0 的正确输出
while(!userFile.eof())
{
userFile >> user[index].user_id >> user[index].fname >> user[index].lname >> user[index].gender >> user[index].age >> user[index].phone >> user[index].address;
index++;
}
while(!contactFile.eof())
{
contactFile >> contact[index].user_id >> contact[index].contact_with >> contact[index].contact_start >> contact[index].contact_end >> contact[index].distance;
index++;
}
users(userFile,index,user);
contacts(contactFile,index,contact);
假设索引从零开始。假设 userFile 有 10 行假设 contactFile 有 10 行
当您在 while(!userFile.eof()) 中填充您的用户数组时,您将索引增加到 10...但是在执行 while(!contactFile.eof()) 时,您的索引不再从零开始...而是从 10 点停止的地方开始并继续到 20 点
你需要一个额外的索引...
int uIndex = 0;
int cIndex = 0;
while(!userFile.eof())
{
userFile >> user[uIndex].user_id >> user[uIndex].fname >> user[uIndex].lname >> user[uIndex].gender >> user[uIndex].age >> user[uIndex].phone >> user[uIndex].address;
uIndex++;
}
while(!contactFile.eof())
{
contactFile >> contact[cIndex].user_id >> contact[cIndex].contact_with >> contact[cIndex].contact_start >> contact[cIndex].contact_end >> contact[cIndex].distance;
cIndex++;
}
users(userFile,uIndex,user);
contacts(contactFile,cIndex,contact);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句