Conversion from binary

C

curiousEngine

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?
 
M

Markus Moll

Hi
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
 
J

Jim Langston

curiousEngine said:
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.
 
C

Chris ( Val )

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.
 
C

curiousEngine

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.
 
J

Jack Klein

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
 

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

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top