Ok so I need to pull up records one at a time and then have the loop terminate when it reches the end of the input file using while and the istreamVar.eof() commands

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
	ifstream ticketSale;
	int num2, num4, num6, num8;
	double num1, num3, num5, num7;

	while(!ticketSale.eof())
	
	ticketSale.open("c:\\ticketSale.txt");
	
    cout << fixed << showpoint;
	cout << setprecision(2) << endl;

	ticketSale >> num1 >> num2;
	cout << "Ticket Price: $ " << num1 << " Tickets Sold: " << num2 << "   Profit: $ " << num1*num2 << endl;
	
	ticketSale >> num3 >> num4;
	cout << "Ticket Price: $ " << num3 << " Tickets Sold: " << num4 << "  Profit: $ " << num3*num4 << endl;

	ticketSale >> num5 >> num6;
	cout << "Ticket Price: $ " << num5 << "  Tickets Sold: " << num6 << "  Profit: $ " << num5*num6 << endl;
	
	ticketSale >> num7 >> num8;
	cout << "Ticket Price: $ " << num7 << "  Tickets Sold: " << num8 << "  Profit: $ " << num7*num8 << endl;
	
	
    ticketSale.close();
	

    return 0;
}

Input file contains:

250 5750
100 28000
50 35750
25 18750

Actual problem reads:

//Use the infile statement, while statement, and infile.eof() statement
//to try to read one record a time, process them and then read another
//record until end of file.

Please help ^^;

Recommended Answers

All 2 Replies

line 13 and 15 are backwards. You have to open the file before you can use it for anything. Switch those two lines around.

Next, you need to use brackets { and } to encluse all the code you want to be executed within that while statement. Without the brackets only the first statement immediately following the while statement will get executed.

The purpose of a while statement is to avoid the repetion that you have in your code, such as lines 20, 21 and 23, 24 and 26,27 and 29, 30. You can replace all those with just one set within the loop.

As for the infile.eof() -- I don't know why your teacher is stupid enough to require you to use it because it doesn't work the way most people would think. eof() is not even needed in your program.

cout << fixed << showpoint;
cout << setprecision(2);
while( ticketSale >> num1 >> num2 )
{
cout << "Ticket Price: $ " << num1 << " Tickets Sold: " << num2 << "   Profit: $ " << num1*num2 << endl;
}

You need to open the file before you start checking for eof. You should only need num1 and num2. That's what loop are for. Don't forget the { } brackets. It loops like you have the basic code needed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.