Conversion from binary

Discussion in 'C++' started by curiousEngine, Oct 2, 2007.

  1. 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?
     
    curiousEngine, Oct 2, 2007
    #1
    1. Advertising

  2. curiousEngine

    Markus Moll Guest

    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
     
    Markus Moll, Oct 2, 2007
    #2
    1. Advertising

  3. curiousEngine

    Jim Langston Guest

    "curiousEngine" <> wrote in message
    news:...
    > 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.
     
    Jim Langston, Oct 2, 2007
    #3
  4. On Oct 3, 1:45 am, 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?


    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
     
    Chris ( Val ), Oct 2, 2007
    #4
  5. 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.
     
    curiousEngine, Oct 2, 2007
    #5
  6. curiousEngine

    Jack Klein Guest

    On Tue, 02 Oct 2007 15:45:25 -0000, curiousEngine
    <> 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
     
    Jack Klein, Oct 2, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor Hannak
    Replies:
    0
    Views:
    7,520
    Victor Hannak
    Feb 20, 2004
  2. kelvSYC
    Replies:
    2
    Views:
    4,778
    Brad BARCLAY
    Dec 30, 2003
  3. Delali Dzirasa
    Replies:
    4
    Views:
    1,045
    Delali Dzirasa
    Sep 15, 2003
  4. Raja
    Replies:
    2
    Views:
    15,033
  5. , India
    Replies:
    2
    Views:
    506
    Fraser Ross
    Sep 15, 2009
Loading...

Share This Page