strcmp

This is a discussion on strcmp within the C++ Programming forums, part of the General Programming Boards category; I've been trying to get my little program working all day now. My main problem is that I can't get ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    1

    strcmp

    I've been trying to get my little program working all day now. My main problem is that I can't get strcmp to realize that "O" is equal to "O" and not equal to "X". Here's my code:

    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
    	int num1;
    	int num2;
    	int xando[3][3];
    	char xoro[2];
    	xando[0][0] = 0;
    	xando[0][1] = 0;
    	xando[0][2] = 0;
    	xando[1][0] = 0;
    	xando[1][1] = 0;
    	xando[1][2] = 0;
    	xando[2][0] = 0;
    	xando[2][1] = 0;
    	xando[2][2] = 0;
    	while ((xando[0][0] == 0) || (xando[0][1] == 0) || (xando[0][2] == 0) || (xando[1][0] == 0) || (xando[1][1] == 0) || (xando[1][2] == 0) || (xando[2][0] == 0) || (xando[2][1] == 0) || (xando[2][2] == 0)) {
    		cout<<"\n "<<xando[0][0]<<" "<<xando[0][1]<<" "<<xando[0][2]<<"\n";
    		cout<<" "<<xando[1][0]<<" "<<xando[1][1]<<" "<<xando[1][2]<<"\n";
    		cout<<" "<<xando[2][0]<<" "<<xando[2][1]<<" "<<xando[2][2]<<"\n";
    		cout<<"Enter row number, use 0-2.\n";
    		cin>> num1;
    		cout<<"Enter column number, use 0-2.\n";
    		cin>> num2;
    		cout<<"Now pick X or O.\n";
    		cin.getline (xoro, 2, '\n');
    		cin.get();
    		if ((num1 == 0) && (num2 == 0)) {
    			if (strcmp(xoro,"O")) {
    				xando[0][0] = 1;
    			}
    			else if (strcmp(xoro,"X")) {
    				xando[0][0] = 2;
    			}
    		}
    		else if ((num1 == 0) && (num2 == 1)) {
    			if (strcmp(xoro,"O")) {
    				xando[0][1] = 1;
    			}
    			else if (strcmp(xoro,"X")) {
    		       xando[0][1] = 2;
    			}
    		}
    		else if ((num1 == 0) && (num2 == 2)) {
    			if (strcmp(xoro,"O")) {
    				xando[0][2] = 1;
    			}
    			else if (strcmp(xoro,"X")) {
    		       xando[0][2] = 2;
    			}
    		}
    		else if ((num1 == 1) && (num2 == 0)) {
    			if (strcmp(xoro,"O")) {
    				xando[1][0] = 1;
    			}
    		else if (strcmp(xoro,"X")) {
    				xando[1][0] = 2;
    			}
    		}
    		else if ((num1 == 1) && (num2 == 1)) {
    			if (strcmp(xoro,"O")) {
    				xando[1][1] = 1;
    			}
    			else if (strcmp(xoro,"X")) {
    				xando[1][1] = 2;
    			}
    		}
    		else if ((num1 == 1) && (num2 == 2)) {
    			if (strcmp(xoro,"O")) {
    				xando[1][2] = 1;
    			}
    			else if (strcmp(xoro,"X")) {
    				xando[1][2] = 2;
    			}
    		}
    		else if ((num1 == 2) && (num2 == 0)) {
    			if (strcmp(xoro,"O")) {
    				xando[2][0] = 1;
    			}
    			else if (strcmp(xoro,"X")) {
    				xando[2][0] = 2;
    			}
    		}
    		else if ((num1 == 2) && (num2 == 1)) {
    			if (strcmp(xoro,"O")) {
    				xando[2][1] = 1;
    			}
    			else if (strcmp(xoro,"X")) {
    				xando[2][1] = 2;
    			}
    		}
    		else if ((num1 == 2) && (num2 == 2)) {
    			if (strcmp(xoro,"O")) {
    				xando[2][2] = 1;
    			}
    		else if (strcmp(xoro,"X")) {
    				xando[2][2] = 2;
    			}
    		}
    		else {
    			cout<<"Your number is too high!\n";
    			cin.get();
    		}
    	}
    }
    It's supposed to put a "1" into "xando[0][0]" if you enter a "O" or a "2" if "X" is entered. Would appreciate any help with this.

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    strcmp returns 0 if the strings are the same, and non-zero if they are different. Use != 0 instead of == 0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 06:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 05:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 01:07 PM
  4. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 08:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21