0

I'm currently working on a program to convert a decimal input into its hexadecimal equivalent. I plan on using a vector to systematically collect the values and then spit them out in reverse (Thus in hex). However I have run into numerous problems. All inputs up until 16 work as intended, then things get weird. Entering 16 results in the system outputting a smiley face, inputting 18 results in 2 different colored smiley faces, 23 results in the system beeping.

This should be a relatively straightforward code, it's just been acting up in ways I've never seen! Hope this info helps

EDIT: I'm aware of the std::hex function, though this is for a class and we are not allowed to use it unfortunatley.

#include <iostream>
#include <string>
#include <vector>
#include <iomanip>

using  namespace std;

int main(){

int input, tester = 0, switchStuff, county = 0, remainderCount = 1, inputCount, remainder = 1, remainderCount2, inputCount2;
string stopGo;
char remainderLetter;

do{

   while (tester != 1){
  cout << "Enter the number you would like to convert to Hex format: ";
  cin >> input;
  if (cin.fail()){                     //check if user input is valid
     cout << "Error: that is not a valid integer.\n";
     cin.clear(); cin.ignore(INT_MAX, '\n');     //Clear input buffer
     continue;  //continue skips to top of loop if user input is invalid, allowing another attempt
  }
  else{
     tester = 1;     //Tester variable allows loop to end when good value input
  }
 }

inputCount = input;

while(inputCount != 0){

  remainderCount = inputCount % 16;
  inputCount = (inputCount - remainderCount) / 16;
  county++;

   }

vector<string>userInfo(county);

inputCount2 = input;

for (int i = 0; county > i; i++){

  remainderCount2 = inputCount2 % 16;
  inputCount2 = (inputCount2 - remainderCount2) / 16;

  if (remainderCount2 == 10){
     remainderLetter = 'A';
  }
  else if (remainderCount2 == 11){
     remainderLetter = 'B';
  }
  if (remainderCount2 == 12){
     remainderLetter = 'C';
  }
  else if (remainderCount2 == 13){
     remainderLetter = 'D';
  }
  if (remainderCount2 == 14){
     remainderLetter = 'E';
  }
  else if (remainderCount2 == 15){
     remainderLetter = 'F';
  }

  if (remainderCount2 >= 10){
     userInfo[i] = remainderLetter;
  }
  else{
     userInfo[i] = remainderCount2;
  }


}

cout << "The result in Hexadecimal format is: ";    

for (int i = 0; i < county; i++)
  cout << userInfo[i];

cout << endl << "Would you like to continue? (Enter Yes/No): ";     //Check whether to continue or not
cin >> stopGo;
cout << endl;

tester = 0;

}while ((stopGo.compare("Yes") == 0) || (stopGo.compare("yes") == 0) || (stopGo.compare("y") == 0) || (stopGo.compare("Y") == 0));   //Leaves user with a range of ways to say 'yes'

cout << "Thank you for using this program!" << endl;

system("pause");
 }
Dozar
  • 71
  • 1
  • 2
  • 6
  • 1
    Are you aware, that you can use std::hex to print any integer numer as hex on std::out? – MikeMB Dec 04 '14 at 08:01
  • 1
    Maybe you will consider switching to something like this: cout< – MateuszZawadzki Dec 04 '14 at 08:01
  • No time for a complete answer, but the last part of your code is terrible: given that the ASCII code for `A` is 65, you can use that with something like `remainderLetter=remainderCount2+55;` or better `remainderLetter=remainderCount2+'A'-10;` – kebs Dec 04 '14 at 08:17

1 Answers1

0

Try to change

userInfo[i] = remainderCount2;

to

userInfo[i] = remainderCount2 + '0';

('cause digit 0,1,... is not the same as char '0','1',...)

btw, there is an easy way to convert dec to hex

std::string sDecimal[] = "155";
char xHex[50];
sprintf(xHex, "%X", atoi(sDecimal.c_str()));
Fl0
  • 149
  • 5