VERY strange problem using the function gets() 
Author Message
 VERY strange problem using the function gets()

I hope someone can help me.
Simply: if I type these lines...

...
cout << "Sentence1";
gets(str);
cout << "Sentence2";
...

i get the result:

<prompt for the input of the string>
Sentence1
Sentence2

Does someone know why MS Visual C++ executes the gets()
function before the first cout instruction?
Please help me because I'm going MAD!



Wed, 19 Jan 2005 00:23:30 GMT  
 VERY strange problem using the function gets()
gets() is probably picking up the return character from previous input.  It
just -looks- like it is running before hand because it doesn't pause between
those 2 cout calls.

You need to read in the return character before you call gets().  Try
cin.getline()

-Sean


Quote:
> I hope someone can help me.
> Simply: if I type these lines...

> ...
> cout << "Sentence1";
> gets(str);
> cout << "Sentence2";
> ...

> i get the result:

> <prompt for the input of the string>
> Sentence1
> Sentence2

> Does someone know why MS Visual C++ executes the gets()
> function before the first cout instruction?
> Please help me because I'm going MAD!



Wed, 19 Jan 2005 01:07:50 GMT  
 VERY strange problem using the function gets()
Hi,
there is nothing strange here. You simply cant mix
iostream and C I/O calls... choose one.

Andrew

Quote:
>-----Original Message-----
>I hope someone can help me.
>Simply: if I type these lines...

>....
>cout << "Sentence1";
>gets(str);
>cout << "Sentence2";
>....

>i get the result:

><prompt for the input of the string>
>Sentence1
>Sentence2

>Does someone know why MS Visual C++ executes the gets()
>function before the first cout instruction?
>Please help me because I'm going MAD!
>.



Wed, 19 Jan 2005 01:08:00 GMT  
 VERY strange problem using the function gets()
Look at
ios_base::sync_with_stdio
Ronald Laeremans
Visual C++ compiler and libraries team


Quote:
> Hi,
> there is nothing strange here. You simply cant mix
> iostream and C I/O calls... choose one.

> Andrew

> >-----Original Message-----
> >I hope someone can help me.
> >Simply: if I type these lines...

> >....
> >cout << "Sentence1";
> >gets(str);
> >cout << "Sentence2";
> >....

> >i get the result:

> ><prompt for the input of the string>
> >Sentence1
> >Sentence2

> >Does someone know why MS Visual C++ executes the gets()
> >function before the first cout instruction?
> >Please help me because I'm going MAD!
> >.



Wed, 19 Jan 2005 01:33:12 GMT  
 VERY strange problem using the function gets()

Quote:
> >cout << "Sentence1";
> >gets(str);
> >cout << "Sentence2";
> Hi,
> there is nothing strange here. You simply cant mix
> iostream and C I/O calls... choose one.

You can, actually...there's a function that will sync the two...I'm not sure
it's necessary here though, since he's using iostreams for standard out and
C streams for standard in; I'm not sure if that counts as mixing.  Also, you
may need to flush cout to get Sentence1 to appear, as it may be buffered.

However, in addition to what everyone else has said, I strongly recommend
avoiding gets...it's a buffer overflow waiting to happen.  If you must have
gets behavior, use fgets(buf,size,stdin), which at least lets you specify
the size of your buffer, as to avoid overflowing it.  gets will happily
write all over your stack.

Ken



Wed, 19 Jan 2005 01:55:15 GMT  
 VERY strange problem using the function gets()
Thank you very much.
I solved the problem using cin.getline().
I was referring to an old C++ book :o

Quote:
>-----Original Message-----
>gets() is probably picking up the return character from
previous input.  It
>just -looks- like it is running before hand because it

doesn't pause between
Quote:
>those 2 cout calls.

>You need to read in the return character before you call
gets().  Try
>cin.getline()

>-Sean



>> I hope someone can help me.
>> Simply: if I type these lines...

>> ...
>> cout << "Sentence1";
>> gets(str);
>> cout << "Sentence2";
>> ...

>> i get the result:

>> <prompt for the input of the string>
>> Sentence1
>> Sentence2

>> Does someone know why MS Visual C++ executes the gets()
>> function before the first cout instruction?
>> Please help me because I'm going MAD!

>.



Thu, 20 Jan 2005 20:41:58 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. VERY strange problem using the function gets()

2. strange problem occur while trying to access member function through static function

3. Getting time strange problem

4. Strange crash using virtual function with thirdy part library class

5. Strange problem with function call - Anyone?

6. Strange Problem using SQL statement in ISAPI Extension

7. Strange member function naming problem...

8. Strange virtual function problem, please help!

9. strange problem when using mscomm32.ocx

10. Getting dialog box color without using GetSysColor API function

11. Problem with getting values back from a function...

12. gets() function problem...

 

 
Powered by phpBB® Forum Software