Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a newline using \b

Tags:

c++

c

I want the output of the following code as

-|-2-|-

(When i input the value of key as 2). I know \b just move the cursor to 1 space back but why it isn't able to move one space back after a newline is used to input the value of key. Is it possible to use the escape character \b after a newline character has been used.

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

int main()
{
    int key;
    printf("Enter value of key: )"
    printf("-|-");
    scanf("%d",&key);
    printf("\b-|-");
    return 0;
}
like image 970
APan Avatar asked Feb 01 '26 10:02

APan


1 Answers

Here is a simple solution :

#include<stdio.h>

int main()
{
   int key;
   char output[8] = {'\0'};

   printf("Enter value of key: )";
   scanf("%d",&key);
   sprintf(output,"-|-%d-|-",key);
   printf("%s\r\n",output);

   return 0;
}
like image 109
Joze Avatar answered Feb 03 '26 03:02

Joze