Kumar Kumar - 2 months ago 10
C Question

Update previous line instead print new line

Here is a simple program in c. It take two integers and add them. In this code, I want to update previous line by new line instead making a new one, Can any body help me on this topic.

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

int main()
{
int a,b;
printf("Enter two integer to add\r");
scanf("%d%d",&a,&b);
a=a+b;
printf("Your value is -- %d",a);

return 0;
}


I have use \r instead of \n, to take cursor back to the start. I have input data in program console which writing from start. But the next line "Your value is.." printed as new line, I want to remove the first line and then input value and print next line "Your value..". How do I do that? Please help me

Answer

You cannot move up in the terminal like that, unless using some complex lib as curses. You can use the "clear screen" trick, maybe that would achieve what you want.

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

    int main()
    {
    int a,b;
    system("clear"); // or "cls" on windows
    printf("Enter two integer to add\r");
    scanf("%d%d",&a,&b);
    system("clear"); // or "cls" on windows
    a=a+b;
    printf("Your value is -- %d",a);

    return 0;
    }
Comments