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

To get the values from the file, you have to open the file. Your program takes values from cin ( which is the standard input ) and outputs results to cout ( which is standard output). To be able to get values from a file. you have to open the file and then replace cin with the pointer for the opened file. Try searching for C++ File Handling tutorials. You will have to use
file open
replace cin with the file you opened
file close

WolfPack is correct if you want to put the name of the file into your code. From the post, I read that you wanted to pipe a file into it. It would help if you posted your operating system. This is out you can pipe files on a Linux machine:

> output redirect
< input redirect

You could type in a command something like this:

./myprogram < inputfile.dat > outputfile.dat

This would use whatever information is in inputfile.dat as your 'standard input'. Likewise it would redirect your standard output to outputfile.dat.

This way, you can use a file with your program without rewriting the code to open a file.

WolfPack is correct if you want to put the name of the file into your code. From the post, I read that you wanted to pipe a file into it. It would help if you posted your operating system. This is out you can pipe files on a Linux machine:

> output redirect
< input redirect

You could type in a command something like this:

./myprogram < inputfile.dat > outputfile.dat

This would use whatever information is in inputfile.dat as your 'standard input'. Likewise it would redirect your standard output to outputfile.dat.

This way, you can use a file with your program without rewriting the code to open a file.

Yes you are correct. You can do that in windows too. Then no need to rewrite the code. Sorry I didnt see the piping part. My mistake.

This article has been dead for over six months. Start a new discussion instead.