// read from file and show how many seats are reserved and open
#include <iostream>
#include <fstream>
#include <cstdlib>
usingnamespace 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;
}
elseif (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;
}
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.
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.
@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.
// read from file and show how many seats are reserved and open
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
usingnamespace std;
int main()
{
constchar x = 'x'; // x represents open seats
constchar 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++;
elseif (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 . . .
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.