EOF Problem

Hi. Can someone help me to rectify the problem of my end of file? When I execute this problem, it cannot input the variables and I don't know what is the problem. Thanks in advance!

Here is the question:

Type 3: There are several lines containing the information of the given operations. Each line contains one string, denoting the operator and two bits denoting the operands. Read until end of file.

Sample Input 3
3
AND 1 1
OR 1 0
Sample Output 3
1
1


Code:

if(input == 3)
{
//cout << "(input3)" << endl;

while(!cin.eof())
cin >> b3 >> x3 >>y3;
//cout << (b3) << (x3) << (y3) << endl;

{
if(b3 == "AND")
{
if(x3 == 1 && y3 ==1)
{
cout << 1 << endl;
}
else
{
cout << 0 << endl;
}
}
if(b3 == "OR")

{
if(x3 || y3 == 1)
{
cout << 1 << endl;
}
else
{
cout << 0 << endl;
}
}
}
}
Last edited on
Are you dealing with actual text files? Because I'm pretty sure it ain't for cin.
Hi YFGHNG,

What you mean by text file? I am compiling on a cpp file.
Well yes, but the method you're calling is applicable to external files. In the same directory.

Like, you have a .cpp file, and then you have a .txt file.

EDIT: Tutorial for file io
http://www.cplusplus.com/doc/tutorial/files/
Last edited on
Oh I see. But I am not dealing with text files. May I ask how can I read end of file then?
Question: for the purposes of your project, why is it important you reach end of file?

Also, you can actually make it so your program can treat your .cpp as output. Notice how in the tutorial they have fileObject( fileName )? Instead of .txt, you just call your main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

int main()
{
    bool a ;
    bool b ;
    std::string binary_op ;

    // canonical: loop till input fails; either eof or invalid input (in this case, neither 0 nor 1) 
    while( std::cin >> binary_op >> a >> b ) 
    {
        if( binary_op == "AND" ) std::cout << ( a && b ) << '\n' ;
        else if( binary_op == "OR" ) std::cout << ( a || b ) << '\n' ;
        // etc.

        else std::cerr << "unsupported binary operation '" << binary_op << "'\n" ;
    }
}
Topic archived. No new replies allowed.