C++ and C difference

E

EvanB

How could I convert the following C code into a C++ equivalent? I have no
idea how to do the formatting in C++.

void OutputPay(Employee *emp){
printf(“%06X %8.3f\n”,emp->DID, emp->DPay);
emp->DPaid = TRUE;
}

Thanks in advance,
Evan
 
V

Victor Bazarov

EvanB said:
How could I convert the following C code into a C++ equivalent? I have no
idea how to do the formatting in C++.

void OutputPay(Employee *emp){
printf(“%06X %8.3f\n”,emp->DID, emp->DPay);
emp->DPaid = TRUE;
}

RTFM on "iostream manipulators". Pay attention to 'hex' format flag,
'setfill' manipulator, 'setw' manipulator, 'fixed' format flag, and
'setprecision' manipulator.

V
 
M

Mike Wahler

EvanB said:
How could I convert the following C code into a C++ equivalent?

Your code qualifies as "C++ code".
I have no
idea how to do the formatting in C++.

void OutputPay(Employee *emp){
printf(“%06X %8.3f\n”,emp->DID, emp->DPay);
emp->DPaid = TRUE;
}

C++ introduces 'IOStreams', 'stream manipulators',
'stream format flags', etc.:

void OutputPay(Employee *emp)
{
std::cout << std::hex
<< std::setfill('0')
<< std::setw(6}
<< emp->DID
<< std::dec
<< std::fixed
<< std::setprecision(3)
<< std::setw(11)
<< emp-DPay);

emp->DPaid = true; /* 'true' is a C++ keyword */
}

See the declarations of the <ios> and <iomanip> headers.

Recommended books:
http://www.josuttis.com/libbook/
http://www.langer.camelot.de/iostreams.html

-Mike
 
M

Mike Wahler

I left out one item:
void OutputPay(Employee *emp)
{
std::cout << std::hex
<< std::setfill('0')
<< std::setw(6}
<< emp->DID
<< std::dec

<< std::setfill(' ')
<< std::fixed
<< std::setprecision(3)
<< std::setw(11)
<< emp-DPay);

emp->DPaid = true; /* 'true' is a C++ keyword */
}

(All formatting changes applied to a stream remain in effect
until specifically changed again -- except 'setw()', which
must be invoked each time a field width need be specified).

-Mike
 
V

Vyacheslav Kononenko

EvanB said:
How could I convert the following C code into a C++ equivalent? I have no
idea how to do the formatting in C++.

void OutputPay(Employee *emp){
printf(“%06X %8.3f\n”,emp->DID, emp->DPay);
emp->DPaid = TRUE;
}

Thanks in advance,
Evan

void Employee::OutputPay(){
std::cout << boost::format(“%06X %8.3f\n”) % DID % DPay;
DPaid = true;
}

and variations
 
H

Howard

Vyacheslav Kononenko said:
void Employee::OutputPay(){
std::cout << boost::format(%06X %8.3f\n) % DID % DPay;
DPaid = true;
}

and variations

Technically speaking, that's not standard C++, is it? Isn't the Boost
library third-party software? (I don't see anything related to Boost in my
libraries, at least.)

Also, isn't that a typo? I think you meant to use the << operators before
DID and DPay, right?

-Howard
 
J

JKop

Howard posted:
Technically speaking, that's not standard C++, is it? Isn't the Boost
library third-party software? (I don't see anything related to Boost
in my libraries, at least.)

Also, isn't that a typo? I think you meant to use the << operators
before DID and DPay, right?

-Howard


Then copy-paste the boost file. Now it's Standard C++!


( or would that be piracy? ;-P )


-JKop
 
E

EvanB

Awesome, thanks. That helps tremendously :) The spacing operations were
throwing me off in C++.

Evan
 
D

David Hilsee

Howard said:
Technically speaking, that's not standard C++, is it? Isn't the Boost
library third-party software? (I don't see anything related to Boost in my
libraries, at least.)

Well, to be fair, the OP didn't ask for a solution that only used the
standard library. The boost library is a good library that one can download
from boost.org, so I think it deserves to be mentioned.
Also, isn't that a typo? I think you meant to use the << operators before
DID and DPay, right?

No, boost::format overloads the % operator. See
http://www.boost.org/libs/format/doc/format.html
 

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

Forum statistics

Threads
474,183
Messages
2,570,965
Members
47,512
Latest member
FinleyNick

Latest Threads

Top