www.digitalmars.com         C & C++   DMDScript  

c++ - Problem with input, no output

reply David Harmon <<source netcom.com>> writes:
Using Digital Mars Compiler Version 8.38n and stlport.  Win98SE.
Compile and run, enter some numbers, press ctrl-Z for EOF.
The output "Answer: " etc. never appears.  Uncomment the
indicated line, and all works as I expect.  
Am I doing something dumb?

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
int main()
{
    typedef double T;
    vector<T> scores;
    cout << "Enter scores, EOF to end: ";
    copy(istream_iterator<T>(cin), istream_iterator<T>(),
            back_inserter(scores));
    // cout << "\nUncomment this line, problem goes away.\n";
    cout << "Answer: " << scores.size()  << '\n';
}
Apr 14 2004
next sibling parent David Harmon <<source netcom.com>> writes:
On Wed, 14 Apr 2004 18:33:08 -0700 in c++,
David Harmon <<source netcom.com>> wrote:
Using Digital Mars Compiler Version 8.38n and stlport.  Win98SE.

Sorry, "Digital Mars Compiler Version 8.38n" is the string it displays when run dmc with no arguments. But it was downloaded as dm840c.zip so I guess I have got version 8.40.
Apr 14 2004
prev sibling parent reply "Pablo Aguilar" <paguilarg hotmail.com> writes:
try with an std::flush at the end:

cout << "Answer: " << scores.size()  << '\n' << flush;

 Using Digital Mars Compiler Version 8.38n and stlport.  Win98SE.
 Compile and run, enter some numbers, press ctrl-Z for EOF.
 The output "Answer: " etc. never appears.  Uncomment the
 indicated line, and all works as I expect.
 Am I doing something dumb?

 #include <iostream>
 #include <vector>
 #include <iterator>
 #include <algorithm>
 using namespace std;
 int main()
 {
     typedef double T;
     vector<T> scores;
     cout << "Enter scores, EOF to end: ";
     copy(istream_iterator<T>(cin), istream_iterator<T>(),
             back_inserter(scores));
     // cout << "\nUncomment this line, problem goes away.\n";
     cout << "Answer: " << scores.size()  << '\n';
 }

Apr 15 2004
parent David Harmon <<source netcom.com>> writes:
On Thu, 15 Apr 2004 12:19:36 -0700 in c++,
"Pablo Aguilar" <paguilarg hotmail.com> wrote:

try with an std::flush at the end:

cout << "Answer: " << scores.size()  << '\n' << flush;

OK, I have now tried that. Same result. Of course, the stream should normally be flushed at the end of execution automatically. What was it that you expected that experiment to reveal? The problem apparently has to do with the cout<<'\n' immediately after input EOF before outputting anything else. That is what makes all the difference in my experiments now, but I see no reason why it should make such a difference.
Apr 15 2004