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");
}
Try to change
to
('cause digit 0,1,... is not the same as char '0','1',...)
btw, there is an easy way to convert dec to hex