Hey
Im doing a loop with somthing similar to this:
char stn[25];
do
{
printf ("Insert a number:\n");
gets(stn);
}while (stn!="123");
if (str=="123")
{
printf ("Good value!");
}
Now if I input 123, it enters the if that prints "Good value!" and the program ends naturally.
Now if I input 32432, 23432, 23234, 1233,9898, 87343, 238432 (all of this it would continue inside the do while loop) and finally I enter 123, it prints "Good value!" but the program itself crashes.
Very strange. I think it is a error with the buffer overfilling or something similar. I tried with fflush(stdin) before/after/both in the gets() loop but nothing.
How can I solve this?
You can't use this
stn!="123"
str=="123"
Try looking up strcmp or the string library of functions
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
Here's what my manpages say about gets
BUGS
Never use gets(). Because it is impossible to tell without knowing the
data in advance how many characters gets() will read, and because
gets() will continue to store characters past the end of the buffer, it
is extremely dangerous to use. It has been used to break computer
security. Use fgets() instead.
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
Try running this code...What do you notice about the output. Is match different than str?
#include <stdio.h>
#include <stdlib.h>
char match[] = "123";
int main(int argc, char**argv)
{
char str[25];
fputs("enter a value->", stdout);
fgets(str, 25, stdin);
fputs(str, stdout);
fputs(match, stdout);
exit(EXIT_SUCCESS);
}
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
>>Now if I input 32432, 23432, 23234, 1233,9898, 87343, 238432
gets() is a horrible function because if the input buffer is not large enough to hold all the characters you type then it will just scribble any excess characters all over memory. So if you typed that all on one line before hitting the key then gets() will crash your program. gets() doesn't stop at the first spaces, but only when '\n' is encountered.
A better way to do it is to use fgets(), which will limit the input to the size of the input buffer. The problem with fgets() is that it may put the '\n' in the buffer too, which you will have to remove.
fgets(stn, sizeof(stn), stdin);
[edit]gerard ^^^ posted the correct example.[/edit]
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
An additional thing that looks real bad and may be contributing to your problems ..
fflush(stdin);
you might read Why fflush(stdin) is wrong .
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
Title: Strange bug with gets()
Answer: the "gets" function *IS* a bug. kill it before it breeds.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
The problem was that the size I was passing in the first function was a incorrect size and it had problems.
This is something simple so gets works OK for this :)
NO. You're unfortunately, understandably, but undeniably WRONG.
the PROBLEM was that you were usinggets() in the first place.
gets() is never OK. ever. so get it out of your head.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179