format commands in C++

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

format commands in C++

3,831 Views
dmat619
Contributor I
I'm a student learning C++.  Our textbook is "Big C++" by Horstman.  The book came with a CD containing Code Warior Development Tools for Windows Ver 8.0. 
I am having trouble formating output.  Im using the following command:
cout << fixed << setprecision(2);
and the compiler complains.  I have tried to find the correct format for this command with no luck.  I can not find common header files on my disk, or any reference to any on the Metroworks site.
Can anyone tell me what is wrong with the above command?
Thanks,
dmat619
Labels (1)
0 Kudos
4 Replies

327 Views
mccPaul
Contributor I
Hi
 
To use cout you need to #include <iostream.h>.
 
Without the C compiler error it is hard to say what else is wrong. I suspect that setprecision(2) should be precision(2) as I think that setprecision is probably the member function of cout that does the same job as the precision(2) iostream keyword. It does depend on your C++ std library though.
 
You should find out who owns and supports CodeWarrior for Windows now, or maybe you should get the free Express version of Visual Studio from Microsoft if you can't find who supports CW for Windows.
 
Cheers,
 
Paul.
0 Kudos

327 Views
dmat619
Contributor I
Thanks Paul,
I have included iostream, and the text book uses setprecision().  I have a class tonight, so I'll check with the instructor.  Your right, I need to find out who supports code warrior for windose.  I  think I'll try tech support for that one.  All I can find on google is links to Metroworks which bring me to Freescale.
0 Kudos

327 Views
CompilerGuru
NXP Employee
NXP Employee
That single line looks OK to me, so I guess the problem is in the surrounding code which is not provided.
Please provide a complete source file for such reports and also provide the exact compiler error messages (Ctrl-C works!).

With a different version of CW, I'm able to compile the code below.

#include <iostream>
#include <iomanip>
using namespace std;
void test(void) {
    cout << fixed << setprecision(2);
}

0 Kudos

327 Views
J2MEJediMaster
Specialist I
Metrowerks was bought by Freescale (technically by a division of Motorola, but that was spun off to become Freescale), and CodeWarrior is used for embedded systems work now. The technology for writing windows appllications with CodeWarrior was sold off some time ago so that we no longer support Windows platform development. Nevertheless, I'll try to help.

I'm not familiar with the Windows version of CodeWarrior, but I'd look in the CodeWarrior reference material to see if that command is supported. You might want to start studying the formatting capabilities of the printf() function to format your output.

Sorry that I can't be much more help than that.

---Tom
0 Kudos