I'm writing a program that will take 5 characters the user inputs. The characters get compared, and a if the word matches anything, a message is printed out. It's being implemented in c.
Originally,I was using fgets and having setting the limit to the characters I needed. However, this caused a few problems.
fgets(inputStr, OP_LEN + 1, stdin);
So, it'd take the input and save the characters to the string. If the user input was longer than op_len, then it'd keep running the comparisons on the input in chunks that are the length of op_len.
Like, if the input bobloblawblog
then it would compare boblo, blawb, log\n instead of just looking at boblo and ignoring the rest.
So im changing it to use a loop of getchar, like this:
int charv = 0;
int loopvar = 0;
int count = 0;
while (loopvar = 0)
{
charv = getchar();
if ((count < OP_LEN + 1) && (charv != LF) && (charv != EOF))
{
inputStr[count] = charv;
}
else
{
loopvar = 1;
inputStr[count + 1] = 0;
}
count++;
}
Afterwords, the comparisons are made and a loop restarts to take the inputs and start over. That part works when fgets. However, when I changed the input to be taken with the above, it never actually gets anything and loops nonstop. Can anybody help me with what I am doing wrong?
Originally,I was using fgets and having setting the limit to the characters I needed. However, this caused a few problems.
fgets(inputStr, OP_LEN + 1, stdin);
So, it'd take the input and save the characters to the string. If the user input was longer than op_len, then it'd keep running the comparisons on the input in chunks that are the length of op_len.
Like, if the input bobloblawblog
then it would compare boblo, blawb, log\n instead of just looking at boblo and ignoring the rest.
So im changing it to use a loop of getchar, like this:
int charv = 0;
int loopvar = 0;
int count = 0;
while (loopvar = 0)
{
charv = getchar();
if ((count < OP_LEN + 1) && (charv != LF) && (charv != EOF))
{
inputStr[count] = charv;
}
else
{
loopvar = 1;
inputStr[count + 1] = 0;
}
count++;
}
Afterwords, the comparisons are made and a loop restarts to take the inputs and start over. That part works when fgets. However, when I changed the input to be taken with the above, it never actually gets anything and loops nonstop. Can anybody help me with what I am doing wrong?