Writing a program that reads and writes registered classroom seats

I'm trying to write a program that uses an "input.txt" and outputs it to "output.txt". The file contains the course name, building code, room the number of seats in the room and the number of students already registered. I need to out put in a certain format for my class, and I'm not getting very far.

this is the input
 
 CSC_101 TEC 202 44 37


this is the output
1
2
3
Subject        Building       Room           Open Seats
********************************************************
csc_101         TEC          202                  7



and this is the code i've got so far to open the file:
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>



using namespace std;


int main() {

	fstream infile("input.txt", ios::in);
	fstream outfile("output.txt", ios::out);

	char cname[8];
	char bname[4];
	int num1, num2, num3;

	while (infile.get(cname)) {

		if (cname == '\n')lines++;
	}

}


I'm kind of scrapping off another project of mine, but I can't seem to figure it out. It's an online class and the videos don't do much help in explaining how things function.

Thanks in advance for the help.
This link may help you a little bit about files:
http://www.cplusplus.com/doc/tutorial/files/

Are you restricted to use only char data types for the course name and the building name or can you use the string?

My suggestion would be to work in small parts of the problem. In other words, I would first focus on reading the file with no issues and ensure that it is place in the correct variables.

Also, I would ensure the file was open successfully with an if-else statement.

if (file open)
// Read the file

else
// Notify the user.
I'm not restricted for the char data types, I can use any. That's just what I used on my last project and I figured it would transfer to this one, but nothing seems to transfer...
Before you decide on how you're going to read in the information, I would consider how you're going to store in the information you are reading. That requires reading in the file, obviously, but how are you organizing it? You certainly have to use some form of data structure.

1
2
3
char cname[8];
char bname[4];
int num1, num2, num3;


This implies that file you are reading from only has one course. What if it has multiple courses and the file contains a lot of lines? How are you going to read it then? Consider the following:

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
67
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main() {

  // Define a very, very basic data structure here. These arrays are all known
  // as parallel arrays. It isn't the best, but most programming 101 classes
  // seem to introduce this is the most basic data structure.
  const size_t MaxCourses = 10;
  string courseName[MaxCourses];
  string courseLoc[MaxCourses];
  int roomNum[MaxCourses];
  int maxSeats[MaxCourses];
  int takenSeats[MaxCourses];
  int openSeats[MaxCourses];
  size_t counter = 0; // This will keep track of where we are at in our array.

  // Create an object to access the file stream.
	fstream infile("input.txt");
  // If we can't open the input file, why continue with the program? Get out of
  // the program by returning 1 or some other value (it doesn't really matter
  // the number you return unless you're dealing with something much more
  // advance).
  if(!infile) {
    cerr << "Error: Unable to open the file\n";
    return 1;
  }
  else {

    /* Ok, this is where you are going to be reading from the file, assuming that
     * it has been opened using the ifstream object. From what you've given us,
     * the input file is formatted as such:
     *   COURSE  BUILDING  ROOM  MAX_SEATS  TAKEN_SEATS
     *
     *   So their data types will be the following
     *   STRING  STRING  INT  INT  INT
     *
     * From the above, you know you have to read in a 2 strings and then 3 ints
     * */

    while (infile >> courseName[counter] && counter < MaxCourses) {
      infile >> courseLoc[counter]
             >> roomNum[counter]
             >> maxSeats[counter]
             >> takenSeats[counter];

      // Once we are done with one iteration of the loop, we increase the
      // counter by one because by incrementing by 1, the next iteration will
      // deal with the next array.
      counter++;
    }
    infile.close();
  }

  // Output to the file
	fstream outfile("output.txt");
  for(size_t i = 0; i < counter; i++)
    outfile << "Course " << courseName[i] << " at " << courseLoc[i]
            << " " << roomNum[i] << " with " << (maxSeats[i] - takenSeats[i])
            << " open seats\n";
  outfile.close();
  return 0;
}


EDIT:
Here's the file I am reading from

CSC_101 TEC 202 44 37
CSC_102 TEC 203 30 23
CSC_211 TEC 204 25 11


And here is the output file

Course CSC_101 at TEC 202 with 7 open seats
Course CSC_102 at TEC 203 with 7 open seats
Course CSC_211 at TEC 204 with 14 open seats
Last edited on
Topic archived. No new replies allowed.