www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - Problem with input, no output

↑ ↓ ← 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
→ 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
"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
↑ ↓ → 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