CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2010
    Posts
    2

    Console Adds A \n Automatically When Enter Is Pressed

    Hello, just joined this forum (: sorry if this is posted but i have searched google everywhere and can not seem to resolve this problem. My program is a MasterMind game, where you have to guess 4 colours in the correct order and correct colour to win. Ill just post the one function im trying to fiddle with as it is quite long unless you want me to (:.

    The Code:

    void getGuess(char *guess, int n)
    {
    int valid;
    int c;
    cout << "\n";
    for(c=0; c<n; c++)
    {
    do
    {
    cout << "Please Enter Guess " << c+1;
    gotoxy(20,19);
    cin >> (guess[c]);
    guess[c] = toupper(guess[c]);
    switch(guess[c])
    {
    case 'R':
    case 'G':
    case 'C':
    case 'M':
    case 'B':
    case 'Y':
    valid = 0;
    break;
    default:
    valid = 1;
    printf("Please Re-Enter either 'R' 'G' 'C' 'M' 'B' 'Y'"); break;
    }

    }while(valid !=0);
    }


    }

    When i dont use the 'gotoxy(20,19);' the output is as follows:

    Please Enter Guess 1
    Please Enter Guess 2
    Please Enter Guess 3
    Please Enter Guess 4

    So it goes on a new line each time i print it but i want the other lines to just replace the second line. So it looks like this:

    Please Enter Guess 1: (When you put a colour and press enter it just replaces the '1' with a '2' etc.

    When i have the 'gotoxy(20,19);' it does work kinda. It replaces the line but i have to have the cursor above the 'Please Enter Guess 1' so it looks like this.

    ***************** m
    Please Enter Guess 1

    Ignore the stars, they are just to move the 'm' or what ever colour is located.
    Which isnt really what i want, i hope all that makes sense, thanks very much in advance (:

  2. #2
    Join Date
    Apr 2009
    Posts
    598

    Re: Console Adds A \n Automatically When Enter Is Pressed

    Yes, in console mode, you cannot move backwards, therefore you cannot replace characters that are already displayed.
    This is inherited from the time when the console was not a screen but a printer.

    A solution is given by the curses and ncurses library, see http://en.wikipedia.org/wiki/Ncurses

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Console Adds A \n Automatically When Enter Is Pressed

    Actually you can kind of overwrite one line in a console by not allowing the line feed. I do not have an example handy and my memory is quite dark in this area since I have not done it for years (rather do GUI stuff these days) but essentially you need to catch the line feed and overwrite the previous line....

  4. #4
    Join Date
    Apr 2010
    Posts
    2

    Smile Re: Console Adds A \n Automatically When Enter Is Pressed

    thanks for the info and quick responses. Ill have a search of catching the line feed, or i might try something else without the need to do this.
    (:

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured