C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Need help with break instruction
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Renato
Guest





PostPosted: Fri Jul 22, 2005 4:07 pm    Post subject: Need help with break instruction Reply with quote



I am doing this tutorial at
http://www.cplusplus.com/doc/tutorial/tut2-1.html

This code didn't work.

// break loop example
#include <iostream.h>
int main ()
{
int n;
for (n=10; n>0; n--) {
cout << n << ", ";
if (n==3)
{
cout << "countdown aborted!";
break;
}
}
return 0;
}

Didn't show any error at compiling. But does nothing when I run it.
I use Borland C++ Compiler 5.5
Thank you in advance for your help.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Allan W
Guest





PostPosted: Sat Jul 23, 2005 1:02 am    Post subject: Re: Need help with break instruction Reply with quote



Your output isn't being "flushed" to the console before the program
exits.
Try making this change:

Renato wrote:
Quote:
// break loop example
#include <iostream.h
int main ()
{
int n;
for (n=10; n>0; n--) {
cout << n << ", ";
if (n==3)
{
cout << "countdown aborted!";

cout << endl; // <== Add this line <==

Quote:
break;
}
}
return 0;
}

With that line in place you should see output.
(You could also have inserted the same line after the
end of the for() loop, or several other places...)


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Peter Julian
Guest





PostPosted: Sat Jul 23, 2005 1:04 am    Post subject: Re: Need help with break instruction Reply with quote




"Renato" <renato.frambach (AT) gmail (DOT) com> wrote

Quote:
I am doing this tutorial at
http://www.cplusplus.com/doc/tutorial/tut2-1.html

This code didn't work.

// break loop example
#include <iostream.h

#include
Quote:
int main ()
{
int n;
for (n=10; n>0; n--) {
cout << n << ", ";

std::cout << n << ", ";

Quote:
if (n==3)
{
cout << "countdown aborted!";

std::cout << "countdown aborted!";

Quote:
break;
}
}
return 0;
}

Didn't show any error at compiling. But does nothing when I run it.

Set a breakpoint on the return statement and run in debug. Note that you'll
want to replace other includes as well if you continue to read outdated
tutorials:

<string.h> ... <string>
etc

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
BigBrian
Guest





PostPosted: Sat Jul 23, 2005 1:07 am    Post subject: Re: Need help with break instruction Reply with quote



Renato wrote:
Quote:
I am doing this tutorial at
http://www.cplusplus.com/doc/tutorial/tut2-1.html

This code didn't work.

// break loop example
#include <iostream.h
int main ()
{
int n;
for (n=10; n>0; n--) {
cout << n << ", ";
if (n==3)
{
cout << "countdown aborted!";
break;
}
}
return 0;
}

Didn't show any error at compiling. But does nothing when I run it.
I use Borland C++ Compiler 5.5
Thank you in advance for your help.


It compiles and runs fine for me with g++ 2.95.2 on solaris 8.

-Brian


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Thomas Tutone
Guest





PostPosted: Sat Jul 23, 2005 1:07 am    Post subject: Re: Need help with break instruction Reply with quote

Renato wrote:
Quote:
I am doing this tutorial at
http://www.cplusplus.com/doc/tutorial/tut2-1.html

Use a different tutorial - that one apparently was written before 1997,
when the C++ Standard was adopted. The code therefore includes errors
(which may be unrelated to the problem you are having).

Quote:
This code didn't work.

Meaning that it didn't print anything?

Quote:
// break loop example
#include <iostream.h

That's a nonstandard header. Instead use:

#include
Quote:
int main ()
{

add this:

using namespace std;

Quote:
int n;
for (n=10; n>0; n--) {
cout << n << ", ";
if (n==3)
{
cout << "countdown aborted!";
break;
}
}

add this:

cout << endl;

Quote:
return 0;
}

Didn't show any error at compiling. But does nothing when I run it.

iostreams may buffer output. It's possible that the output got stuck
in that buffer. If so, then adding the "cout << endl;" will force the
flushing of the buffer, so that it should print.

As a check, does a simple "hello world" program give you the expected
output?

Best regards,

Tom


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
James Kanze
Guest





PostPosted: Sun Jul 24, 2005 11:39 am    Post subject: Re: Need help with break instruction Reply with quote

Allan W wrote:
Quote:
Your output isn't being "flushed" to the console before the
program exits.

It should be.

Quote:
Try making this change:

Renato wrote:

// break loop example
#include <iostream.h
int main ()
{
int n;
for (n=10; n>0; n--) {
cout << n << ", ";
if (n==3)
{
cout << "countdown aborted!";


cout << endl; // <== Add this line <==


break;
}
}
return 0;
}

With that line in place you should see output.

Even without it. return 0 calls exit(0), which is supposed to,
sooner or later, delete all instances of ios::Init. And
deleting the last instance should flush cout.

