Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Conversion from binary

Reply
Thread Tools

Conversion from binary

 
 
curiousEngine
Guest
Posts: n/a
 
      10-02-2007
Write a program to print out the integers from 40 to 127 in decimal,
octal,
hexadecimal and also print out the equivalent character.
************************************/
#include<iostream>
using namespace std;

void main(){
for (int i(40); i <= 127; i++){
cout<<"Decimal Value:"<<i;
cout<<"Octal Value:"<<oct<<i<<endl;
cout<<"Hexadecimal Value:"<<hex<<i;
cout<<"Character:"<<char(i);
}
}

how to refactor the above code?

 
Reply With Quote
 
 
 
 
Markus Moll
Guest
Posts: n/a
 
      10-02-2007
Hi

curiousEngine wrote:

> Write a program to print out the integers from 40 to 127 in decimal,
> octal,
> hexadecimal and also print out the equivalent character.
> ************************************/
> #include<iostream>
> using namespace std;
>
> void main(){
> for (int i(40); i <= 127; i++){
> cout<<"Decimal Value:"<<i;
> cout<<"Octal Value:"<<oct<<i<<endl;
> cout<<"Hexadecimal Value:"<<hex<<i;
> cout<<"Character:"<<char(i);
> }
> }
>
> how to refactor the above code?


Refactor? WHY? (Let me guess... you copied it from someone else and your
instructor will notice...)

Markus

 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      10-02-2007
"curiousEngine" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Write a program to print out the integers from 40 to 127 in decimal,
> octal,
> hexadecimal and also print out the equivalent character.
> ************************************/
> #include<iostream>
> using namespace std;
>
> void main(){
> for (int i(40); i <= 127; i++){
> cout<<"Decimal Value:"<<i;
> cout<<"Octal Value:"<<oct<<i<<endl;
> cout<<"Hexadecimal Value:"<<hex<<i;
> cout<<"Character:"<<char(i);
> }
> }
>
> how to refactor the above code?


Looks good to me. Although you have one endl after Octal Value, but not
after hte others so things will get cramped. And with that many you
probably don't want a new line anyway but a few spaces between them. I.E.

cout<<"Decimal Value:"<< i << " ";

and do the rest for the other lines.


 
Reply With Quote
 
Chris ( Val )
Guest
Posts: n/a
 
      10-02-2007
On Oct 3, 1:45 am, curiousEngine <(E-Mail Removed)> wrote:
> Write a program to print out the integers from 40 to 127 in decimal,
> octal,
> hexadecimal and also print out the equivalent character.
> ************************************/
> #include<iostream>
> using namespace std;
>
> void main(){
> for (int i(40); i <= 127; i++){
> cout<<"Decimal Value:"<<i;
> cout<<"Octal Value:"<<oct<<i<<endl;
> cout<<"Hexadecimal Value:"<<hex<<i;
> cout<<"Character:"<<char(i);
> }
>
> }
>
> how to refactor the above code?


You could try to incorporate some white space, new lines
and cascade the stream insertion operator '<<" to remove
many of the "cout" symbols in your code.

--
Chris Val


 
Reply With Quote
 
curiousEngine
Guest
Posts: n/a
 
      10-02-2007
No. I've written the code by myself.
Wanted to check if its the good one. i.e without having recourse to
another specific library to have the conversion done.

 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      10-02-2007
On Tue, 02 Oct 2007 15:45:25 -0000, curiousEngine
<(E-Mail Removed)> wrote in comp.lang.c++:

> Write a program to print out the integers from 40 to 127 in decimal,
> octal,
> hexadecimal and also print out the equivalent character.
> ************************************/
> #include<iostream>
> using namespace std;
>
> void main(){


Change the above to "int main()", which makes your program actual,
legal C++, as required by the language standard. Specifying a return
type for main() other than int makes the program ill-formed.

> for (int i(40); i <= 127; i++){
> cout<<"Decimal Value:"<<i;
> cout<<"Octal Value:"<<oct<<i<<endl;
> cout<<"Hexadecimal Value:"<<hex<<i;
> cout<<"Character:"<<char(i);
> }
> }
>
> how to refactor the above code?


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
x86 binary runs; x86_64 binary throws segfault Don C Programming 60 03-19-2010 05:58 AM
(8-bit binary to two digit bcd) or (8-bit binary to two digit seven segment) Fangs VHDL 3 10-26-2008 06:41 AM
writing binary file (ios::binary) Ron Eggler C++ 9 04-28-2008 08:20 AM
A 64-bit binary returning a value to a 32-bit binary? spammenotplui31@yahoo.ca C Programming 12 04-08-2007 07:02 AM
Re: ostreams, ios::binary, endian, mixed binary-ascii Marc Schellens C++ 8 07-15-2003 12:27 PM



Advertisments