Hello,

I have the following code:

`#include <iostream.h>

void main()
{
    double test=3.46578953218549;
    cout.setf(ios::dec);
    cout<<test;
}`

I understand that iostream.h is outdated and Microsoft Visual Studio has iostream.
However, When I remove the ".h" the setf, ios, etc, line 6 has multiple errors.
So, my question is: How do I set the number of decimal places without having "setprecision(51)" in every cout line, as such:

`#include <iostream.h>

void main()
{
    double test=3.46578953218549;
    cout<<std::setprecision(51)<<test;
}`

Or, where can I download iostream.h?

Joe

cout and pretty much everything else in the Standard Template Library (i.e. the header files without the .h at the end) are members of the std namespace.

On line #2 add in: using namespace std;

There are several ways to correct the problem: add using std::cout after the includes and before any functions, or on line 6 use std::cout

Glorious. Thank you for the help DeanMSands3 and Ancient Dragon!

But, what does that code mean?
What does namespace actually mean? What does it do?
Why is is std::cout required if I already included iostream?

To give you a general idea of my experience: I'm a junior electrical engineer with a concentration in computers at a Western New England University. I know functions, classes, pointers, templates, structs, etc. I've made my own little complier. I've simply never played with cout and cin. I've stuck to printf and fprintf.

Also, which is "better"? (cout & cin vs. printf & fprintf);

Read this article to understand namespaces.

cout and cin replace printf() and scanf() respectively. Which is better? c++ program should generally use c++ conformant functions and classes. Although most C functions can be used in c++ programs, it is customary to use c++ functions whever possible. If you are more comfortable with C functions such as printf() then you might want to confinue using them while learning c++ language, then change over gradually to c++ cin function.

I understand that iostream.h is outdated and Microsoft Visual Studio has iostream.

The headers like <iostream.h> are pre-standard (from before C++ was standardized), so it dates back to the 90s or earlier. Most compilers probably still support it for legacy reasons, but they don't have to, and some don't.

However, When I remove the ".h" the setf, ios, etc, line 6 has multiple errors.

This is because of namespaces. The old ".h" version of the I/O stream library pre-dates the introduction of namespaces into the C++ language. Since standardization in the 90s, all components (classes, constants and functions) are required to be within the std namespace, before that, they were in the global namespace.

Also, these functions you refer to, like "setf", "setw" or "setprecision", are technically obtained from the standard header <iomanip>, not <iostream> (although on many compilers, including <iostream> also includes <iomanip>).

But, what does that code mean?
What does namespace actually mean? What does it do?

A namespace is, as its name implies, a space for names. The idea is simple. If you have all these standard libraries, plus your own code, plus the entirety of the codes and libraries that you could grab from the internet, it is pretty much inevitable that there would be name conflicts between them (e.g., two separate libraries choosing to use the same name for a component that they provide). In the old days (i.e., in older languages that don't have namespaces), people would generally have to name all there functions with some unique prefix, like "MyUniqueLibrary_ComputeValue()" instead of just "ComputeValue()", because otherwise, somebody who uses your library could also be using some other library that just happens to have name conflicts with yours. In C++ (and similarly in other languages), this problem is solved with namespaces (or a similar concept). You start a namespace by writing namespace MyLib { /* ... */ }; where you insert whatever code (functions, classes, constants, etc.) you want to put in that namespace. Then, internally to the namespace, you can use all the other components of the same namespace simply by their short name (unqualified name), and the compiler will assume that you are referring to the components within the current namespace (and not those of some other library), and thus, resolving the problem of name-clashes and allowing library programmers to use simple short names within their libraries as opposed to long prefixed names. That said, outside a particular namespace (like std), if you want to call or access a component of that namespace, you will need to specify the namespace in which to find the component, e.g., by writing std::cout (instead of just cout). Furthermore, if you are going to use that particular component a lot, you can write using std::cout; at the start of a function (or file) and after that, you only need to refer to it as cout for the compiler to understand that you mean std::cout. If you are going to use many components of one particular namespace (like std), and that you are sure that none of your code (or any third-party library that you include) has name conflicts with components of that namespace, then you can write using namespace std; to tell the compiler to import all the components of that namespace.

Why is is std::cout required if I already included iostream?

std::cout is simply the fully-qualified name for the standard component cout from the iostream library.

Also, which is "better"? (cout & cin vs. printf & fprintf);

It is somewhat a matter of preference. Mostly, once you get used to cout/cin (and other IO stream components) and get used to the general C++ style for programming, the whole printf / scanf style goes by the way side. There are some technical reasons why the C-style functions are bad, and there are some cases in which the C++-style functions are a bit slower (but in general, they are faster). Overall, it is mostly a matter of style, and using C++ style IO is probably going to help you warm up to C++ style programming (generic programming, value semantics, and RAII).

@DeanMSands3:

cout and pretty much everything else in the Standard Template Library

cout and other I/O stream components are not part of the Standard Template Library (STL), but of the IO-stream library.

Woops, I apoligize for my extra "is".

Thank you Ancient Dragon for the link.
Mike, thank you, thank you, thank you, for explaining everything!
A few things that you described reminds me of overloading the operator functions one too many times.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.