>For examle: When a user inputs '211', the program should return a string containing 'd3'.
>I would like someone to tell me if there is a more efficient way..
Yes, there is.
Code:
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdlib>
using namespace std;
/*
* Decimal => Hexadecimal converter
* Copyright (c) Liz Guth 2003
*/
int main()
{
long decimal;
cout<<"Enter a number: "<<flush;
if (!(cin>> decimal))
{
cerr<<"Whoops, invalid input";
exit(EXIT_FAILURE);
}
ostringstream convert;
convert<< hex << decimal;
cout<<"Your number, "<< decimal <<", is "
<< convert.str() <<" in hexadecimal."<<endl;
}