• 0

(C++) Unexpected end of file found


Question

We're learning how to input text files. This assignment is to find the median of a sequence of numbers (ordered).

I'm able to read in the data once, to give me a list lf the numbers. Great. Next, I need to read in the data again so I can find the number at a specific position. The way I'm doing it now is to reopen the file and use a FOR LOOP to go through the inputs until I reach the input at the position I desire.

The problem is that it won't let me do that. It keeps giving me an unexpected end of file error. I don't know why this is happening, because I'm closing the file and then reopening it.

Any suggestions would be appreciated.

Here's the code; Hopefully the formatting won't get screwed up. heh

#include <iostream.h>

#include <fstream.h>

#include <cmath>

#include <iomanip.h>

double input, total;

int position1, position2, position, count, count1;

int main()

{

//**************

//* INITIALIZE *

//**************

double middle;

count = 0;

count1 = 0;

middle = 0;

//------------

//| Greeting |

//------------

cout << "Welcome to Warren's Median Evaluator! \n\n";

//-----------------

//| Open the file |

//-----------------

ifstream fin;

fin.open("C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\A3 Part 1a\\even.dat");

//***********

//* PROCESS *

//**********************************************************

//* Check if file opened correctly.                        *

//* Read entries from file into program and count how many *

//**********************************************************

if (!fin)

{

  cout << "File could not be opened, or end of file.";

  fin.close();

  return 1;

}

else

  cout << "The numbers read from the file are: \n";

  fin >> input;

  while (fin)

  {

  total = total + input;

  count = count + 1;

  cout <<input << " ";

  fin >> input;

  }

  fin.clear();

fin.close();

//*************

//* TERMINATE *

//**********************

//* Final calculations *

//**********************

fin.open("C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\A3 Part 1a\\even.dat");

if (!fin)

{

  cout << "File could not be opened, or end of file.";

  fin.close();

  return 1;

}

else

  cout << "The numbers read from the file are: \n";

  fin >> input;

  while (fin)

  {

  for (count1 = 0; count = 4; count++)

    fin >> input;

  cout << input;

  }

return 0;

}

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0
We're learning how to input text files.  This assignment is to find the median of a sequence of numbers (ordered). 

I'm able to read in the data once, to give me a list lf the numbers.  Great.  Next, I need to read in the data again so I can find the number at a specific position.  The way I'm doing it now is to reopen the file and use a FOR LOOP to go through the inputs until I reach the input at the position I desire.

The problem is that it won't let me do that.  It keeps giving me an unexpected end of file error.  I don't know why this is happening, because I'm closing the file and then reopening it. 

Any suggestions would be appreciated.

Here's the code; Hopefully the formatting won't get screwed up. heh

584816831[/snapback]

I think the problem lies in your file path. The path has spaces so you need to put quotes around it.

"\"C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\A3 Part 1a\\even.dat\""

Link to comment
Share on other sites

  • 0

if your project is in the "C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\A3 Part 1a" directory, then just using "even.dat" would be fine.

STV

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.