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?

Recommended Answers

All 10 Replies

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

You can't use this

stn!="123"
str=="123"

Try looking up strcmp or the string library of functions

It is not a literal example :) Im very sorry that I did not state that.

void valid(int stringi,int *var)
 {
     char strings[20];
     itoa(stringi,strings,10);

        if (strlen(strings)==8)
        {
            if ((stringi>=11111111) || (stringi<=99999999))
            {
                    printf ("Valid!\n");
                    (*var)=1;
            }
        }
        else
        {
            printf ("Invalid!\n");
            (*var)=0;
        }
 }

 void insertstring(char *string)
 {
     int var=0;
     do
     {
        fflush(stdin);
        printf ("Insert:\n");
        gets(string);
        //fgets(string,sizeof(&string),stdin);
        valid(atoi(string),&var);
     }while(var==0);
 }

This is a quick rough up of my real example. fgets is something I tried as well but it doesnt work correctly.

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.

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);
}
commented: A good starting point +8

>>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 <Enter> 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]

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);
}

Well it seems that in the main function it works always but using 2 functions, it doesnt. So if you dont mind a example using 2 functions :)

>>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 <Enter> 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);

If I want it to only get 8 characters, I should set it to 9? (To include the /0 character)

Title: Strange bug with gets()

Answer: the "gets" function *IS* a bug. kill it before it breeds.

commented: Read the title. Thought the same. +8

Answer: the "gets" function *IS* a bug. kill it before it breeds.

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 :)

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 using gets() in the first place.

gets() is never OK. ever. so get it out of your head.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.