4
int main(int argc, char** argv)
{
//Local Declaration
char last_name[20];
char first_name[20];
char phone_number[20];
char address[30];

//Statement
printf("Enter your last name: ");
fgets(last_name, 20, stdin);

printf("Enter your first name: ");
fgets(first_name, 20, stdin);

printf("Enter your phone number: ");
fgets(phone_number, 20, stdin);

printf("Enter your address: ");
fgets(address, 30, stdin);

printf("=====Address book=====\n");
printf("Name: %s%s\n", first_name, last_name);
printf("Phone Number: %s\n", phone_number);
printf("Address: %s\n", address);
return (EXIT_SUCCESS);
}

The result doesn't come out as I expected... I meant the first name and last name to be in one line (e.g. Mark Zuckerberg). But it comes out like this

Mark

Zuckerberg

What is wrong here? Why is there a new line in between?

slow
  • 805
  • 3
  • 13
  • 27

3 Answers3

4

See manual page

Quote:

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte (aq\0aq) is stored after the last character in the buffer.

So the string read by fgets includes the new line character at the end. You will need to remove it.

EDIT

To remove end of line (and allow for DOS) do

int end = strlen(first_name) - 1;
if (end >= 0 && '\n' == first_name[end])
{
   first_name[end--] = 0;
   if (end >= 0 && '\r' == first_name[end]) first_name[end] = 0;
}
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • Thanks for the reply! but then how do you remove newline character at the end? – slow Mar 05 '13 at 09:59
  • Can anyone come up with a better way to remove it than: `first_name[strcspn(first_name, "\n")] = '\0';`? – autistic Mar 05 '13 at 10:02
  • @modifiablelvalue - I have not used that function in my reply? – Ed Heal Mar 05 '13 at 10:09
  • All good :) I shall delete that unnecessary correction, now. – autistic Mar 05 '13 at 10:21
  • Another simple question for last, do 0 and '\0' the same job? Because I tried both and they all worked just the same. – slow Mar 05 '13 at 10:30
  • 1
    @ProgrammingNerd - Yes they are the same. Just a matter of preference. – Ed Heal Mar 05 '13 at 10:31
  • @ProgrammingNerd Just to explain why they both work, the NUL character can be represented by any expression that evaluates to zero. The first is plain old zero in decimal whilst the second is the octal escape sequence with a single octal digit of 0. You could even use `\x0` (hexadecimal escape with zero) if you really wanted to. – Metabble Mar 05 '13 at 14:09
1

As suggested by Ed, see manual, a simple way to replace '\n' by ' ' (simple space) could be:

first_name[strlen(first_name) - 1] = ' ';

strlen uses string.h, don't forget to include it

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • If the last character isn't `'\n'`, this has the unfortunate side-effect of erasing a significant character. – autistic Mar 05 '13 at 10:04
  • @modifiablelvalue I know, I only want to give a hint to OP – David Ranieri Mar 05 '13 at 10:06
  • Thanks. This definitely worked :) – slow Mar 05 '13 at 10:07
  • But why is '\n' at the end of the string as I am entering in my name? – slow Mar 05 '13 at 10:09
  • @ProgrammingNerd, you are welcome, see modifiablelvalue comment and take care that `len - 1 == '\n'` – David Ranieri Mar 05 '13 at 10:10
  • 1
    (from the man page quoted by Ed) reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte (aq\0aq) is stored after the last character in the buffer. – David Ranieri Mar 05 '13 at 10:12
  • Suppose EOF were encountered before the first byte read. What would `strlen(first_name) - 1` evaluate to? Supposing that evaluates to some negative number, wouldn't `first_name[strlen(first_name) - 1]` then be undefined behaviour? – autistic Mar 05 '13 at 13:18
0

fgets() , will store '\n' in variable first_name, when you hit enter , after you type Mark , so the string stored in first_name is "Mark\n" , printf() will just do its job and print the newline character.

Another good alternative is to use fscanf()

fscanf (stdin, "%s", first_name);

EDIT:

To check for fscanf() errors.

char str[50];
int bytes = -1;
fscanf (stdin, "%s%n",str,&bytes);
 if(bytes == -1)
    perror("\nIncomplete Bytes Parsed\n");
Barath Ravikumar
  • 5,658
  • 3
  • 23
  • 39
  • I like this. It's unfortunate you didn't delve into the return value of fscanf, though. If you could show the OP how to ensure that fscanf was successful, you'd earn a +vote from me. – autistic Mar 05 '13 at 13:23