I've read many threads on how to remove a new line character '\n' using fgets()-- however, how would one go about removing a new line character simply by using getchar() ?

Saw this from a web search I did months ago so I'm not exactly sure if it's efficient so I'd like remarks from other members about it

int c;
while ((c = getchar()) != '\n' && c != EOF); //discards the \n stuck in the input stream?

Edited 4 Years Ago by zeroliken: n/a

Saw this from a web search I did months ago so I'm not exactly sure if it's efficient so I'd like remarks from other members about it

int c;
while ((c = getchar()) != '\n' && c != EOF); //discards the \n stuck in the input stream?

Thanks for the quick reply-- but it doesn't seem to be working for me
My problem specifically is I have the following code

printf("How many songs will be entered? ");
	scanf("%d",&num_songs);
	
	while ((c = getchar()) != '\n' && c != EOF);

	for(i = 0; i < num_songs  && (t = fgets(titles[i],sizeof(titles[i]),stdin)) != NULL && (a = fgets(artists[i],sizeof(artists[i]),stdin));  i++)
{	
		
		t = fgets(titles[i],sizeof(titles[i]),stdin);
		trim_newline(titles[i]);

etc...

and for some reason it is taking in a new line as the titles array. I am wondering if this has something to do with my fgets statements in my guard for the for loop? is there a way around this?

is the problem something like fgets getting skipped after receiving an int from the user?

Edited 4 Years Ago by zeroliken: n/a

is the problem something like fgets getting skipped after receiving an int from the user?

Yes, something of the sort. After the user gets prompted for an int (the number of songs), the code essentially skips fgets for title until the user hits a new line.

Example:

How many songs will be entered? 3
'/n'
Little Bit
Lykke Li
3

'/n'
Let's Dance
M. Ward
4

'/n'
Hello
World
2

'/n'

instead of the correct format which is
How many songs will be entered? 3
Little Bit
Lykke Li
3

Let's Dance
M. Ward
4

Hello
World
2

Well I got it to work... try commenting out the while loop and it skips the fgets

#include<stdio.h>
int main(void){
	char name[30];
	int c,r;
	printf("Enter number");
	scanf("%d", &r);

	while ((c = getchar()) != '\n' && c != EOF);

	printf("Enter name");
	fgets(name,sizeof(name),stdin);
        return 0;
}

Edited 4 Years Ago by zeroliken: forgot the return statement

Well I got it to work... try commenting out the while loop and it skips the fgets

#include<stdio.h>
int main(void){
	char name[30];
	int c,r;
	printf("Enter number");
	scanf("%d", &r);

	while ((c = getchar()) != '\n' && c != EOF);

	printf("Enter name");
	fgets(name,sizeof(name),stdin);
        return 0;
}

I tried this code in a separate file and indeed it worked, however when I tried to run my code with your suggestion

printf("How many songs will be entered? ");
scanf("%d",&num_songs);

while ((c = getchar()) != '\n' && c != EOF);

for(i = 0; i < num_songs && (t = fgets(titles[i],sizeof(titles[i]),stdin)) != NULL && (a = fgets(artists[i],sizeof(artists[i]),stdin));  i++)
{	
		
		t = fgets(titles[i],sizeof(titles[i]),stdin);
		trim_newline(titles[i]);
etc...

it still skips fgets for title :( I think it has something to do with the for loop... but I'm not quite sure what it is. I also tried handling the two while statements within my for loop-- but it still doesn't suffice.

printf("How many songs will be entered? ");
scanf("%d",&num_songs);
for(i = 0; i < num_songs && (t = fgets(titles[i],sizeof(titles[i]),stdin)) != NULL && (a = fgets(artists[i],sizeof(artists[i]),stdin));  i++)
{	
	t = fgets(titles[i],sizeof(titles[i]),stdin);
	trim_newline(titles[i]);

	a = fgets(artists[i],sizeof(artists[i]),stdin);
        trim_newline(artists[i]);

	printf("Enter a rating: ");
	scanf("%d", &ratings[i]);
        printf("Rating: %d\n", ratings[i]);

	song_numbers[i] = i;
	printf("Song number: %d\n", song_numbers[i]);

		
}

Edited 4 Years Ago by dbsp: n/a

try putting the while loop at the end of the for loop(or at different parts beginning, after the scanf, etc.) and see if it helps

Get rid of that complex FOR statement. Doing 2 fgets() in a FOR statement is a recipe for disaster. Keep your code simple.
Pull those 2 reads out and put them in the loop itself.

The problem is not with fgets() reading a newline character, it's with scanf() leaving a newline character in the stream for fgets() to read. To fix the problem, change how you're using scanf(), or use a different method of input.

There are two common recommendations. First, you can avoid scanf() altogether and always use fgets() for stream input. Then the string retrieved by fgets() can be parsed in memory without any worry of mucking up the stream.

#include <stdio.h>

int main(void)
{
    char line[BUFSIZ];
    int value;
    
    if (fgets(line, sizeof line, stdin) &&
        sscanf(line, "%d", &value) == 1)
    {
        printf("The integer you typed is %d\n", value);
    }
    
    return 0;
}

Second, you can try to clean up the stream after calling scanf() like zeroliken's code:

#include <stdio.h>

int main(void)
{
    int value;
    int rc = scanf("%d", &value);
    
    if (rc != EOF)
    {
        int ch;
        
        // Clean up the stream (assumes everything left is garbage
        while ((ch = getchar()) != '\n' && ch != EOF)
        {
            // All work done in the condition
        }
    }
    
    if (rc == 1)
    {
        // Display the value if it was successfully read
        printf("The integer you typed is %d\n", value);
    }
    
    return 0;
}
Comments
I wasn't familiar with the first one so thanks for posting
This article has been dead for over six months. Start a new discussion instead.