What i've written below is a much simplified version of my program but it has the exact same problem (didn't want to bore people with long code).
I wrote the printf's appropriate to what the code does or doesn't do.
it doesn't take the fgets (in the function) in the second while loop but never the first, and i want it to take a string in the first.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int this_is_function()
{
char anotherBuffer[255];
char test2[] = "win";
printf("This fgets doesn't catch first time.\n>> ");
fgets(anotherBuffer, 4, stdin);
printf("%s\n", anotherBuffer);
if (strcmp(test2,anotherBuffer) == 0){
printf("success");
return 1;
}
return 0;
}
int main(void)
{
char *buffer;
char test1[] = "hello";
buffer = (char*)malloc(255);
printf("If i enter some input here.\n>> ");
fgets(buffer, 6, stdin);
printf("%s\n", buffer);
if (strcmp(buffer,test1) == 0){
while (this_is_function() == 0)
printf("Error");
}
free(buffer);
}
any ideas? thanks in advance
3 6343 @ocli5568
There is probably a '\n' left in input stream,which is read in first loop iteration causing fgets to stop receiving input from the stream.
So you will need to check to see if the input stream is empty before you use fgets,if it is not you'll need to empty it yourself ,something like: - int this_is_function()
-
{
-
char anotherBuffer[255];
-
char test2[] = "win";
-
-
printf("This fgets doesn't catch first time.\n>> ");
-
-
while(!feof(stdin))
-
{
-
getchar();
-
}
-
-
fgets(anotherBuffer, 4, stdin);
-
-
printf("%s\n", anotherBuffer);
-
-
if (strcmp(test2,anotherBuffer) == 0){
-
printf("success");
-
return 1;
-
}
-
-
return 0;
-
}
NOTE:Usually there will be only '\n' left in stream so you could change the while loop statement for an if statement.
@ocli5568
In main when you type in six characters and press the enter key more than six characters are present in the system's input buffer but at maximum only six of them are read. The others are left there for a next read (scanf). Even worse, Microsoft in all its glorious wisdom had decided that the enter key stuffs two characters in your input buffer: \r and \n. If even one of them is left in the input buffer your reading goes berzerk. You have to anticipate on that.
kind regards,
Jos
thanks so much for all the advice. although savage i tried implementing your version, it seemed to get stuck on a while loop and i don't understand enough about the error functions to go about fixing anything there. but i tried initializing my test function as what it is, adding a '\n' to the end of it and that cleared up the problem (using both of your help, again thankyou).
would that fix the problem consistently? because it seems like a flimsy quick fix.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Rob Somers |
last post by:
Hey all
I am writing a program to keep track of expenses and so on - it is not
a school project, I am learning C as a hobby - At any rate, I am new
to structs and reading and writing to files,...
|
by: Peter Mount |
last post by:
Hello
What's the syntax for using fgets() to store a string in memory? I
understand that fgets() can solve the problem of storing a string that has
more characters than the size of the declared...
|
by: Paul Hsieh |
last post by:
"Paul D. Boyle" <boyle@laue.chem.ncsu.edu> wrote:
> There was a recent thread in this group which talked about the
> shortcomings of fgets(). I decided to try my hand at writing a
> replacement...
|
by: TTroy |
last post by:
Hello,
I have found some peculiar behaviour in the fgets runtime library
function for my compiler/OS/platform (Dev C++/XP/P4) - making a C
console program (which runs in a CMD.exe shell).
The...
|
by: Aomighty |
last post by:
Hi, I've been creating a simple calculator program that asks whether
you want to add, subtract, multiply or divide, asks you for input, and
then performs the calculation. Things seem to be going...
|
by: santosh |
last post by:
Hi,
A book that I'm currently using notes that the fgets() function does
not
return until Return is pressed or an EOF or other error is encountered.
It then at most (in the absence of...
|
by: FireHead |
last post by:
Hello C World & Fanatics
I am trying replace fgets and provide a equavivalant function of
BufferedInputReader::readLine.
I am calling this readLine function as get_Stream.
In the line 4 where...
|
by: Sheth Raxit |
last post by:
Machine 1 :
bash-3.00$ uname -a
SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R
bash-3.00$ gcc -v
Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/...
|
by: Bill Cunningham |
last post by:
I was talking with someone about fgets and he said that fgets puts the
\n in a string but not \0. I decided to test this assumption because my
documentation didn't say if fgets put \0 after a...
|
by: Krumble Bunk |
last post by:
Hi all,
Having some trouble with a seemingly-simple task. Need to have a
basic CLI functionality as per:
prompt <- \n
prompt <- \n
promptquit <- should quit as per my snippet, but...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |