Confused from input and output

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
// read from file and show how many seats are reserved and open

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int main()
{
    char x = 'x'; // x represents open seats
    char o = 'o'; // o represents closed seats

ifstream myfile; // reading the filename object
myfile.open("seats.txt", ios::in); // when file opens

if(myfile.is_open())
{

    for(char i=0; i =  15; i++ ) // number of seats reserved
    {
        if(i == x)
        {
           cout << " there are " << x++ << " found" << endl;
        }
        else if (i == o)
        {
            cout << " there are " << o++ << " found" << endl;
        }
        }

    }
          cout << "the number of seats open are " << x << endl;
        cout << "the number of open seats are " << o << endl;

}
else
    cout << "sorry the file did not open correctly" << endl;

myfile.close();

   system("pause");
    return 0;
}
Last edited on
confused about reading a file , i am a beginner and is totally confused.

xoxxx
oxxxx
oxxoo

is the file but, every time i run it it says sorry file did not open correctly, i was wondering why is this happening and , am i doing any silly mistakes Thanks for any feedback.
Last edited on
Where are you actually reading from the file?
http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/

You haven't shown the format of the input file, so can not suggest specific code.

Line 21, 25: You're using the assignment operator (=), not the comparison operator (==).

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
@AbstractionAnon first off thanks for showing me the code tags and the assignment instead of comparison, silly mistake but, i am a bit confused on the format of the input file. Can you please elaborate?
every time i run it it says sorry file did not open correctly

Then your input file is not in the correct place, or is not named correctly.
It should be in the same directory as your source files.

What you've shown is a 5x3 array. Since we only need to count the open and reserved seats, we don't need to keep the entire array in memory. We only need to deal with one row at a time.

First off, we need someplace to read each row into. We'll use a std::string for that.
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
// read from file and show how many seats are reserved and open

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
    const char x = 'x'; // x represents open seats
    const char o = 'o'; // o represents closed seats
    string row;     //  Holds one row from the file
    int seats_open = 0;     //  We need counters for each
    int seats_taken = 0;
    
    ifstream myfile; // reading the filename object
    myfile.open("seats.txt", ios::in); // when file opens

    if(! myfile.is_open())
    {   cout << "sorry the file did not open correctly" << endl;
        return 1;
    }
    while (getline(myfile, row))
    {   //  Read a good row
        //  Count each character in the row
        for(unsigned i=0; i<row.length(); i++)      // loop for # of seats in row
        {   if(row[i] == x)
                seats_open++;           
            else if (row[i] == o)
                seats_taken++;                     
        }
    }
    cout << "the number of seats open are " << seats_open << endl;
    cout << "the number of seats taken are " << seats_taken << endl;
    system("pause");
    return 0;
}
the number of seats open are 10
the number of seats taken are 5
Press any key to continue . . .





Last edited on
i see now i wasn't reading the rows or columns before. I did not do much work on double arrays, just need more practice i guess, Thank you so much, it really helped out a lot.
Topic archived. No new replies allowed.