Application using File I/o

I am writing an application that will figure out the hotel occupancy. I previously wrote the program using user input but now I need to modify it to use file I/o. I finally got the file to be able to open but i'm not really sure how to read in the lines or the most practical way of doing this? At the bottom I showed how the file is setup. The first line of the file will be the int numFloors. The next lines will be numRooms followed by numOccupied.

Is there a way to read the first line and set it equal to the variable numFloors? What is the best way to read the file? Also, Do I need to remove the cin >> inside of my code for the current blocks of code?

#NEW
I have to also put in error handling:For failed validations, issue a message to describe the validation error and
a. Return with return code 10 if file fails to open
i. This is written as: return 10;
b. Return with return code 12 if fail to read number of floors (e.g. empty
file).
c. Return with return code 14 of number of floors is invalid
d. For floor related validations, issue a continue statement to skip further
processing for that floor.

I have put in the return and continues where I believe they belong but when the code compiles it stops as soon as it hits the first error in the file. Is the continue in the wrong place?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "pch.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	ifstream inputFile;
	inputFile.open("Occupancyx.txt");
	if (!inputFile.is_open()) {
		return 10;
	}
	int numFloors, numRooms, numOccupied, totalEmpty, totalRooms, totalOccupied;
	totalRooms = 0;
	totalOccupied = 0;


	double occupancyRate;


	cout << "How many floors are there? ";
		inputFile >> numFloors;
		cout << numFloors << endl;
		if (inputFile.eof()) {
			return 12;
		}
		while (numFloors <= 1) {
			return 14;
			cout << "Please enter a valid number of floors : " << endl;
			inputFile >> numFloors;
		}

		for (int i = 1; numFloors >= i; i++) {
			if (i == 13)
				continue;


			cout << "How many rooms are on floor " << i << "? ";
			inputFile >> numRooms;
			cout << numRooms << endl;
			while (numRooms <= 9) {
				continue;
			}
			cout << "How many of the rooms are occupied: ";
			inputFile >> numOccupied;
			cout << numOccupied << endl;
			while (numOccupied < 0 || numOccupied > numRooms) {
				continue;
			}
			totalRooms += numRooms;
			totalOccupied += numOccupied;
			totalEmpty = totalRooms - totalOccupied;
			occupancyRate = static_cast <double>(totalOccupied) / totalRooms * 100;
			cout << setprecision(1) << fixed << endl;

		}

		cout << "The hotel has a total of " << totalRooms << " total rooms" << endl;
		cout << "The hotel has a total of " << totalOccupied << " occupied rooms" << endl;
		cout << "The hotel has a total of " << totalEmpty << " empty rooms" << endl;
		cout << "Percentage of occupied rooms is " << occupancyRate << endl;

	}


The file:
14
1 10
12 -10
13 14
14 10
15 10
16 10
17 10
18 10
19 10
20 10
21 10
22 10
24 10
Last edited on
I think you can replace all the old cins with inputFile
eg
inputFile >> variable;

and remove the prompts, wherever you wanted to read from the file instead of the keyboard.
Topic archived. No new replies allowed.