Thread: Someone explain me why this isn't working!

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    305

    Question Someone explain me why this isn't working!

    This program is supposed to put a "%" on the screen then move it around when the user presses keys.
    a: left
    s: right
    w: up
    z: down
    It doesn't work. The program just puts another "%" right next to the other one.

    #include <iostream.h>
    #include <conio.h>

    char key;
    int x = 35, y = 18;

    int main(){
    key = getch();

    for(;;){
    gotoxy(x,y);
    cout << "%";

    getch();
    switch(key){
    case 'a':
    x--;
    break;
    case 's':
    x++;
    break;
    case 'w':
    y--;
    break;
    case 'z':
    y++;
    break;
    }
    }
    }

  2. #2
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    You have to erase the old % by printing out a space right after the input but before you change x or y.

  3. #3
    BubbleMan
    Guest

    Post Try..

    In the switch statement, right before x--, x++, y--, or y++, put cout << "\b";

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    well I geuss it doesn't display much at all but if you keep pressing enough keys it will draw a pattern of %'s

    a couple of problems....

    1) you dont erase the old %

    2) cout is buffered...... That means that nothing is written to the screen until cout's internal buffer is full. You need to flush the buffer like this....

    cout << "%"<<flush;
    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

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    305

    Angry New code but still the same problem without % being repeated

    #include <iostream.h>
    #include <conio.h>

    char key;
    int x = 1, y = 1;

    int main(){
    key = getch();

    for(;;){
    clrscr();
    gotoxy(x,y);
    cout << "%" << flush;

    getch();
    switch(key){
    case 'a':
    cout << '\b'; --x;
    break;
    case 's':
    cout << '\b'; ++x;
    break;
    case 'w':
    cout << '\b'; --y;
    break;
    case 'z':
    cout << '\b'; ++y;
    break;
    }
    }
    }

  6. #6
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    id you problem that each time the "%" moves the it remains in the previous position and creates a new one also?

    have you tried clearing the screen?

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    #include <iostream.h>
    #include <conio.h>

    char key;
    int x = 1, y = 1;

    int main(){
    clrscr();
    for(;;){

    gotoxy(x,y);
    cout << "%" << flush;

    key=getch();
    switch(key){
    case 'a':
    cout << '\b'; --x;
    break;
    case 's':
    cout << '\b'; ++x;
    break;
    case 'w':
    cout << '\b'; --y;
    break;
    case 'z':
    cout << '\b'; ++y;
    break;
    }
    }
    }
    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

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    305

    Talking it worked!!! but a new question

    How do i get rid of the annoying little _?

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    if you are using a console and not dos then read this.....

    SetConsoleCursorInfo
    The SetConsoleCursorInfo function sets the size and visibility of the cursor for the specified console screen buffer.

    BOOL SetConsoleCursorInfo(
    HANDLE hConsoleOutput, // handle to console screen buffer
    CONST CONSOLE_CURSOR_INFO *lpConsoleCursorInfo
    // address of cursor information
    );

    Parameters
    hConsoleOutput
    Handle to a console screen buffer. The handle must have GENERIC_WRITE access.
    lpConsoleCursorInfo
    Pointer to a CONSOLE_CURSOR_INFO structure containing the new specifications for the screen buffer's cursor.
    Return Values
    If the function succeeds, the return value is nonzero.

    If the function fails, the return value is zero. To get extended error information, call GetLastError.

    Remarks
    When a screen buffer's cursor is visible, its appearance can vary, ranging from completely filling a character cell to showing up as a horizontal line at the bottom of the cell. The dwSize member of the CONSOLE_CURSOR_INFO structure specifies the percentage of a character cell that is filled by the cursor. If this member is less than 1 or greater than 100, SetConsoleCursorInfo fails.

    CONSOLE_CURSOR_INFO
    The CONSOLE_CURSOR_INFO structure contains information about the console cursor.

    typedef struct _CONSOLE_CURSOR_INFO { // cci
    DWORD dwSize;
    BOOL bVisible;
    } CONSOLE_CURSOR_INFO, *PCONSOLE_CURSOR_INFO;

    Members
    dwSize
    Specifies a number between 1 and 100, indicating the percentage of the character cell that is filled by the cursor. The cursor appearance varies, ranging from completely filling the cell to showing up as a horizontal line at the bottom of the cell.
    bVisible
    Specifies the visibility of the cursor. If the cursor is visible, this member is TRUE.
    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

  10. #10
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    you mean the Cursor?

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    Yeah a cursor.

  12. #12
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    wel if ya do heres how do make the console cursor dissapear

    Code:
    CONSOLE_CURSOR_INFO cci;
    
    cci.bVisible = FALSE;
    
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    
    SetConsoleCursorInfo(hConsole,&cci);
    that should do it.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  13. #13
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    didn't even see your reply stoned simultanious posts what are the odds?
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  14. #14
    Registered User
    Join Date
    Sep 2001
    Posts
    305

    Exclamation

    It don't work!

  15. #15
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    you mean the cursor stays or do you get compiler errors?

    see if SetConsoleCursorInfo(hConsole,&cci); returns true.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C question explain please..
    By code_guru in forum C Programming
    Replies: 3
    Last Post: 11-04-2005, 02:54 AM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM