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?

1
  • 2
    fgets doesn't skip '\n', thats your work Commented Mar 5, 2013 at 9:48

3 Answers 3

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; } 
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the reply! but then how do you remove newline character at the end?
Can anyone come up with a better way to remove it than: first_name[strcspn(first_name, "\n")] = '\0';?
@modifiablelvalue - I have not used that function in my reply?
All good :) I shall delete that unnecessary correction, now.
@ProgrammingNerd - Yes they are the same. Just a matter of preference.
|
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

7 Comments

If the last character isn't '\n', this has the unfortunate side-effect of erasing a significant character.
@modifiablelvalue I know, I only want to give a hint to OP
Thanks. This definitely worked :)
But why is '\n' at the end of the string as I am entering in my name?
(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.
|
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"); 

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.