Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Help!

  1. #1
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help!

    g'day peoples,
    im new to these forums and to the coding world so be nice.
    using c++ here, ive got to draw a grid 9x9 and place a star in the middle. I can draw the grid, but the bloody star keeps appearing at the bottom, heres my code, thx for the help.


    -----

    #include <iostream.h>

    void main ()
    {
    int column=0;
    int row=0;
    int x=5;
    int y=5;

    for (column=0; column <9;column ++)
    {int column=0;
    for (row=0; row <19; row ++)
    cout <<"-"; cout << endl;
    for (row=0; row <10; row++)
    cout << "| ";
    cout << endl;}
    for (row=0; row <19; row ++)
    cout <<"-"; cout << endl;

    for (int row=0; row <19; ++row)
    {cout <<endl;
    for (int column=0; column <10; ++column)
    if ((column==x)&&(row==y))
    cout <<"?";
    else
    cout <<"*";
    }
    return;
    }

  • #2
    Regular Coder
    Join Date
    Feb 2004
    Location
    WA
    Posts
    213
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That is happening because you already drew the grid. There are two ways to deal with this.

    - print the asterisk while you are making the grid. start drawing the grid and when you detect that you are at the middle of the grid, draw the asterisk and continue with the grid.

    - print the asterisk by using console gotoxy functions.

    the first method is very portable in the sense that you can even redirect the output the printer and the asterisk would still be in the middle of the grid.

    btw, your code is very confusing..

    for example
    Code:
    for (column=0; column <9;column ++) 
    {int column=0;
    It is a very bad programming practice to use overlapping scopes for variables. besides you are not even using that variable inside the loop!

    Try to implement my first suggestion. You will need to go with the second method if you are writing something interactive like a game.
    Nobody is Perfect. I am Nobody.

  • #3
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    ^ This looks very much like a homework assignment. Professors like to give this type of problem out as homework.

    wargis,

    In the future you need to read our posting guidelines regarding proper titles of threads.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #4
    Regular Coder
    Join Date
    Feb 2004
    Location
    WA
    Posts
    213
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's the reason I tried to point him in the right direction and not feed him with full blown code
    Nobody is Perfect. I am Nobody.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •