473,473 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

comparing strings in a tic tac toe game

1 New Member
I am trying to write a test to see if a tic tac toe space is occupied.
char board[3][3];
int row;
int col;
cout << " Enter row";
cin >> row
cout << "Enter col":
board[row][col]=='X';
if(!strcmp(board[col],"O"))
{
cout << "Space already taken";
}
Any ideas please?
Apr 15 '07 #1
2 1212
Savage
1,764 Recognized Expert Top Contributor
I am trying to write a test to see if a tic tac toe space is occupied.
char board[3][3];
int row;
int col;
cout << " Enter row";
cin >> row
cout << "Enter col":
board[row][col]=='X';
if(!strcmp(board[col],"O"))
{
cout << "Space already taken";
}
Any ideas please?
hi,
Why are you checking only for O?

strcmp returns 0 if they match,so you could use like:

Expand|Select|Wrap|Line Numbers
  1. if(strcmp(board[row][col],"O")==0||strcmp(board[row][col],"X")==0)
Savage
Apr 15 '07 #2
Ganon11
3,652 Recognized Expert Specialist
Why are you bothering to use strcmp? strcmp is used when you will have multiple characters forming a string. But what you are doing is comparing individual characters: thus, you can write

Expand|Select|Wrap|Line Numbers
  1. if (board[row][col] == 'X') {
  2.    // ...
  3. }
and will have no problems.
Apr 16 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: beliavsky | last post by:
By mistake I coded something like print ("1" > 1) and got the result "True". Comparing an integer and a string seems meaningless to me, and I would prefer to have an exception thrown. Can...
26
by: William Park | last post by:
How do you compare 2 strings, and determine how much they are "close" to each other? Eg. aqwerty qwertyb are similar to each other, except for first/last char. But, how do I quantify that? ...
5
by: Curtis Gilchrist | last post by:
I am required to read in records from a file and store them in descending order by an customer number, which is a c-style string of length 5. I am storing these records in a linked list. My...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
3
by: Robert Dell | last post by:
I have a problem comparing strings in an order form i'm writing. I want to give a running total at the bottom of the page and it appears to be working except it doesn't compare correctly (it...
88
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm...
2
by: Manny Chohan | last post by:
Hi, i have two datetime values in format 11/22/04 9:00 AM and 11/22/04 9:30 AM. How can i compare dates .net c# or if there is any other way such as Javascript. Thanks Manny
1
by: Zach | last post by:
Greetings, I am writing a routine that will print out the beginning and end strings for a game: String A1 -"Begin msg1" String B1 -"End msg1" String A2 -"Begin msg2" String B2 -"End msg2"
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
4
by: MC felon | last post by:
what's the best way to compare two string elements? std::string cstr; if ( cstr.at(3) == cstr.at(2)) //do something or std::string cstr; if ( cstr == cstr) //do something ?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.