Unable to sync input and output streams 
Author Message
 Unable to sync input and output streams

The problem arises with the following code:

cout.sync_with_stdio();
cout << "sentence1 ";
cin.getline(string1, LENGHT);
cout << "sentence2\n";
...
cout << "sentence3 ";
cin.getline(string2, LENGHT); //this line isn't executed!
cout << "sentence4 ";

The last 3 lines don't work as expected. The output is:
sentence1 <input for string1> sentence2
sentence3 sentence4

How do I solve this problem?



Fri, 21 Jan 2005 23:26:36 GMT  
 Unable to sync input and output streams

Quote:
> The problem arises with the following code:

> cout.sync_with_stdio();

You don't need this for what you've shown here.  sync_with_stdio is meant to
ensure that the C++ stream i/o (cin, cout, cerr) are in sync with the C
stream handles stdin, stdout, stderr (as used with printf, scanf, gets,
fgets, etc.).

Quote:
> cout << "sentence1 ";
> cin.getline(string1, LENGHT);
> cout << "sentence2\n";

I assume you meant LENGTH, not LENGHT.

Quote:
> ...
> cout << "sentence3 ";
> cin.getline(string2, LENGHT); //this line isn't executed!
> cout << "sentence4 ";

What is the value of LENGHT?  How many characters of input are you typing?

Quote:

> The last 3 lines don't work as expected. The output is:
> sentence1 <input for string1> sentence2
> sentence3 sentence4

> How do I solve this problem?

Try adding a cin.ignore(a_number_larger_than_any_single_line,'\n');

before the second call to cin.getline();

-cd



Fri, 21 Jan 2005 23:43:17 GMT  
 Unable to sync input and output streams
I also had to add another ignore() before the first call
of getline() but finally it works! Thanks!
(ps:sorry for the "lenght" but I don't talk in English
every day... :) )

Quote:
>-----Original Message-----


>> The problem arises with the following code:

>> cout.sync_with_stdio();

>You don't need this for what you've shown here.  

sync_with_stdio is meant to
Quote:
>ensure that the C++ stream i/o (cin, cout, cerr) are in
sync with the C
>stream handles stdin, stdout, stderr (as used with

printf, scanf, gets,
Quote:
>fgets, etc.).

>> cout << "sentence1 ";
>> cin.getline(string1, LENGHT);
>> cout << "sentence2\n";

>I assume you meant LENGTH, not LENGHT.

>> ...
>> cout << "sentence3 ";
>> cin.getline(string2, LENGHT); //this line isn't
executed!
>> cout << "sentence4 ";

>What is the value of LENGHT?  How many characters of

input are you typing?
Quote:

>> The last 3 lines don't work as expected. The output is:
>> sentence1 <input for string1> sentence2
>> sentence3 sentence4

>> How do I solve this problem?

>Try adding a cin.ignore

(a_number_larger_than_any_single_line,'\n');
Quote:

>before the second call to cin.getline();

>-cd

>.



Sat, 22 Jan 2005 01:15:11 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Can't sync input and output streams...

2. Programming REXEC() with input and output streams

3. Difficulty with input/output streams

4. input, output, input/output parameters?????

5. unable to sync my appln.please help.

6. unable to scroll output window

7. Unable to get key input

8. Unable to output Bitmaps to printer

9. M0006: unable to pre-process input file

10. How to clear input stream in ANSI C?

11. c file (stream) input buffer problem help PLEASE!

12. flushing the standard input stream (newb question)

 

 
Powered by phpBB® Forum Software