| | |
C++ While Loop and istreamVar.eof() HELP!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 30
Reputation:
Solved Threads: 0
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
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 ^^;
C++ Syntax (Toggle Plain Text)
#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 ^^;
Last edited by Ancient Dragon; Feb 28th, 2008 at 10:15 pm. Reason: add line numbers for convenience
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.
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.
C++ Syntax (Toggle Plain Text)
cout << fixed << showpoint; cout << setprecision(2); while( ticketSale >> num1 >> num2 ) { cout << "Ticket Price: $ " << num1 << " Tickets Sold: " << num2 << " Profit: $ " << num1*num2 << endl; }
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: delete[]
- Next Thread: cstring functions??
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






