473,885 Members | 2,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with a tic tac toe function

1 New Member
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
  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"};
  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.     }
  35.     return 0;
  36. }
  40. // ------------------
  41. // tttFunction()
  43. void tttFunction() {
  45.     for ( i; i < 9; i++)
  47.         tttArray[i] = test[i][k]; 
  49.         if (i = 10 ) {
  50.             k++;
  51.             i = 0;
  53.         }
  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;
  67. }
Feb 28 '07 #1
1 1662
1,510 Recognized Expert Top Contributor
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
Expand|Select|Wrap|Line Numbers
  1.     for (k = 0; k < 10; k++) { // Will run the tttFunction() 10 times
also your loop in tttFunction()
Expand|Select|Wrap|Line Numbers
  2.     for ( i; i < 9; i++)
  4.         tttArray[i] = test[i][k]; 
  6.         if (i = 10 ) {
  7.             k++;
  8.             i = 0;    
  9.         }
Expand|Select|Wrap|Line Numbers
  1.     for ( i=0; i < 9; i++)
  3.         tttArray[i] = test[k][i];// test[i][k]; 
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

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
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 operators? For example, I'm trying to program an onClick function which will load a webpage in one frame and an image in another, based on the name of the button which is being clicked. So clicking the button with name="help" will open...
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 you have a vector<char> and call for_each on it, both of the methods below would be valid: #include <iostream> #include <vector> using namespace std; struct MyFunctionObject
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 in a link. that function can have parameters for URL and window name passed to it. This works peachy in Firefox (1.0). With IE 6 (6.0.29) on two separate computers, I get an "onject expected" error. Going to the MS-based debugger just tells me...
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 *arg); which syntax is more correct?
by: Raymond | last post by:
Hello All: I have find a function declaration like following: ------------------------------------------------ extern int _ts_callback_abort _((GTRID *, long)); ------------------------------------------------ What's the meaning of this statement? Thank you Raymond shen.
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 complains of ambiguity as I expected. I was expecting to hear the same complaint when passing 'char' because I thought both required 'const qualification', but my compiler (gcc 3.4.2) calls function #2.
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 functions and global variables only. I know there is a better way to do this but that is for later. When I run the python program it works fine until I try to go west from my inital start room. I get the room description but no raw_input prompt. I just...
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. Does anybody have any pointers or even some source code? I use ASP on my server. Thanks! Ross
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++.
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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 we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.