0

I am a student that just recently learn C programming and I encounter some of problem in my assignment this is my code :

#include <stdio.h>
int main()
{
char name[30];
printf("\tEnter Your Name = \n");
gets(name);

char address[100];
printf("\tPlease enter your address = \n");
gets(address);

printf ("\tYour name is = %s\n", name);
printf("\tYour address is = %s", address);

getchar();
return 0;
}

as you can see the it function correctly but I need to put tab at my input so the result should be look like this

 
(tab)  Enter Your Name = 
(tab)  user input
(tab)  Please enter your address = 
(tab)  user input
(tab)  your name is = base from user input
(tab)  your address is = base from user input 

I hope that someone can help and explain to me. Thanks

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 3
    Never ***ever*** use `gets`! It's so [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) that it have even been removed from the C language specification. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude Nov 01 '21 at 11:38
  • 1
    `gets` is depreciated use `fgets` instead – 0___________ Nov 01 '21 at 11:39
  • 1
    simply add tabs where you need them `printf("\tEnter Your Name = \n");` => `printf("\tEnter Your Name = \n\t");` I think that you have copied the code from somewhere. – 0___________ Nov 01 '21 at 11:40
  • Do you want to *print* tabs, or do you want the user to actually input tabs? It's not quite clear. And if you want the user to input tabs, what are the problem you have? Don't you get the tabs in the input? – Some programmer dude Nov 01 '21 at 11:42

1 Answers1

0

I need to put tab at my input so the result should be look like this ...

As input may or may not already include a leading tab, consider conditionally pointing to the beginning of input or to the next character.

char *tabless_name = name;  // form the base
if  (tabless_name[0] == '\t') {
  tabless_name++; 
}
printf ("\tYour name is = %s\n", tabless_name);

Concerning gets(): Why is the gets function so dangerous that it should not be used?.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256