hey i need helpin in pipping a file and using those values in the file to work with the program. ive done the program but it doesnt read the file. can someone help me here.
#include<iostream>
#include<string.h>
using namespace std;
//the cost of the toll gates
const double A_GATE = 2.2;
const double B_GATE = 2.8;
const double C_GATE = 2.3;
const double D_GATE = 3.8;
const string CUSTOMER_TOK = "@";
//Returns the cost of the toll gate
double getTollAmount(string tollGate)
{
if (tollGate == "A")
return A_GATE;
if (tollGate == "B")
return B_GATE;
if (tollGate == "C")
return C_GATE;
if (tollGate == "D")
return D_GATE;
//if it reaches here then its an invalid gate
cerr << "Invalid Gate: Corrupted tolldata.txt file" << endl;
return -1;
}
int main() {
string line; //line from stdin
string token1; //id
string token2; //toll gate or '@'
string token3; //timestamp or name/address
string token4; //date
string token5; //time
double amount;
double totalAmount = 0;
int startPos, endPos;
//read each line from the piped in file
while(true) {
//read a whole line
cin >> ws;
getline(cin, line);
if(cin.fail()) break; // abort program upon any input failure
startPos = 0;
endPos = 0;
//get first token
endPos = line.find("\t");
token1 = line.substr(startPos, endPos-startPos);
//get next token
startPos = endPos+1;
endPos = line.find("\t", startPos);
token2 = line.substr(startPos, endPos-startPos);
//get next token
startPos = endPos+1;
endPos = line.find("\t", startPos);
token3 = line.substr(startPos, endPos-startPos);
//start of a new customer
if (token2 == CUSTOMER_TOK) {
//we just finished processing the previous customer
if (totalAmount != 0)
cout << "TOTAL: $" << totalAmount << endl;
totalAmount = 0;
//prints out the header with the customer details
cout << "\n--INVOICE FOR TOLL EXPENSES--" << endl;
cout << "-ID-\t\t-Name, Address-" << endl;
cout << token1 << "\t\t" << token3 << "\n" << endl;
cout << "-Date-\t\t-Time-\t\t-Station-\t-Amount-" << endl;
}
//print the record charged to the customer
else {
//parse for the date
startPos = endPos+1;
endPos = token3.find(":", startPos);
token4 = token3.substr(startPos, endPos-startPos);
//parse for the date
startPos = endPos+1;
endPos = token3.length();
token5 = token3.substr(startPos, endPos-startPos);
//get the cost of the toll gate
amount = getTollAmount(token2);
totalAmount += amount;
//prints out the record charged details
cout << token4 << "\t" << token5 << "\t\t" << token2 << "\t\t" << amount << endl;
}//end else
}//end while
//print the total amount for the last customer
cout << "TOTAL: $" << totalAmount << endl;
system("pause");
return 0;
}//end main