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:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
usingnamespace 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.
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.
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:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
usingnamespace 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;
}