myromance123
February 4th, 2009, 03:49 PM
Hey there, I'm having trouble understanding how this statement works
while(getchar() != '\n');
From what I understand is that it searchs until it reaches the newline character right?
Well how does it remove that character from standard input?
For example in the code below (which frustrated me until I tried the above statement):
#include <stdio.h>
int main()
{
char words[200];
char unwanted[2];
char input;
int done = 1;
puts("\n\tHey there, what do you want to be reminded?");
fgets(words, 200, stdin);
puts("\tOk I understand!\n");
puts("Would you like to exit? (Q) or be reminded? (R)\n");
while(done)
{
printf("Choice(type here):");
fgets(unwanted, 2, stdin);
input = tolower(unwanted[0]);
while(getchar() != '\n');
switch(input)
{
case('r'):
printf("\t%s\n", words);
puts("Would you like to exit now? (Q) or be reminded again? (R)");
break;
case('q'):
printf(" Now Exiting...\n");
done=0;
break;
default:
puts("Invalid key pressed");
puts("Please press only Q to exit or R to be reminded");
break;
}
}
puts("Thanks for using me!! ");
return(0);
}
Now when I first tried the above I didn't include that statement and the stupid newline character wouldn't stop bugging me ( I also used scanf() instead fgets() at first but it was too troublesome)
So if anyone could help me understand why:
fgets(unwanted, 2, stdin);
input = tolower(unwanted[0]);
doesn't suffice and that I need the while statement?
Plus how that statement works?
I had also declared the words variable without stating the amount of elements (the [200]) but found I couldn't use fgets() without a predetermined size...
Is there a way to use fgets() without determining the size?
Because the point of the program is to record input (which could be very long or short)..
Im still quite the beginner at C so please keep it simple, thanks!
:)
while(getchar() != '\n');
From what I understand is that it searchs until it reaches the newline character right?
Well how does it remove that character from standard input?
For example in the code below (which frustrated me until I tried the above statement):
#include <stdio.h>
int main()
{
char words[200];
char unwanted[2];
char input;
int done = 1;
puts("\n\tHey there, what do you want to be reminded?");
fgets(words, 200, stdin);
puts("\tOk I understand!\n");
puts("Would you like to exit? (Q) or be reminded? (R)\n");
while(done)
{
printf("Choice(type here):");
fgets(unwanted, 2, stdin);
input = tolower(unwanted[0]);
while(getchar() != '\n');
switch(input)
{
case('r'):
printf("\t%s\n", words);
puts("Would you like to exit now? (Q) or be reminded again? (R)");
break;
case('q'):
printf(" Now Exiting...\n");
done=0;
break;
default:
puts("Invalid key pressed");
puts("Please press only Q to exit or R to be reminded");
break;
}
}
puts("Thanks for using me!! ");
return(0);
}
Now when I first tried the above I didn't include that statement and the stupid newline character wouldn't stop bugging me ( I also used scanf() instead fgets() at first but it was too troublesome)
So if anyone could help me understand why:
fgets(unwanted, 2, stdin);
input = tolower(unwanted[0]);
doesn't suffice and that I need the while statement?
Plus how that statement works?
I had also declared the words variable without stating the amount of elements (the [200]) but found I couldn't use fgets() without a predetermined size...
Is there a way to use fgets() without determining the size?
Because the point of the program is to record input (which could be very long or short)..
Im still quite the beginner at C so please keep it simple, thanks!
:)