Well i don't have code on me, but I sometimes come across a situation in the past where my program skips the user input, such as a gets line or an fgets line. I'm wondering why it skips this?
is there a general reason behind this?
Printable View
Well i don't have code on me, but I sometimes come across a situation in the past where my program skips the user input, such as a gets line or an fgets line. I'm wondering why it skips this?
is there a general reason behind this?
Next time post code. This is such a general question, that if it wasn't such a common issue, nobody would have the slightest idea what you're talking about.
The issue is probably because you're mixing and matching different forms of input functions that behave slightly differently. One example would be if you tried to read an integer via scanf() and then a string via fgets().
If you entered a number such as 45, the input buffer would look like this:
scanf() would read the 45 and leave the '\n' in the buffer, resulting in an input buffer that looks like this:Code:'4', '5', '\n'
fgets() would then come along and look for a string. It is built, however, to stop on either of two conditions:Code:'\n'
- It reaches the limit on the chars read, set by the caller (in the 2nd argument).
- It finds a '\n' in the buffer.
Since fgets() finds a '\n', it takes it, stores it, and returns right away. The user is not prompted for input because no such input request is needed when what you need is already in the buffer. Thus it appears as if the user was simply ignored.
A solution would be to place a getchar() before the fgets() call in such a case, however, a better solution might be to always use fgets() to read input from the user and parse it via sscanf() or some other method.
So anyway here's the code. I type 1 and it skips the input for the reverse string. please note this is a friend's code, i don't even do this subject but its always interesting to know why. I did a packet sniffer program in C last semester and came across similar errors but never knew EXACTLY why.
Code:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define strLength 120
int main()
{
while (1)
{
menu();
}
return;
}
int menu()
{
//variables
int nChoice;
int n;
int x;
int fibCount;
printf("\n\nChoose an Option.\n");
printf("--------------------\n\n");
printf("1. Reverse a String\n");
printf("2. Fibonacci Sequence\n");
printf("3. Get Statistics on a String\n");
printf("4. Play Extended Tic-Tac-Toe\n");
printf("5. Exit the Program\n\n");
printf("Input Choice: ");
scanf("%d", &nChoice);
switch(nChoice)
{
case 1 : printf("\nYou chose Option 1: Reverse a String.\n");
printf("---------------------------------------\n\n");
RevString();
break;
case 2 : printf("\nYou chose Option 2: Fibonacci Sequence.\n");
printf("---------------------------------------\n\n");
do
{
printf("How many numbers would you like displayed? (between 1 and 100): ");
scanf("%d", &n);
}while(n < 1 || n > 100);
for(n = 0; n < fibCount ; n++)
{
x=Fib(n);
printf("%d ", x);
}
break;
case 3 : printf("\nYou chose Option 3: Statistics of a String.\n");
printf("-------------------------------------------\n\n");
StatString();
break;
case 5 : printf("\nThank You for using this Program.\n");
printf("---------------------------------\n\n");
exit(0);
default: printf("Incorrect input.\n");
printf("--------------\n\n");
menu();
}
}
int RevString()
{
char str[strLength];
char revStr[strLength];
int i = 0;
int j = 0;
printf("Enter the string to be reversed: \n\n");
fgets(str, 120, stdin);
for(i = strlen(str) - 1; i >= 0; i--)
{
revStr[j] = str[i];
j++;
}
revStr[j] = '\0';
printf("You inputted: %s\n", str);
printf("Input reversed: %s\n", revStr);
return;
}
Surprise. My explanation fits the given situation.