Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Conversion from binary (http://www.velocityreviews.com/forums/t541444-conversion-from-binary.html)

 curiousEngine 10-02-2007 03:45 PM

Conversion from binary

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<<"Character:"<<char(i);
}
}

how to refactor the above code?

 Markus Moll 10-02-2007 03:49 PM

Re: Conversion from binary

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<<"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

 Jim Langston 10-02-2007 03:52 PM

Re: Conversion from binary

"curiousEngine" <curious.engine@gmail.com> wrote in message
> 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<<"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.

 Chris ( Val ) 10-02-2007 04:02 PM

Re: Conversion from binary

On Oct 3, 1:45 am, curiousEngine <curious.eng...@gmail.com> 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<<"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

 curiousEngine 10-02-2007 04:12 PM

Re: Conversion from binary

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.

 Jack Klein 10-02-2007 05:02 PM

Re: Conversion from binary

On Tue, 02 Oct 2007 15:45:25 -0000, curiousEngine
<curious.engine@gmail.com> 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<<"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

 All times are GMT. The time now is 01:34 PM.