Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In this code a hexadecimal number, such as 1a is entered, followed by an octal number such as 777. However when inputs are given the outputs are not as expected from a conversion between bases.

#include <iostream>
using namespace std;
	int main()
	{
		int hexin, octalin;
		cout << ("Please enter two space-separated positive integer numeric values. \nPleasue ensure the first is in hexadecimal and the second is in octal.\n");
		cin >> hexin >> octalin;
		cout << hex
			<< hexin		<< " hexadecimal = " 
			<< oct << hexin	<< " octal = " 
			<< dec << hexin	<< " decimal\n" 
			<<  octalin << " octal = " 
			<<hex	<< octalin  << " hexadecimal = " 
			<<dec	<< octalin	<< " decimal";
		return 0;
	}


Input: 1a 777

Output:
1 hexadecimal = 1 octal = 1 decimal
0 octal = 0 hexadecimal = 0 decimal

Am I using output formatters incorrectly in some way?

What I have tried:

I have tried using hex and octal in as a string and as an integer. I have also tried using std::cout with std::hex, oct, etc. Neither gave accurate results.
Posted
Updated 14-Jul-20 19:09pm

Quote:
Why is my output not correct when utlizing cout<<hex

What magic are you using to tell your program that first input is hex and second is oct ?
In C++ hew and oct number have a special syntax if you want your input to be recognized as numbers in those bases. It is bocumented if you search internet.
Try input with: 0x1A and 0777
 
Share this answer
 
v3
Comments
CPallini 15-Jul-20 1:59am    
5.
Patrice T 15-Jul-20 2:20am    
Thank you.
Your output looks fine. It's the input where you're going wrong.
Trying to parse "1a 777" as integers, it will get "1" then nothing ("a" doesn't parse as an integer).
You want something along the lines of
C++
cin >> hex >> hexin;
cin >> oct >> octin;

Sorry I don't have C++ to hand but that should get you pointed in the right direction.

oops got arrows wrong.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900