Thread: Looping Probrems...

  1. #1

    Angry Looping Probrems...

    I have tried awhile and cannot find out why this won't work.
    Here it is:

    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <conio.h>
    #include <dos.h>

    int col = 0, row = 0 , x = 0, y = 0;
    void cls()
    {
    COORD coordScreen = { 0, 0 };
    DWORD cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD dwConSize;
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    GetConsoleScreenBufferInfo(hConsole, &csbi);
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
    SetConsoleCursorPosition(hConsole, coordScreen);
    }

    char t[3][3];

    void DisplayTable() {
    cls();
    cout << "Table:\n";
    for (col = 1; col <= 3; col++) {
    for (row = 1; row <= 3; row++) {
    if (t[row][col] == '1') {
    cout << "X";
    }
    if (t[row][col] == '2') {
    cout << "O";
    }
    if (t[row][col] == NULL) {
    cout << "-";
    }
    }
    cout << endl;
    }
    }
    void NewTable() {
    for (col = 1; col <= 3; col++) {
    for (row = 1; row <= 3; row++) {
    t[row][col] = NULL;
    row++;
    }
    col++;
    }
    }

    void main() {
    NewTable();
    cout << "Tic Tac Toe\nBy: Scott A. Hand\nEnter X: ";
    cin >> x;
    cout << "Enter Y: ";
    cin >> y;
    t[x][y] = '1';
    DisplayTable();
    }

    It's supposed to look like this if I say 2, 2:

    ---
    -X-
    ---,

    but it looks like this:

    ---
    -X-
    --

    I can't figure it out! Thanks in advance.
    -Save the whales. Collect the whole set.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    big hint on how to fix your code.....

    C++ counts from zero in arrays..... so your arrays should loop from 0 to 2 not 1 to 3!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3

    Thumbs down Tried it...

    It didn't work!!
    -Save the whales. Collect the whole set.

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Are you sure you modified your for loops to take this into account. Also, if the user enters 2,2 because arrays start from zero you will have to translate this to 1,1 before passing the values to your functions.

    Code:
    #include <iostream.h> 
    #include <string.h> 
    #include <stdlib.h> 
    #include <windows.h> 
    #include <conio.h> 
    #include <dos.h> 
    
    int col = 0, row = 0 , x = 0, y = 0; 
    
    void cls() 
    { 
        COORD coordScreen = { 0, 0 }; 
        DWORD cCharsWritten; 
        CONSOLE_SCREEN_BUFFER_INFO csbi; 
        DWORD dwConSize; 
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); 
    
        GetConsoleScreenBufferInfo(hConsole, &csbi); 
        dwConSize = csbi.dwSize.X * csbi.dwSize.Y; 
        FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten); 
        GetConsoleScreenBufferInfo(hConsole, &csbi); 
        FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten); 
        SetConsoleCursorPosition(hConsole, coordScreen); 
    } 
    
    char t[3][3]; 
    
    void DisplayTable() { 
        cls(); 
        cout << "Table:\n"; 
        for (col = 0; col < 3; col++) { 
            for (row = 0; row < 3; row++) { 
                if (t[row][col] == '1') { 
                    cout << "X"; 
                    } 
                if (t[row][col] == '2') { 
                    cout << "O"; 
                    } 
                if (t[row][col] == NULL) { 
                    cout << "-"; 
                    } 
                } 
                cout << endl; 
            } 
        } 
    
    void NewTable() { 
        for (col = 0; col < 3; col++) { 
            for (row = 0; row < 3; row++) { 
                t[row][col] = NULL; 
                row++; 
                } 
            col++; 
            } 
        } 
    
    int  main() { 
        NewTable(); 
        cout << "Tic Tac Toe\nBy: Scott A. Hand\nEnter X: "; 
        cin >> x; 
        cout << "Enter Y: "; 
        cin >> y; 
        t[x-1][y-1] = '1'; 
        DisplayTable();
    
        return 0; 
    } 
    

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 01:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 06:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 01:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:12 AM