Of course, there could be a problem due to the output not ending
with a 'n'. Under Unix, at least, this typically means that
the next shell prompt will appear on the same line.

Globally, I suspect a problem with the environment, and the way
he is invoking the program. From what I gather under Windows,
for example, invoking the program other than from a command line
in a DOS box will result in a temporary window, which closes as
soon as the program terminates. In that case, with his program,
the window will appear and disappear before he has had time to
see it. And since his output is in the window...

--
James Kanze mailto: [email]james.kanze (AT) free (DOT) fr[/email]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
James Kanze
Guest





PostPosted: Sun Jul 24, 2005 11:40 am    Post subject: Re: Need help with break instruction Reply with quote

Peter Julian wrote:
Quote:
"Renato" <renato.frambach (AT) gmail (DOT) com> wrote in message
news:1122045772.464536.301210 (AT) g43g2000cwa (DOT) googlegroups.com...

I am doing this tutorial at


This code didn't work.

// break loop example
#include <iostream.h

#include

And change a correct program into one which shouldn't compile?

Since the OP is learning C++, he certainly should be using the
standard libraries, instead of the classical ones. But if you
want to point that fact out, you should at least do so
correctly. In his case, he needs to replace his include with:

#include #include <iostream>

and change all instances of cout to std::cout. Anything less
either doesn't compile or results in undefined behavior.

--
James Kanze mailto: [email]james.kanze (AT) free (DOT) fr[/email]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Peter Julian
Guest





PostPosted: Sun Jul 24, 2005 6:04 pm    Post subject: Re: Need help with break instruction Reply with quote


"James Kanze" <kanze (AT) none (DOT) news.free.fr> wrote

Quote:
Peter Julian wrote:
"Renato" <renato.frambach (AT) gmail (DOT) com> wrote in message
news:1122045772.464536.301210 (AT) g43g2000cwa (DOT) googlegroups.com...

I am doing this tutorial at


This code didn't work.

// break loop example
#include
#include
And change a correct program into one which shouldn't compile?

You need someone to hold your hand to figure out which lines to replace?
Compiles fine here:

/* output:

10, 9, 8, 7, 6, 5, 4, 3, countdown aborted!

*/

Quote:

Since the OP is learning C++, he certainly should be using the
standard libraries, instead of the classical ones. But if you
want to point that fact out, you should at least do so
correctly. In his case, he needs to replace his include with:

#include #include

Don't you know the STL iostream hierarchy? the ostream include directive
above is superfluous.

Quote:

and change all instances of cout to std::cout. Anything less
either doesn't compile or results in undefined behavior.

Thats exactly what i did. Did you even read my comment?

Quote:

--
James Kanze mailto: [email]james.kanze (AT) free (DOT) fr[/email]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Bo Persson
Guest





PostPosted: Sun Jul 24, 2005 11:31 pm    Post subject: Re: Need help with break instruction Reply with quote


"Peter Julian" <p_julian (AT) trap (DOT) trap.com> skrev i meddelandet
news:yaQEe.187$d02.53605 (AT) news20 (DOT) bellglobal.com...
Quote:

"James Kanze" <kanze (AT) none (DOT) news.free.fr> wrote in message

Since the OP is learning C++, he certainly should be using the
standard libraries, instead of the classical ones. But if you
want to point that fact out, you should at least do so
correctly. In his case, he needs to replace his include with:

#include <ostream
#include
Don't you know the STL iostream hierarchy? the ostream include
directive
above is superfluous.


No, its not!

The Standard specifically states that it is unspecified what headers are
included by other headers.

For internal header, before declaring cin, cout, etc.


The fact that many implementations choose to include <ostream> doesn't
make it standard (or portable), just a common practice.

Bo Persson



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Francis Glassborow
Guest





PostPosted: Sun Jul 24, 2005 11:37 pm    Post subject: Re: Need help with break instruction Reply with quote

In article <yaQEe.187$d02.53605 (AT) news20 (DOT) bellglobal.com>, Peter Julian
<p_julian (AT) trap (DOT) trap.com> writes
Quote:
Since the OP is learning C++, he certainly should be using the
standard libraries, instead of the classical ones. But if you
want to point that fact out, you should at least do so
correctly. In his case, he needs to replace his include with:

#include #include
Don't you know the STL iostream hierarchy? the ostream include directive
above is superfluous.

With regret, and probably as an oversight, that is not the case. The
Standard only specifies that iostream provides for cout, cin, clog, cerr
and the four wide character equivalents. Nothing more. If you want to
use istream or ostream and be fully portable you must include those
headers.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Old Wolf
Guest





PostPosted: Mon Jul 25, 2005 8:10 am    Post subject: Re: Need help with break instruction Reply with quote

Peter Julian wrote:
Quote:
Note that you'll want to replace other includes as well if you continue
to read outdated tutorials:

iostream.h> ... <iostream
string.h> ... <string

