Sep 28, 2021 at 6:28pm UTC
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void printContacts(string user_id[], float contact_with[], float contact_start[],float contact_end[],double distance[], int size)
{
cout << "user_id\tcontact_with\tcontact_start\tcontact_end]tdistance" << endl;
for (int i = 0; i < size; i++)
{
cout << "\n" << user_id[i] << contact_with[i] << contact_start[i] << contact_end[i] << distance[i] << endl;
}
}
struct Contacts
{
string user_id;
float contact_with;
float contact_start;
float contact_end;
double distance;
};
int main()
{
//Welcoming messages
cout << "WELCOME TO THE CONTACT TRACE PROGRAM DURING COVID-19 OUTBREAK IN FIJI" << endl;
cout << "-----------------------------------------------------------------------" << endl;
const int CAPACITY = 1000;
string user_id[CAPACITY];
float contact_with[CAPACITY];
float contact_start[CAPACITY];
float contact_end[CAPACITY];
double distance[CAPACITY];
int size = 0;
ifstream infile;
infile.open("contacts.txt");
if (infile.is_open())
{
string temp;
string id;
float cont_with;
float con_start;
float con_end;
double dist;
getline(infile, temp);
while (infile >> id >> cont_with >> con_start >> con_end >> dist)
{
user_id[size] = id;
contact_with[size] = cont_with;
contact_start[size] = con_start;
contact_end[size] = con_end;
distance[size] = dist;
size++;
}
}
else
{
cout << "Could not found file" << endl;
}
/*while (!infile.eof())
{
infile >> cons[size].user_id >> cons[size].contact_with >> cons[size].contact_start >> cons[size].contact_end >> cons[size].distance;
cout << cons[size].user_id << cons[size].contact_with << cons[size].contact_start << cons[size].contact_end << cons[size].distance;
size++;
}
infile.close();*/
printContacts(user_id, contact_with, contact_start, contact_end, distance);
return 0;
}
Last edited on Sep 28, 2021 at 6:31pm UTC
Sep 28, 2021 at 9:10pm UTC
avoid the .eof() loop ... it does not always work as expected. It works ok until it does not, but when it does not, its difficult to diagnose and fix.
prefer a while( file read) style, eg
while( file>>data)
Last edited on Sep 28, 2021 at 9:10pm UTC
Sep 28, 2021 at 9:31pm UTC
my question is to correct my code, which is a function that read the text from a file but a uses of struct, pointers, arrays and classes
Last edited on Sep 28, 2021 at 9:31pm UTC
Sep 28, 2021 at 9:33pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void printContacts(string user_id[], float contact_with[], float contact_start[],float contact_end[],double distance[], int size)
{
cout << "user_id\tcontact_with\tcontact_start\tcontact_end]tdistance" << endl;
for (int i = 0; i < size; i++)
{
cout << "\n" << user_id[i] << contact_with[i] << contact_start[i] << contact_end[i] << distance[i] << endl;
}
}
struct Contacts
{
string user_id;
float contact_with;
float contact_start;
float contact_end;
double distance;
};
int main()
{
//Welcoming messages
cout << "WELCOME TO THE CONTACT TRACE PROGRAM DURING COVID-19 OUTBREAK IN FIJI" << endl;
cout << "-----------------------------------------------------------------------" << endl;
const int CAPACITY = 1000;
string user_id[CAPACITY];
float contact_with[CAPACITY];
float contact_start[CAPACITY];
float contact_end[CAPACITY];
double distance[CAPACITY];
int size = 0;
ifstream infile;
infile.open("contacts.txt" );
if (infile.is_open())
{
string temp;
string id;
float cont_with;
float con_start;
float con_end;
double dist;
getline(infile, temp);
while (infile >> id >> cont_with >> con_start >> con_end >> dist)
{
user_id[size] = id;
contact_with[size] = cont_with;
contact_start[size] = con_start;
contact_end[size] = con_end;
distance[size] = dist;
size++;
}
}
else
{
cout << "Could not found file" << endl;
}
/*while (!infile.eof())
{
infile >> cons[size].user_id >> cons[size].contact_with >> cons[size].contact_start >> cons[size].contact_end >> cons[size].distance;
cout << cons[size].user_id << cons[size].contact_with << cons[size].contact_start << cons[size].contact_end << cons[size].distance;
size++;
}
infile.close();*/
printContacts(user_id, contact_with, contact_start, contact_end, distance);
return 0;
}
make the function that the texts from file correct on using struct, pointers, arrays,and classes
Last edited on Sep 28, 2021 at 9:34pm UTC