473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

how to refactor the above code?

Oct 2 '07 #1
5 1784
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

Oct 2 '07 #2
"curiousEngine" <cu************@gmail.comwrote in message
news:11**********************@r29g2000hsg.googlegr 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.
Oct 2 '07 #3
On Oct 3, 1:45 am, curiousEngine <curious.eng...@gmail.comwrote:
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
Oct 2 '07 #4
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.

Oct 2 '07 #5
On Tue, 02 Oct 2007 15:45:25 -0000, curiousEngine
<cu************@gmail.comwrote 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
Oct 2 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Aakash Bordia | last post by:
Hello, Does anybody know what is the documented and known behavior of inserting/updating binary columns using host variables from a client to a server which have different code pages? Will any...
5
by: john | last post by:
Here is the short story of what i'm trying to do. I have a 4 sided case labeling printer setting out on one of our production lines. Now then i have a vb.net application that sends data to this...
4
by: Ken Tough | last post by:
Seems like a simple thing to find out, but I'm struggling. I have googled, but everything I find is about implicit conversion, not explicit. Is this implementation-specific, or does ANSI/ISO...
9
by: bowsayge | last post by:
Inspired by fb, Bowsayge decided to write a decimal integer to binary string converter. Perhaps some of the experienced C programmers here can critique it. It allocates probably way too much...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
14
by: junky_fellow | last post by:
Can anybody please explain this: When a value with integer type is converted to another integer type other than _Bool, if the new type is unsigned, the value is converted by repeatedly...
4
by: Russell Warren | last post by:
I've got a case where I want to convert binary blocks of data (various ctypes objects) to base64 strings. The conversion calls in the base64 module expect strings as input, so right now I'm...
15
by: David Marsh | last post by:
I accidentally typed %b instead of %d in a printf format string and got a binary representation of the number. Is that standard C or a compiler extension?
4
by: dondigitech | last post by:
I want to convert hex to binary without losing bits. I want to preserve the 8-bits because I ultimately need a 24-bit string to grab information from. I am just using this line of code for the...
6
by: i_robot73 | last post by:
I have a file, containing hex values for dates (MMDDYYYY)<status code><??such as: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.