Help with outputting 2 decimal places...

D

David Casey

I'm trying to output 2 decimal places to the screen even when the last
digit would be 0. Here is what I have now:

#include <iostream.h>
#include <iomanip.h>

double custname=0;

void main()
{
custname=12.5;
cout << setprecision(4) << custname;
}

When I set custname to a value like 12.59 it prints 12.59. But the above
only prints 12.5. I've searched and tried several different ways on Google
and nothing seems to work.

Just so you know, I'm just starting out with C++. Thanks in advance for
any help anyone can provide.

Dave
--
You can talk about us, but you can't talk without us!
US Army Signal Corps!!
http://www.geocities.com/davidcasey98

Remove "IH8SPAM" to reply!
 
R

red floyd

David said:
I'm trying to output 2 decimal places to the screen even when the last
digit would be 0. Here is what I have now:

#include <iostream.h>
#include <iomanip.h>

double custname=0;

void main()
{
custname=12.5;
cout << setprecision(4) << custname;
}

When I set custname to a value like 12.59 it prints 12.59. But the above
only prints 12.5. I've searched and tried several different ways on Google
and nothing seems to work.

Just so you know, I'm just starting out with C++. Thanks in advance for
any help anyone can provide.

Dave

1. You're using old headers.
2. use showpoint or fixed
3. Main returns int

#include <iostream>
#include <iomanip>

using namespace std;

double custname = 0;
int main()
{
custname = 12.5;
cout << setprecision(4) << showpoint << custname << endl;
cout << setprecision(4) << fixed << custname << endl;
return 0;
}


generates:

12.50
12.5000


}
 
D

David Casey

1. You're using old headers.

Yes, fixed that now. :)
2. use showpoint or fixed

I hadn't heard of those commands. The teacher was telling us to just put
cout.precision(2) right inside of main, but that wasn't working.
3. Main returns int

Yes, I know but the teacher hasn't "shown" us that part yet. I know a
little more than the teacher is telling us because it's just a programming
logic class and I've done a little reading on my own regarding C++. I
didn't want to use stuff the teacher hasn't shown us in class.
#include <iostream>
#include <iomanip>

using namespace std;

double custname = 0;
int main()
{
custname = 12.5;
cout << setprecision(4) << showpoint << custname << endl;
cout << setprecision(4) << fixed << custname << endl;
return 0;
}


generates:

12.50
12.5000


}

I just gave this a shot and it worked good:

#include <iostream>
#include <iomanip>

using namespace std;

float custname=0;
float roomnum=0;

void main()
{
cout.setf(ios_base::fixed, ios_base::floatfield);
cout.precision(2);
cout << custname << endl;
cout << roomnum << endl;
}

It also seems to toggle on the 2 decimal place precision which is what I
was looking for and I think the teacher meant but I don't know why he
didn't tell us about the cout.setf line. I also figured out that without
the using namespace std; line, it doesn't work either and that's also
something the teacher hasn't told us to use so again, I wasn't going to go
over what he was already showing us. However, in this case I think I'll
just use it (like I have a choice).

Thanks for the help, though. It gives me another choice to use. :)

Dave
--
You can talk about us, but you can't talk without us!
US Army Signal Corps!!
http://www.geocities.com/davidcasey98

Remove "IH8SPAM" to reply!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top