473,406 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Respecting the standard

Hello,

I was recently told that there were <quote>a lot of things
wrong</quote> with the following program:

#include <iostream>

int main()
{
cout << '\a';
return 0;
}

I believe I should have written std::cout instead of cout.
Alternatively, I think I could have written:

using namespace std; // I can now write 'cout' and 'endl'

Second, I might need to write a newline to cout, otherwise the input
might be discarded, is that correct?

I'll make these two adjustments:

#include <iostream>

int main()
{
std::cout << '\a' << std::endl;
return 0;
}

Do you see anything wrong with this program as far as standard C++
is concerned? Did I really need to write endl to cout?

g++ -ansi -pedantic -Wall -W foo.cxx does not give any warning, but
I don't know how closely g++ adheres to the standard.

Jul 22 '05 #1
8 1262
On Wed, 17 Dec 2003 10:44:09 +0100, Grumble <in*****@kma.eu.org>
wrote:
Hello,

I was recently told that there were <quote>a lot of things
wrong</quote> with the following program:

#include <iostream>

int main()
{
cout << '\a';
return 0;
}

I believe I should have written std::cout instead of cout.
Alternatively, I think I could have written:

using namespace std; // I can now write 'cout' and 'endl'

Second, I might need to write a newline to cout, otherwise the input
might be discarded, is that correct?

I'll make these two adjustments:

#include <iostream>

int main()
{
std::cout << '\a' << std::endl;
return 0;
}
Well, I'd adjust it to:

#include <iostream>
#include <ostream> //required for non-members

int main()
{
std::cout << "\a\n";
return 0;
//cout implicitly flushed on program exit.
}
Do you see anything wrong with this program as far as standard C++
is concerned?
Yes - you need to include <ostream> to use endl (although most
compilers don't care if <iostream> is included).
Did I really need to write endl to cout?
I think portable programs should finish stdoutput with a newline.
g++ -ansi -pedantic -Wall -W foo.cxx does not give any warning, but
I don't know how closely g++ adheres to the standard.


The latest version is pretty close.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #2
Grumble <in*****@kma.eu.org> writes:
Hello,

I was recently told that there were <quote>a lot of things
wrong</quote> with the following program:

#include <iostream>

int main()
{
cout << '\a';
return 0;
}

I believe I should have written std::cout instead of
cout. Alternatively, I think I could have written:

using namespace std; // I can now write 'cout' and 'endl'
Yep, both alternatives are correct.
Second, I might need to write a newline to cout, otherwise the input
might be discarded, is that correct?
ITYM output instead of input - it doesn't get discarded, but the output
buffer will not necessarily be flushed, so it will look like it got
discarded.

I'll make these two adjustments:

#include <iostream>

int main()
{
std::cout << '\a' << std::endl;
return 0;
}

Do you see anything wrong with this program as far as standard C++ is
concerned?
No, it's fine.
Did I really need to write endl to cout?

g++ -ansi -pedantic -Wall -W foo.cxx does not give any warning, but I
don't know how closely g++ adheres to the standard.


If it's g++ 3.x, it's quite close.

HTH & kind regards
frank

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
Jul 22 '05 #3
Grumble wrote:
Hello,

I was recently told that there were <quote>a lot of things wrong</quote>
with the following program:

#include <iostream>

int main()
{
cout << '\a';
return 0;
That return statement is redundant (in C++).

As a purely academic nitpick that happened to come up here recently, you
might need to include <ostream> if you want to use std::endl and keep
the program portable.
}

I believe I should have written std::cout instead of cout.
Alternatively, I think I could have written:

using namespace std; // I can now write 'cout' and 'endl'

Second, I might need to write a newline to cout, otherwise the input
might be discarded, is that correct?

I'll make these two adjustments:

#include <iostream>

int main()
{
std::cout << '\a' << std::endl;
return 0;
}

Do you see anything wrong with this program as far as standard C++ is
concerned? Did I really need to write endl to cout?

g++ -ansi -pedantic -Wall -W foo.cxx does not give any warning, but I
don't know how closely g++ adheres to the standard.


Jul 22 '05 #4
In article <ud********************************@4ax.com>,
tom_usenet <to********@hotmail.com> wrote:
Well, I'd adjust it to:

#include <iostream>
#include <ostream> //required for non-members

int main()
{
std::cout << "\a\n";
return 0;
//cout implicitly flushed on program exit.
}


Just curious: Have you come across any implementation where the
<ostream> is actually required? There is considerable debate on whether
we really want to break every single C++ text which has shown the
traditional "HelloWorld" with only <iostream>. ;-)

Martin Sebor has done an admirable job of trying to bring this issue to
the committee. Unfortunately I don't think he has entirely succeeded
yet.

-Howard
Jul 22 '05 #5
On Wed, 17 Dec 2003 14:29:26 GMT, Howard Hinnant
<hi*****@metrowerks.com> wrote:
In article <ud********************************@4ax.com>,
tom_usenet <to********@hotmail.com> wrote:
Well, I'd adjust it to:

#include <iostream>
#include <ostream> //required for non-members

