Clear screen when user press ENTER

Hi, and thanks in advance for the help.

I found this code in a begginers practice page (http://informaticos53.webcindario.com/programacion.htm) and I can´t figure out how it works:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

int main(){

char c;
char Espera;

printf("Welcome to the writing program in C++.\n\nIf you press + followed by ENTER, you will erase all you´ve written.\nPress - if you want to finish your session.\n\n");

while (c!='-'){
c=getch();
printf("%c",c);

if(c=='+'){
scanf("%c",&Espera);
system("cls");}}}

My particular question is:
How does the program clear the screen only when the user inputs '+' followed by Enter? I can see that the sentence if tells the program to scanf (whathever caracter may come next, and assign it to variable Espera) But, how does it manage to produce different results depending on the vrey following key?

Does the key ENTER (RETURN, INTRO) have a particular effect on the code sequence?
Thank you very much...



Last edited on
1
2
3
4
5
6
7
8
9
10
11
while (c!='-')
{
     c=getch();
     printf("%c",c);

     if(c=='+')
     {
          scanf("%c",&Espera);
          system("cls");
     }
}

Cleaned it up for you.


The way the system works is that when you press Enter, whatever you have typed is sent to the program. So if you type '+' then press Enter, the program reads just the '+' and runs the if-block, clearing the screen.
Thank you so much mukomo.
I found several other examples in the same page and all cleared the screen the same way.
I understand now that Enter is sort of a way of making the code go to the next line.
Thanks again for your quick answer.
But, how does it manage to produce different results depending on the vrey following key?

It doesn't depend on the following key exactly.

try this:
1
2
3
4
5
6
7
8
9
10
11
    while (c!='-')
    {
        c=getch();
        printf("[%c]",c);

        if (c=='+')
        {
            scanf("%c",&Espera);
            system("cls");
        }
    }


Look at the output displayed on the screen before and after you press the plus key:
[q][w][e][r][t][y][+]asdfg


Notice, the letters qwerty+ are output by the printf statement, but the letters asdfg are output during the processing of the scanf statement.

Thus, before pressing '+', the program goes round and around the while loop. But after typing '+', the program is trapped at the scanf statement until you press enter.
Last edited on
Thank you Chervil, I did try what you suggested and now I have a better comprehension of what´s going on here. Very clever of you changing %c to [%c] in the while loop, to better track it. Nice trick, i´ll shurely use it again...

Yet my doubt is still another...

The last hour I´ve been trying to start a simple "guess my number" game with the usual "Press INTRO to continue" after explaining the rules...

How should I apply this method
scanf("%c",&Espera);
to any situation?

I´m used to read users keyboard inputs whith cin >> variable;, and I can´t seem to find the way... But I guess that´s the question for a new topic.
Last edited on
Topic archived. No new replies allowed.