C++ Programming:
How would you write a function that takes a filename and an array of struct time, opens a file, reads each line in the file as the number of days, converts this to a struct time and stores this is in an array?
Here is my code so far (p.s. the function i need help with is called void readData):
#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
#include<vector>
using namespace std;
struct time{
// private:
int years, months, days;
//public:
time(){}
time(int days){
}
};
time getYearsMonthsDays(int days){
time x;
x.years = days/450;
days = days%450;
x.months = days/30;
x.days = days%30;
return x;
}
int getTotalDays(time x){
int totalDays;
totalDays = x.years*450 + x.months*30 + x.days;
return totalDays;
}
void openofile(string ofilename,ofstream &fout){
fout.open(ofilename.c_str());
if(!fout){
cout << “Error opening output file…” << endl;
exit(1);
}
}
void openifile(string ifilename,ifstream &fin){
fin.open(ifilename.c_str());
if(!fin){
cout << “Error opening input file…” << endl;
exit(1);
}
}
void readData(vector <time> &myTimeVector, ifstream &fin){
string ifilename; // takes a file name
int days;
time xArray[days]; // takes an array of struct time
fin.open(ifilename.c_str()); // opens the file
while (!fin.eof()){
fin >> days; // reads each line as the number of days
time x = time(days); // converts this to a struct time
myTimeVector.push_back(x); // and stores this in the array
}
fin.close();
}
bool compare(time a, time b){
if (a.days < b.days)
return true;
else
return false;
}
void swap(time &a, time &b){
time temp;
temp = a;
a = b;
b = temp;
}
void sort(vector <time> &myTimeVector){
for (int i = 0; i < myTimeVector.size(); i++)
for (int j = i+1; j < myTimeVector.size(); j++)
if (compare(myTimeVector[i], myTimeVector[j]))
swap (myTimeVector[i], myTimeVector[j]);
}
void printData(vector <time> myTimeVector, ostream &out){
time x;
for (int i = 0; i < myTimeVector.size(); i++){
out << x.years << “\t” << x.months << “\t” << x.days << endl;
}
}
int main(){
time x;
string ifilename1 = “/home/ec2-user/environment/timedata.txt”;
string ofilename1 = “/home/ec2-user/environment/timeoutput.txt”;
vector<time> myTimeVector;
ifstream fin;
ofstream fout;
openifile(ifilename1,fin);
openofile(ofilename1,fout);
readData(myTimeVector,fin);
fin.close();
printData(myTimeVector,cout);
printData(myTimeVector,fout);
cout << “========================”<<endl;
fout << “========================”<<endl;
sort(myTimeVector);
printData(myTimeVector,cout);
printData(myTimeVector,fout);
fout.close();
return 0;
}
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more
Recent Comments