int main()
{
std::cout << "\a\n";
return 0;
//cout implicitly flushed on program exit.
}
Just curious: Have you come across any implementation where the
<ostream> is actually required?


Nope, other than the Deathstation 9000. I thought that Dietmar's cxxrt
might be one, but it too includes <istream> and <ostream> in
<iostream>.

There is considerable debate on whetherwe really want to break every single C++ text which has shown the
traditional "HelloWorld" with only <iostream>. ;-)
Many hello world programs seem to use std::endl for no good reason,
and I could certainly envision an implementation that doesn't expose
endl unless ostream is explicitly included. But not to include the
non-member operator<<'s (for char*) would be a bit crazed - hello
world output would become a random pointer value!
Martin Sebor has done an admirable job of trying to bring this issue to
the committee. Unfortunately I don't think he has entirely succeeded
yet.


Good luck to him! It doesn't seem very important though, just ironic
that the canonical hello world program relies on unspecified
behaviour.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #6
In article <1i********************************@4ax.com>,
tom_usenet <to********@hotmail.com> wrote:
Many hello world programs seem to use std::endl for no good reason
<nod><flush>
I<flush>
agree<flush>
100%!<flush>
just ironic
that the canonical hello world program relies on unspecified
behaviour.


<chuckle> :-)

-Howard
Jul 22 '05 #7
On Wed, 17 Dec 2003 12:06:03 +0000, tom_usenet
<to********@hotmail.com> wrote:

[snip]
Do you see anything wrong with this program as far as standard C++
is concerned?


Yes - you need to include <ostream> to use endl (although most
compilers don't care if <iostream> is included).


The header synopsis for <iostream> (section 27.3) declares std::cout
as "extern ostream cout;" in namespace std. std::cin is declared as
"extern istream cin;" Therefore, it must include both <istream> and
<ostream>, as far as I can tell.
Did I really need to write endl to cout?


I think portable programs should finish stdoutput with a newline.


std::cin and std::cout should do whatever stdin and stdout are
supposed to do.
g++ -ansi -pedantic -Wall -W foo.cxx does not give any warning, but
I don't know how closely g++ adheres to the standard.


The latest version is pretty close.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html


--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #8
On Wed, 17 Dec 2003 20:20:48 GMT, wouldnt_you_like@to_know.com (Bob
Hairgrove) wrote:
On Wed, 17 Dec 2003 12:06:03 +0000, tom_usenet
<to********@hotmail.com> wrote:

[snip]
Do you see anything wrong with this program as far as standard C++
is concerned?
Yes - you need to include <ostream> to use endl (although most
compilers don't care if <iostream> is included).


The header synopsis for <iostream> (section 27.3) declares std::cout
as "extern ostream cout;" in namespace std. std::cin is declared as
"extern istream cin;" Therefore, it must include both <istream> and
<ostream>, as far as I can tell.


Nope, it just needs complete definitions of basic_istream and
basic_ostream (and the typedefs istream and ostream). Imagine if
<istream> is just

#include <iosfwd>
#include <impl/istream_core.h>
#include <impl/istream_non_members.h>
and <iostream> is

#include <iosfwd>
#include <impl/istream_core.h>
extern istream cin;
extern wistream wcin;
//...

Now you don't get std::endl (or operator<<(ostream, char const*)!).
Did I really need to write endl to cout?


I think portable programs should finish stdoutput with a newline.


std::cin and std::cout should do whatever stdin and stdout are
supposed to do.


Right, it is implementation defined (according to the C standard)
whether text streams (such as stdout) require a terminating newline
character.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Magnus Lie Hetland | last post by:
Is there any interest in a (hypothetical) standard graph API (with 'graph' meaning a network, consisting of nodes and edges)? Yes, we have the standard ways of implementing graphs through (e.g.)...
52
by: piaseckiac | last post by:
I am producing a website on air and need a link to change the entire website from standard to metric for temperature, pressure, miles-kilometers, and volume. Thank you.
6
by: John Bentley | last post by:
John Bentley writes at this level: If we think about our savings accounts then division never comes in (as far as I can see). We deposit and withdraw exact amounts most of the time. Occasionaly...
1
by: Corrie Meyer | last post by:
Announcement: SwiftReports standard edition 1.0 for Visual Studio ..NET 2003 released by UniSwift. We are pleased to announce the first release of a fully-managed reporting tool for the...
43
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
52
by: lovecreatesbeauty | last post by:
Why the C standard committee doesn't provide a standard implementation including the C compiler and library when the language standard document is published? C works on the abstract model of low...
132
by: Frederick Gotham | last post by:
If we look at a programming language such as C++: When an updated Standard comes out, everyone adopts it and abandons the previous one. It seems though that things aren't so clear-cut in the C...
1
by: manish deshpande | last post by:
Hi, When i'm installing MySQL-server-standard-5.0.24a-0.rhel3.i386.rpm by the following command: rpm -i MySQL-server-standard-5.0.24a-0.rhel3.i386.rpm the following error is being shown: ...
270
by: jacob navia | last post by:
In my "Happy Christmas" message, I proposed a function to read a file into a RAM buffer and return that buffer or NULL if the file doesn't exist or some other error is found. It is interesting...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.