I'm currently working on a code that:
takes 2 decimal numbers which are then displayed converted in octal, and hexadecimal, as well as in their original format, in a compact viewing table.
Currently, I'm restricted to a while
loop. and the code looks like:
cout << "Enter in two hexadecimal numbers that will be the beginning and end\n";
while (num1 <= num2){
// takes 2 hexadecimal inputs
cin >> hex >> num1; cin >> hex >> num2;
cout << "Decimal\tOctal\tHexadecimal\n";
cout << "***********************************\n";
It's not much, but this has been eating away at me for some time. I currently have no idea how to approach this.
Note: I don't know what to increment, or if I need another variable. If you could give advice, or point me in the right direction that would be great.
Firstly, you want to get your values, then loop from num1
to num2
with something like:
cout << "Enter in two hexadecimal numbers that will be the beginning and end\n";
cin >> hex >> num1; cin >> hex >> num2; //takes 2 hexadecimal inputs
while (num1 <= num2) {
// ... do stuff
++num1;
}
Then you probably just want io manipulators as you used for input to output num1
in your different bases in the "do stuff" part of the loop.
One thing - you probably want to start the loop after you read the values
cout << "Enter in two hexadecimal numbers that will be the beginning and end" << endl;
cin >> hex >> num1; cin >> hex >> num2; //takes 2 hexadecimal inputs
while (num1<=num2)
{
cout << num1 << " " << oct << num1 << " " << hex << num1 <<endl;
num1++;
}
I'd probably do something like this...
#include <iomanip>
.........
int num1 = 0;
int num2 = 0;
cout << "Enter number 1: ";
cin >> num1;
cout << "Enter number 2: ";
cin >> num2;
if(num1 > num2){
cout << "number 1 needs to be smaller than number 2; exiting...";
return 0;
}
cout << "Decimal\tOctal\tHexadecimal\n";
cout << "***********************************\n";
while(num1 <= num2){
cout << dec << num1 << " " << oct << num1 << " " << hex << num1 << endl;
num1++;
}
look into ios flags for c++
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.