Trying to grab info from a text file

I cant seem to get my program to find the .txt file I have created. Is there a particular spot I should create it? The code I have is below.
The program just automatically goes to error.


ifstream inputFile ("PaintInfo.txt");
inputFile.open ("PaintInfo.txt");
if (!inputFile)
{
cout << "Unable to open the file." << endl << endl;
exit (1);
}
string lastname;
double roomnumber, wallcolor, ceilingcolor, windowcount, doorcount;
double height, width, depth;

inputFile >> lastname;
cout << "Last Name: " << lastname << endl << endl;
inputFile >> roomnumber;
cout << "Number of Rooms: " << roomnumber << endl << endl;
inputFile >> windowcount;
cout << "Number of Windows: " << windowcount << endl << endl;
inputFile >> doorcount;
cout << "Number of Doors: " << doorcount << endl << endl;
inputFile >> wallcolor;
cout << "Wall Color: " << wallcolor << endl << endl;
inputFile >> ceilingcolor;
cout << "Ceiling Color: " << ceilingcolor << endl << endl;
inputFile >> height >> width >> depth;
cout << "The height, width, and depth of the room: " << height << ", " << width << ", " << depth << endl << endl;
The first two lines are redundant. I don't know if attempting to open a file without closing the previous would cause your problem, if any.
1
2
3
4
5
ifstream file("file.txt");
//or
ifstream file;
file.open("file.txt");
//but not both 


Make sure your file is in either:
1) the same directory as the project file if you are running the program with an IDE
2) the same directory as the executable if you are running the program directly

Also, instead of exit(1), you could have a simple return 1;.
Topic archived. No new replies allowed.