Jump to content

C++ Help with a Code

Go to solution Solved by 56KBs,

Hi ichihgo,

 

You've got a problem with your logic if you're trying to detect when it is not X, O or ., in the case of your code:

if(x[i][j] != 'X' || x[i][j] != 'O' || x[i][j] != '.')

You're doing if it is not X or it is not O or it is not ., what you want to be doing is if it is not X and it isn't O and it isn't . then flag as illegal, what you want is:

if(x[i][j] != 'X' && x[i][j] != 'O' && x[i][j] != '.')

Hope this helps

I try to make the code deticed wether there's an input which is not (X,O,".") but it always ake the condition true

 

Using array

#include <iostream>#include<string>using namespace std;int main ( ){    bool illegal=false;char x[3][3];for (int i=0;i<3;i++){    for (int j=0;j<3;j++)    {        cin>>x[i][j];    }}for (int i=0;i<3;i++){    for (int j=0;j<3;j++)    {        if(x[i][j] != 'X' || x[i][j] != 'O' || x[i][j] != '.')        {            illegal=true;            break;        }    }    if(illegal==true)        break;}if(illegal==true)    cout<<"NO";else    cout<<"YES";return 0;}

Using Strings

	#include<iostream>#include<string>using namespace std;int main(){    bool illegal=false;string x1,x2,x3;getline(cin,x1);getline(cin,x2);getline(cin,x3);for (int i=0;i<3;i++){    if (x1.at(i)!= 'X' || x1.at(i)!= 'O' || x1.at(i)!= '.')    {         illegal=true;         break;    }}for (int i=0;i<3;i++){    if (x2.at(i)!= 'X' || x2.at(i)!= 'O' || x2.at(i)!= '.')    {         illegal=true;         break;    }}for (int i=0;i<3;i++){    if (x3.at(i)!= 'X' || x3.at(i)!= 'O' || x3.at(i)!= '.')    {         illegal=true;         break;    }}if(illegal==true){    cout<<"unillegal";}else if(illegal==false)    cout<<"Ok";    return 0;}
Link to comment
https://linustechtips.com/topic/281446-c-help-with-a-code/
Share on other sites

Link to post
Share on other sites

Hi ichihgo,

 

You've got a problem with your logic if you're trying to detect when it is not X, O or ., in the case of your code:

if(x[i][j] != 'X' || x[i][j] != 'O' || x[i][j] != '.')

You're doing if it is not X or it is not O or it is not ., what you want to be doing is if it is not X and it isn't O and it isn't . then flag as illegal, what you want is:

if(x[i][j] != 'X' && x[i][j] != 'O' && x[i][j] != '.')

Hope this helps

Link to comment
https://linustechtips.com/topic/281446-c-help-with-a-code/#findComment-3821190
Share on other sites

Link to post
Share on other sites

Hi ichihgo,

 

You've got a problem with your logic if you're trying to detect when it is not X, O or ., in the case of your code:

if(x[i][j] != 'X' || x[i][j] != 'O' || x[i][j] != '.')

You're doing if it is not X or it is not O or it is not ., what you want to be doing is if it is not X and it isn't O and it isn't . then flag as illegal, what you want is:

if(x[i][j] != 'X' && x[i][j] != 'O' && x[i][j] != '.')

Hope this helps

 

I kinda feel sorry for myself I stayed an hour trying to figure it out :3

 

Thanks :)

Link to comment
https://linustechtips.com/topic/281446-c-help-with-a-code/#findComment-3821228
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×