Need help with char type usage

I am at just the beginning of becoming a master programmer whatever it takes ! I am an ambitious person and in my path to becoming one of the best stands this giant stone. I am trying to make a little X & O game but at the reading X or O part of the program it seems to be stuck.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int main()
{
    char a[4][4],y;          ///y is for X or O and the matrix is the 3*3 table
    int i,j;
    for(i=1;i<=3;++i)       ///to anyone wondering, i like to start from 1 to n+1 and not from 0 to n
    {
        for(j=1;j<=3;++j) a[i][j]='*';
    }
    while(3)
    {
        do
        {
            cin>>y;
        }while(y!='X' || y!='O');
        cout<<"it will pass";
    }
    for(i=1;i<=3;++i)
    {
        cout<<endl<<endl;
        for(j=1;j<=3;++j) cout<<a[i][j]<<"   ";
    }
}




I put the "it will pass" message to notify me whether or not the reading y repetitive instruction will end or not but whatever i input, that message will not show up.

As some sort of disclaimer, dont get mad at me for making a big mistake with the char type. I haven't studied it yet in school but i like to work ahead of things i learn in school.
Also sorry for any spelling or grammar mistakes its very late and im exhausted because of trying to solve this problem myslef.
The do-while loop ends when this comes out as false:
while(y!='X' || y!='O');


When will this ever, ever be false? Can you think of a value for y in which (y!='X' || y!='O') is false? I can't.
Last edited on
to anyone wondering, i like to start from 1 to n+1 and not from 0 to n

If you refuse to use the language properly, you will never master it.

BTW. what is your first language?
Also, this:

while(3)

This is an infinite loop. Is it meant to be an infinite loop?
Welcome to the forum!

Line 7: Nice job commenting your code! This is so important and so few people do it.
Lines 7 & 9: while you may like going from 1 to n+1, I urge you to get used to going from 0 to n. That's the way that arrays are indexed so if you do it differently, you have to make all of your arrays 1 item larger than needed. It also prevents you from using range-based for loops (a sort of "for each item" construct).

Line 13: while (3)? why 3? It would be better to say while(true) Your goal is to make the code as clear as possible. In either case though, you've created an infinite loop so the program will never get out.

Line 18: Here is your problem. You're mistaking how boolean logic works. Suppose y is 'X':
- y!='X' is false, because y IS 'X'
- y!='O' is true because y isn't 'O'
- false || true is true, by definition
What you really want is y!='X' && y!='O'

Also sorry for any spelling or grammar mistakes
They are both fine.
im exhausted because of trying to solve this problem myslef.

I think the best piece of advice I can give a new programmer is this: if you get stuck for more than 30 minutes, seek out help. The answer is almost always something very simple and your time is better spent doing something fun while you wait for an answer.
Also, this:

while(3)

This is an infinite loop. Is it meant to be an infinite loop?


Yes. As some point there will be a return 0. This is just a small part of the program on which i was stuck.
If you refuse to use the language properly, you will never master it.

BTW. what is your first language?


You mean programming language ? If not, romanian.
As a final reply

Thanks everybody for the little bits of help and dhayden for explaining the problem. Hope you guys have a great day !
Topic archived. No new replies allowed.