1) 2) <string> is NOT a replacement for <string.h>. That honour
goes to <cstring>.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Rene Moehring
Guest





PostPosted: Mon Jul 25, 2005 11:36 am    Post subject: Re: Need help with break instruction Reply with quote

On 25 Jul 2005 04:10:35 -0400, Old Wolf wrote:
Quote:
Peter Julian wrote:
Note that you'll want to replace other includes as well if you continue
to read outdated tutorials:

iostream.h> ... <iostream
string.h> ... <string

1)

Wrong, it is still legal.

Quote:
2) <string> is NOT a replacement for <string.h>. That honour
goes to <cstring>.

Yes string.h may be deprecated but it is not illegal.

--
I'm not a racist. I hate everyone equally!

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Rob Williscroft
Guest





PostPosted: Mon Jul 25, 2005 1:26 pm    Post subject: Re: Need help with break instruction Reply with quote

Rene Moehring wrote in news:dc2d9o$r8u$03$1 (AT) news (DOT) t-online.com in
comp.lang.c++.moderated:

Quote:
On 25 Jul 2005 04:10:35 -0400, Old Wolf wrote:
Peter Julian wrote:
Note that you'll want to replace other includes as well if you
continue to read outdated tutorials:

iostream.h> ... <iostream
string.h> ... <string

1)
Wrong, it is still legal.

2) <string> is NOT a replacement for <string.h>. That honour
goes to <cstring>.

Yes string.h may be deprecated but it is not illegal.


???

usenet-isnt-a-write-only-medium'ly yr's Rob.
--
http://www.victim-prime.dsl.pipex.com/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Mon Jul 25, 2005 3:20 pm    Post subject: Re: Need help with break instruction Reply with quote

Francis Glassborow wrote:
Quote:
In article <yaQEe.187$d02.53605 (AT) news20 (DOT) bellglobal.com>, Peter Julian
[email]p_julian (AT) trap (DOT) trap.com[/email]> writes
Since the OP is learning C++, he certainly should be using
the standard libraries, instead of the classical ones. But
if you want to point that fact out, you should at least do
so correctly. In his case, he needs to replace his include
with:

#include <ostream
#include
Don't you know the STL iostream hierarchy? the ostream
include directive above is superfluous.

With regret, and probably as an oversight, that is not the
case.

I've often wondered about that. I always thought that the
rationale to dividing was that you never needed to include more than you needed.
According to this philosophie, it actually makes more sense for
an implementation of <iostream> to just include <iosfwd>, and
not the whole bazaar.

I understand the desire for a single header to include the whole
bazaar; it's very useful for such small programs. But I also
see an advantage in being able to refer to cout et al. without
pulling in everything.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Dietmar Kuehl
Guest





PostPosted: Mon Jul 25, 2005 7:22 pm    Post subject: Re: Need help with break instruction Reply with quote

Peter Julian wrote:
Quote:
#include <iostream.h

#include
And change a correct program into one which shouldn't compile?

You need someone to hold your hand to figure out which lines to replace?

Note, that merely including the current standard. This is what James was pointing out.

Quote:
Compiles fine here:

It does not matter whether it compiles on one platform. The issue is
whether it should compile and run correctly on all standard conforming
platforms and this is not at all guaranteed if you only include
<iostream>.

Quote:
#include <ostream
#include
Don't you know the STL iostream hierarchy? the ostream include directive
above is superfluous.

You are mistaken and James is right: just including insufficient! All this header does, is to declare the standard stream
objects. For this, the header could look essentially like this:

namespace std {
template <typename _Ct, typename _Tr = ::std::char_traits<_Ct>
class basic_istream;
template <typename _Ct, typename _Tr = ::std::char_traits<_Ct>
class basic_ostream;

extern basic_istream<char> cin;
extern basic_ostream<char> cout;
extern basic_ostream<char> cerr;
extern basic_ostream<char> clog;

extern basic_istream<wchar_t> wcin;
extern basic_ostream<wchar_t> wcout;
extern basic_ostream<wchar_t> wcerr;
extern basic_ostream<wchar_t> wclog;
}

In reality, the header has to make sure that the declarations of
'basic_istream' and 'basic_ostream' are omitted or at least their
default arguments are dropped (only the first declaration of a
template may specify default arguments). Other than this, the above
is valid implementation of <iostream>. Now, given this header try to
output something without the definition of 'basic_ostream'. The
definition of 'basic_ostream' resided in <ostream>, i.e. you need to
include both <iostream> and <ostream> if you want to output something
to 'std::cout'.

Quote:
and change all instances of cout to std::cout. Anything less
either doesn't compile or results in undefined behavior.

Thats exactly what i did. Did you even read my comment?

No, this is not exactly what you did. You omitted the required header
<ostream>. This is what James was pointing out. You may want to check
your facts in the future before replying the way you did...
--
<mailto:dietmar_kuehl (AT) yahoo (DOT) com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.