Compiles just fine for me, no data files so
Code:
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
ifstream myfile ("C:\\Users\\sshaw2\\Documents\\Untitled3.txt");
ofstream outfile ("C:\\Users\\sshaw2\\Documents\\output.txt");
int count = 0;
int x= 0;
float y=0.0;
string lastName;
string firstName;
string rental;
string balance;
std::vector<string> myvector;
string line;
getline(myfile,line);
outfile<<line<<endl;
while(getline(myfile,line))
{
myvector.push_back(line);
}
sort(myvector.begin(),myvector.end(),greater<string>());
// variable to to store the data
float totalbalanceDue =0.0;
int highestRental = 0;
float highestDue = 0.0;
string customerName = ""; // for highest rental customer
string customerName1 = ""; // for hishest due customer
while(!myvector.empty())
{
string line = myvector.back();
outfile<<line<<endl;
myvector.pop_back();
istringstream iss(line);
iss>>lastName;
iss>>firstName;
iss>>rental;
iss>>balance;
stringstream ss1(rental);
ss1>>x;
stringstream ss2(balance);
ss2>>y;
//logic for customer having highest dues
if(y>highestDue)
{
highestDue = y;
customerName1 = firstName+ " " + lastName;
}
else if(y==highestDue)
{
customerName1 = customerName1 + ", " +firstName+" "+lastName;
}
// logic for customer having highest rental
if(x>highestRental)
{
highestRental = x;
customerName = firstName+ " " + lastName;
}
else if(x==highestRental)
{
customerName = customerName + ", " +firstName+" "+lastName;
}
} // END WHILE
outfile <<endl;
outfile <<endl;
outfile <<"Total Balance Due for file:";
outfile <<totalbalanceDue<<endl;
outfile <<"Customer with highest number of rental days: ";
outfile <<customerName<<endl;
outfile <<"Customer with highest balance due are: ";
outfile <<customerName1;
myfile.close();
outfile.close();
return 0;
}
compile results
Code:
g++ -std="gnu++11" -Wall -o "temp" "temp.cpp" (in directory: /home/userx/bin)
temp.cpp: In function 'int main()':
temp.cpp:39:7: warning: unused variable 'count' [-Wunused-variable]
int count = 0;
^
Compilation finished successfully.