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

    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++)
    cout << "Please Enter Guess " << c+1;
    cin >> (guess[c]);
    guess[c] = toupper(guess[c]);
    case 'R':
    case 'G':
    case 'C':
    case 'M':
    case 'B':
    case 'Y':
    valid = 0;
    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

    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

  3. #3
    Join Date
    May 2000
    KY, USA

    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

    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