C++ While Loop and istreamVar.eof() HELP!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 30
Reputation: Exo1337 is an unknown quantity at this point 
Solved Threads: 0
Exo1337 Exo1337 is offline Offline
Light Poster

C++ While Loop and istreamVar.eof() HELP!

 
0
  #1
Feb 28th, 2008
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

  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. ifstream ticketSale;
  10. int num2, num4, num6, num8;
  11. double num1, num3, num5, num7;
  12.  
  13. while(!ticketSale.eof())
  14.  
  15. ticketSale.open("c:\\ticketSale.txt");
  16.  
  17. cout << fixed << showpoint;
  18. cout << setprecision(2) << endl;
  19.  
  20. ticketSale >> num1 >> num2;
  21. cout << "Ticket Price: $ " << num1 << " Tickets Sold: " << num2 << " Profit: $ " << num1*num2 << endl;
  22.  
  23. ticketSale >> num3 >> num4;
  24. cout << "Ticket Price: $ " << num3 << " Tickets Sold: " << num4 << " Profit: $ " << num3*num4 << endl;
  25.  
  26. ticketSale >> num5 >> num6;
  27. cout << "Ticket Price: $ " << num5 << " Tickets Sold: " << num6 << " Profit: $ " << num5*num6 << endl;
  28.  
  29. ticketSale >> num7 >> num8;
  30. cout << "Ticket Price: $ " << num7 << " Tickets Sold: " << num8 << " Profit: $ " << num7*num8 << endl;
  31.  
  32.  
  33. ticketSale.close();
  34.  
  35.  
  36. return 0;
  37. }

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 ^^;
Last edited by Ancient Dragon; Feb 28th, 2008 at 10:15 pm. Reason: add line numbers for convenience
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,408
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ While Loop and istreamVar.eof() HELP!

 
0
  #2
Feb 28th, 2008
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.
  1. cout << fixed << showpoint;
  2. cout << setprecision(2);
  3. while( ticketSale >> num1 >> num2 )
  4. {
  5. cout << "Ticket Price: $ " << num1 << " Tickets Sold: " << num2 << " Profit: $ " << num1*num2 << endl;
  6. }
Last edited by Ancient Dragon; Feb 28th, 2008 at 10:25 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 98
Reputation: codeaa is an unknown quantity at this point 
Solved Threads: 12
codeaa codeaa is offline Offline
Junior Poster in Training

Re: C++ While Loop and istreamVar.eof() HELP!

 
0
  #3
Feb 28th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC