Array won't display new values?

This is a discussion on Array won't display new values? within the C++ Programming forums, part of the General Programming Boards category; I'm trying to make a simple tic-tac-toe program. In the example below, it's incomplete, only I finished it enough to ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    31

    Array won't display new values?

    I'm trying to make a simple tic-tac-toe program. In the example below, it's incomplete, only I finished it enough to run it and check for errors.
    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <time.h>
    
    char playagain='y';
    int number;
    char array[10];
    int main(void)
    {
    	array[1]='1';
    	array[2]='2';
    	array[3]='3';
    	array[4]='4';
    	array[5]='5';
    	array[6]='6';
    	array[7]='7';
    	array[8]='8';
    	array[9]='9';
    	while(playagain=='y')
    	{
    	cout << array[1];
    	cout << "|";
    	cout << array[2];
    	cout << "|";
    	cout << array[3];
    	cout << "\n-----\n";
    	cout << array[4];
    	cout << "|";
    	cout << array[5];
    	cout << "|";
    	cout << array[6];
    	cout << "\n-----\n";
    	cout << array[7];
    	cout << "|";
    	cout << array[8];
    	cout << "|";
    	cout << array[9];
    	cout << "\n";
    	cout << "Please enter the number of the space you would like to place an X in: ";
    	cin >> number;
    	for(int i=1;i<10;i++)
    	{
    	if(number==array[i])
    	{
    	array[i]='X';
    	}
    	}
    	}
    	return 0;
    }
    When I run it, no matter what number I put in, it always displays array[i] (i being whatever number I put in), as the number it originally was, not 'X', as it should be. As you can see, I declare the array numbers outside of while(playagain=='y'), so it shouldn't redefine them after looping. Please help.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,649
    Hint:
    Code:
    char c = '1';
    int n = 1;
    if (c != n)
       cout << "This line will be executed always!" << endl;
    gg

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    I see. So, how can I make this work? I don't want to use multiple variables for each space...Can I convert a char into an int temporarily?

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,649
    Quickest solution:
    Code:
    int number; ==> char number;
    gg

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    array[1]='1';
    array[2]='2';
    array[3]='3';
    array[4]='4';
    array[5]='5';
    array[6]='6';
    array[7]='7';
    array[8]='8';
    array[9]='9';
    Why don't you use a loop, instead writing all this? ahh and one thing, arrays are from 0 to N-1, and not from 1 to N-1. About changing the type of a variable temporary, search for casting, well, I don't know C++, but in C it goes like this:

    Code:
    (typeName) varName;
    this forces varName to be of typeName.
    Last edited by Vber; 03-17-2003 at 05:10 AM.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    I know, but if i=0, that elimintes some much easier coding later on

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    declare number as a char...

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    Yeah, I tried that, but number as a char didn't accept input properly. Anyway, I found somewhere on this board that char i='4', i=i-'0' makes i integer 4.

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <time.h>
    
    char playagain='y';
    char number;
    char array[10];
    int main(void)
    {
    	array[1]='1';
    	array[2]='2';
    	array[3]='3';
    	array[4]='4';
    	array[5]='5';
    	array[6]='6';
    	array[7]='7';
    	array[8]='8';
    	array[9]='9';
    
    	while(playagain=='y')
    	{
    		for (int i = 1; i < 10; i++)
    		{
    			if (i % 3 != 0)
    				cout << array[i] << "|";
    			
    			else 
    				cout << array[i] << "\n-----\n";
    		}
    
    	cout << "\nPlease enter the number of the space you would like to place an X in: ";
    	cin >> number;
    
    	cout << endl;
    
    	for(i=1;i<10;i++)
    	{
    	if(number==array[i])
    	{
    	array[i]='X';
    	}
    	}
    	}
    	return 0;
    }
    I tried that in 3 diff compilers and it worked....Also, cleaned it up alittle.

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    Alright, well, that part's working, now. Please see my other thread if you're interested in helping me with my other problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to 'replicate' an array of hex values?
    By sef0 in forum C Programming
    Replies: 8
    Last Post: 05-27-2009, 08:53 AM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. How to read in empty values into array from input file
    By wpr101 in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2002, 09:59 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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