tags:

views:

126

answers:

7

When I use the code recommended in the book, I get an error. I am using NetBeans 6.8 for Mac.

Here is the code:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{

  ifstream inputFile;

  int number;

  inputFile.open("MacintoshHD/Users/moshekwiat/Desktop/random.txt");

  inFile >> number;

  cout<<endl <<number<<endl;

  inputFile.close();

  return (0);
}

Here is the error:

main.cpp:20: error: 'inFile' was not declared in this scope

What needs to be done?? Thank You

A: 

Change inFile to inputFile

John Weldon
+2  A: 

Replace inFile with inputFile.

Orsol
Thanks-got rid of the error, but now it simply outputs 0! The file has a different number on every line
mokwi8
@mokwi8: If you want to read each line, do so. There's no trace of a loop in your program. You read from inFile once in the program, and that's probably what's happening.
David Thornley
@David Thornley: Sorry, I am new to C++/programming in general. What would be the best looping statement to use? I do not know how many numbers there are.Also, I checked the file-first number is not 0, it is 42
mokwi8
@mokwi8: After fixing the typo, you've written a program to get exactly one integer (the first) out of the file and print it. That's exactly what your program is doing! Keep reading and your book will no doubt show you how to handle the rest of the file.
Owen S.
@Owen S.: I thought so too, but the first number is not 0, and that is what my program is outputting
mokwi8
@mokwi8: ok, got it. I posted the problem and fix to that below.
Owen S.
+1  A: 

For a start, there's no 'inFile' object in your code.

inFile >> number;

Look again:

ifstream inputFile;
A: 

Are you serious? read your code please.

Liso
You just registered today to say... that ?! Are you serious?
ereOn
A: 

change inFile to inputFile

ennuikiller
A: 

Use inputFile instead of inFile

Gustavo V
+1  A: 

Problem 1 (the one the compiler sees) is a simple typo: inFile should be inputFile. Do make sure you check for typos like this before posting to Stack Overflow.

Problem 2: the path name to your file is probably wrong, and generally, when you try to read from a stream that couldn't be initialized properly because the file couldn't be opened, you'll get 0.

In this case the path you specified is a relative path to the file from the directory your program was launched in, so whatever directory you ran the program from would need a subdirectory called "MacintoshHD", then "Users", then... you get the idea. To get the correct path, right-click on the file in the Finder and select "Get Info". Under "Where: " you'll see the correct path to the directory that contains your file; it will probably say "/Users/moshekwiat/Desktop". Add "/random.txt" to that and that should be the path you use.

Normally, C++ programmers will write code to make sure the file opens correctly before reading from it. A simple way to check for that after initializing inputFile, but before trying to read from it is:

if (! inputFile) {
    cerr << "Could not open the file!" << endl;
    return 1; // returning non-0 status is customary
              // if your program encounters an error
}
Owen S.