Hello All!

I am having trouble with my C++ code. The problem is that my code isn't giving me the correct logic outputs, Also, my teacher said to MUST MAKE the output look like this :

P and Q : T P or Q : T P --> Q : T
P and Q : F P or Q : T P --> Q : F
P and Q : F P or Q : F P --> Q : T

I am reading from a text file too. It's called prog2_input.txt and this is what it contains:

P   Q
T   F
F   T
F   F
T   T
T   F
F   T
F   F

this is the code I have so far, and any help is very appreciated :) Thanks! Oh, and NO advanced stuff please,
teacher said its very simple, and it should be a lot like the code that I have.

#include <iostream> // Include statement(s) //
#include <fstream>

using namespace std; // Using namespace statement(s) //

int main() {

    char storage; // Variable declaration(s) // 
    char letter1; 
    char letter2;

    ifstream input;
    input.open("prog2_input.txt"); // Opening file of txt from ifstream - input //

    input >> letter1 >> letter2; // input is reading each character as letter1 then letter2 //

    while (input >> storage) { // Placeholder for characters being read //

        cout << "P: " << storage << '\t'; // Outputs the storage for P being held //
        input >> storage; // Continues to read input //
        cout << "Q: " << storage << endl; // Outputs the storage for Q being held //

        if (letter1 && letter2) {
            // P and Q //
            cout << "P and Q: " << storage << ' ';
            //input >> storage;
        }

        if (letter1 || letter2) {
            // P or Q //
            cout << "P or Q: " << storage << ' ';
        }

        if ((!letter1) || (letter1 && letter2)) {
            // If P then Q //
            cout << "P --> Q: " << storage << endl; 
        }
    }

    input.close(); // Close input file //

    system ("PAUSE"); // Black box to appear //

    return 0; // return statement(s) //
}

Recommended Answers

All 3 Replies

Your variables letter1 and letter2 will always contain the values 'P' and 'Q' respectively (using the given input file, I mean). So doing things like if(letter1) doesn't make sense.

Inside the loop you should be reading the two truth values on each line into one variable each and then check those instead of letter1 and letter2. Keep in mind that they will be 'T' or 'F' rather than true or false.

You should never just output the truth value you just read. Instead you should output T when the given condition is true and F otherwise.

Okay, so I changed it to 'T' and 'F' but I'm still getting incorrect outputs. I'm also not sure how to change the implication (if) because the teacher just gave it to us and i'm not sure how he got it to be in C++. This is what I have so far:

#include <iostream> // Include statement(s) //
#include <fstream>

using namespace std; // Using namespace statement(s) //

int main() {

    char storage; // Variable declaration(s) // 
    char letter1; 
    char letter2;

    ifstream input;
    input.open("prog2_input.txt"); // Opening file of txt from ifstream - input //

    input >> letter1 >> letter2; // input is reading each character as letter1 then letter2 //

    while (input >> storage) { // Placeholder for characters being read //

        cout << "P: " << storage << '\t'; // Outputs the storage for P being held //
        input >> storage; // Continues to read input //
        cout << "Q: " << storage << endl; // Outputs the storage for Q being held //

        if ('T' && 'F') {
            // P and Q //
            cout << "P and Q: " << 'F' << ' ';
            //input >> storage;
        }
        else if ('F' && 'T') {
            cout << "P and Q: " << 'F' << ' ';
        }
        else if ('F' && 'F') {
            cout << "P and Q: " << 'F' << ' ';
        }
        else 
            cout << "P and Q: " << 'T' << ' ';


        if ('T' || 'T') {
            // P or Q //
            cout << "P or Q: " << 'T' << ' ';
        }
        else if ('T' || 'F') {
            cout << "P or Q: " << 'T' << ' ';
        }
        else if ('F' || 'T') {
            cout << "P or Q: " << 'T' << ' ';
        }
        else
            cout << "P or Q: " << 'F' << ' ';


        if ((!letter1) || (letter1 && letter2)) {
            // If P then Q //
            cout << "P --> Q: " << storage << endl; 
        }
    }

    input.close(); // Close input file //

    system ("PAUSE"); // Black box to appear //

    return 0; // return statement(s) //
}

'F' and 'T' are constants. Something like if('F' && 'T') will always be true. You could just as well write if(true). As I said, what you want to do is to check the truth values that you read, not some constants.

Also note that after you read the second truth value, you no longer have access to the first because you reuse the same variable. This makes it impossible to properly check both truth values, so you should change that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.