1

I wrote the following code for a math problem and it won't write into the output file "coada2.out".

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

int main() {
    int sum = 0, x, y, z;

    ifstream file_in("coada2.in");
    file_in >> x >> y >> z;
    file_in.close();

    ofstream file_out;
    file_out.open ("coada2.out"); 
    sum=x+2;
    if((y+1 || z+1) == sum) {
        file_out << sum;
        file_out.close();
    }
    else{
        cout<<"-1";
    }
    return 0;
}
3
  • 3
    if((y+1 || z+1) == sum) { is quite strange to me. It will not do what you want. Commented Nov 24, 2019 at 16:00
  • 1
    Lots of details/information is missing from this question. Like; What operating system are you using? What compiler are you using? What C++ language standard version are you using? What errors are you getting? Commented Nov 24, 2019 at 16:02
  • You can write to the file of z and y are both -1 and x is -2. In that case (-1+1 || -1 +1 ) will be false and this false which will equal sum because its 0. Commented Nov 24, 2019 at 17:48

1 Answer 1

4

The problem is this line:

if((y+1 || z+1) == sum) {

y+1 || z+1 evaluates to either true or false, which means the body of the if statement can only ever be executed, if sum is 0 or 1.

What you probably meant is:

if(y+1==sum || z+1==sum) {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.