473,320 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Help with a tic tac toe function

This is the program I have written to display 10 tictactoe fields but using 10 strings input by a 2d array. For some reason with mine I'm getting the wrong data output. Could someone take a look at my tttfunction() and tell me if I screwed it up rather badly or not? (I'm struggling my way along through this one).
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<iostream>
  3. using std::cin;
  4. using std::cout;
  5. #include<iomanip>
  6. using std::string;
  7. using std::setw;
  8. void tttFunction();
  9. char tttArray[9];
  10. using std::cerr;
  11. int arraySize = 9;
  12. int k = 0;
  13. int i;
  14. char test[][10]={"---------",
  15.                 "x--------", 
  16.                 "o--------", 
  17.                 "xo-------", 
  18.                 "ooooooooo", 
  19.                 "xxxxxxxxx", 
  20.                 "xo--oxxo-",
  21.                 "---xxxooo", 
  22.                 "o-xo-xo-x", 
  23.                 "x-o-x-o-x"};
  24.  
  25.  
  26.  
  27. int main() {
  28.     cout << " The computer will now generate Tic Tac Toe boards \n based on the character strings provided..."
  29.         << "\n Please Wait";
  30.     for (int k = 0; k < 10; k++) { // Will run the tttFunction() 10 times
  31. cerr << test[9][9]; // error test to make sure array is functioning
  32.     tttFunction(); // Function that copies the 2d array into the one d array and displays the data
  33.     }
  34.  
  35.     return 0;
  36. }
  37.  
  38.  
  39.  
  40. // ------------------
  41. // tttFunction()
  42.  
  43. void tttFunction() {
  44.  
  45.     for ( i; i < 9; i++)
  46.  
  47.         tttArray[i] = test[i][k]; 
  48.  
  49.         if (i = 10 ) {
  50.             k++;
  51.             i = 0;
  52.  
  53.         }
  54.  
  55.  
  56.     cout << "\n\n" << setw(2) << tttArray[0] << setw(2) << "|" << setw(2) << tttArray[1] << setw(2) << "|"
  57.          << setw(2) << tttArray[2] << "\n";
  58.     cout <<  "---+---+---" << "\n";
  59.     cout << setw(2) << tttArray[3] << setw(2) << "|" << setw(2) << tttArray[4] << setw(2) << "|" 
  60.          << setw(2) << tttArray[5] << "\n";
  61.     cout <<  "---+---+---" << "\n";
  62.     cout << setw(2) << tttArray[6] << setw(2) << "|" << setw(2) << tttArray[7] << setw(2) << "|"
  63.          << setw(2) << tttArray[8] << "\n" << "\n\n";
  64. while (k = 10)
  65. break;
  66.  
  67. }
Feb 28 '07 #1
1 1648
horace1
1,510 Expert 1GB
you have a global variable k but your loop in main() uses a local version
Expand|Select|Wrap|Line Numbers
  1.     for (int k = 0; k < 10; k++) { // Will run the tttFunction() 10 times
  2.  
try
Expand|Select|Wrap|Line Numbers
  1.     for (k = 0; k < 10; k++) { // Will run the tttFunction() 10 times
  2.  
also your loop in tttFunction()
Expand|Select|Wrap|Line Numbers
  1.  
  2.     for ( i; i < 9; i++)
  3.  
  4.         tttArray[i] = test[i][k]; 
  5.  
  6.         if (i = 10 ) {
  7.             k++;
  8.             i = 0;    
  9.         }
  10.  
try
Expand|Select|Wrap|Line Numbers
  1.     for ( i=0; i < 9; i++)
  2.  
  3.         tttArray[i] = test[k][i];// test[i][k]; 
  4.  
in general try to avoid using global variables - keeping track of which functions use them and change them is very difficult - pass data into functions using parameters
Feb 28 '07 #2

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

Similar topics

1
by: Edward King | last post by:
....I should have mentioned that I need to call the help function from a hyperlink... but <a href="<? gethelp(1) ?>">Get Help</a> Doesnt work.... EK
2
by: Smogo | last post by:
Hi, I'm new to JavaScript so forgive me if the answer to this question is (a) ridiculously obvious, or (b) beyond the realms of JavaScript.... Is it possible to create URLs from function call...
11
by: James Aguilar | last post by:
In the C++ STL, there are various places where you can call a function that takes a functor as a parameter. This functor may be either a function object or a function pointer. For instance, if...
2
by: Chuck Martin | last post by:
I am having a most frustrating problem that references, web searches, and other resources are no help so far in solving. Basically, I'm trying to design a pop-up window to be called with a funciton...
5
by: emanuela | last post by:
Hi, I've a little question about C language: according to the pthread_create signature: int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void...
4
by: Raymond | last post by:
Hello All: I have find a function declaration like following: ------------------------------------------------ extern int _ts_callback_abort _((GTRID *, long));...
6
by: coolpint | last post by:
I am yet again confused about the process of overload resolution. Can anyone kindly explain why the function #2 is selected over #1 when passed 'char'? When passed 'const char', the compiler...
3
by: Ara Kooser | last post by:
Hello all, I hope I am posting this correctly. I am running Python 2.4.2 on Slackware 11.0 using IDLE. I am in the process of learning python so I am writing a text adventure game using...
12
by: ross m. greenberg | last post by:
I'm trying to create a function in JavaScript using Dreamweaver8 such that when a user hits the ' F1' key when a text box is selected a separate "pop-up" window will open, with text of my choice. ...
1
by: mustafalp | last post by:
i want to write it with following prototype: int occurence (int a,intasize,int e) this function must returns the number of occurence of e in the array a in c++.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.