0

I am currently stuck on what I think is a very small problem. I'm currently in my second semester of comp sci and am pretty new to programming. I am sorry if this is a dumb question, but I cannot figure out why I am getting an output of 37 '.''s when there are clearly 36.

Our project is to create a movie theater ticketing system that has a final report showing the amount of ticket sales. We have to show reserved and open seats. The reserved seats work fine, but the open seats always display 1 more than there actually are.

In the function getSeatInfo, reservedSeats and openSeats are passed by reference. I believe they are automatically updated when the function runs to the final report function. When I use while(fs >> data) to go through the whole file correctly, it says that I have either half (if I use char data;) or none (if I use anything else). I can't figure it out.

Here is the file I am accessing. I am using "A1.txt" as a test key for my file stream.

............#.##
.....####.#....#
###.....#.#.###.
#...#.###.###...
#########.....##

Can someone take a look at this please? Thanks!

void finalReport() { // Function to print the end of days statistics

    for(int i = 1; i <= 3; i++) {

        string auditorium;

        switch(i) {

            case 1: auditorium = "A1.txt";
            break;

            case 2: auditorium = "A2.txt";
            break;

            case 3: auditorium = "A3.txt";
            break;

        }

        int reservedSeats = 0, openSeats = 0;
        getSeatInfo(auditorium, reservedSeats, openSeats);
        int totalSales = reservedSeats * 7;

        cout << "Auditorium " << i << ": " << " Reserved Seats: " << reservedSeats << " Open Seats: " << openSeats << " Total Sales: " << totalSales << endl;

    }


}

void getSeatInfo(string auditorium, int& reservedSeats, int& openSeats) {

    fstream fs;
    char currentChar;
    fs.open(auditorium, ios::in);

    while(!fs.eof()) {

        fs.get(currentChar);

        if(currentChar == '.') {

            reservedSeats += 1;
        }

        else if(currentChar == '#') {


            openSeats += 1;

        }


    }

    fs.close();

}

This is the output I am getting:

Auditorium 1:  Reserved Seats: 44 Open Seats: 37 Total Sales: 308
Auditorium 2:  Reserved Seats: 0 Open Seats: 0 Total Sales: 0
Auditorium 3:  Reserved Seats: 0 Open Seats: 0 Total Sales: 0
Thomas Walker
  • 145
  • 1
  • 7
  • 2
    [`while (!eof())` is wrong.](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – chris Feb 04 '17 at 07:03
  • @chris When I use int data; while(fs >> data) it shows that I have 0 open or reserved seats and messes everything up. – Thomas Walker Feb 04 '17 at 07:08
  • Your file doesn't have integers to read. You can still use that form while reading characters. – chris Feb 04 '17 at 07:31
  • @chris For some reason, when I change it to char, it only reads half of the information. There are 80 characters total in the file and when combined using char data; it only reads 40. What is going on here? – Thomas Walker Feb 04 '17 at 07:35
  • You probably left the other read in. – chris Feb 04 '17 at 07:38
  • @chris You are a life saver. I did not understand what the meaning of the char data;. I now realize I could use the currentChar that I had to read the data in the (fs >> currentChar) statement. It worked perfectly. Thank you for your patience I appreciate it. – Thomas Walker Feb 04 '17 at 07:41

0 Answers0