English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Using C++ programing how would I set up a loop that can read in a number, use the number, and then go on to the next number until I reach an end of file indicator?

Here's a little section of the program:

ifstream inData;

inData.open ("numbers.dat");
inData >> Number;
FormatNum();
cout << Number << endl;

2007-11-05 06:33:26 · 3 answers · asked by steven j 1 in Computers & Internet Programming & Design

3 answers

Just put the section of code inside a while (!inData.fail()) loop.

2007-11-05 06:41:09 · answer #1 · answered by Neebler 5 · 0 0

while (! inData.eof() )
{
fin >> Number;

if (! inData)
break;

FormatNum();
cout << Number << endl;
}

I'm assuming the FormatNum(); is a function that modifies your input. That little if statement I added is just in case there is a trailing EOL (end of line), which would lead the program to believe there is more data when there really isn't and would lead to a crash. So this if statement checks to see if valid data comes in, if not it exists the loop.

Also the .eof() checks to see if this is the end of file.

2007-11-05 15:06:04 · answer #2 · answered by Anonymous · 0 0

Not sure for C++, but for VB, which should be pretty close,

do until filename.eof
what you have
move to next number in file
loop

2007-11-05 14:41:50 · answer #3 · answered by AJ 7 · 0 0

fedest.com, questions and answers