【问题标题】:Decimal to Hex program acting up十进制到十六进制程序起作用
【发布时间】:2014-12-04 07:54:32
【问题描述】:

我目前正在开发一个程序,将十进制输入转换为其十六进制等效值。我计划使用向量系统地收集值,然后将它们反向吐出(因此以十六进制表示)。但是我遇到了很多问题。直到 16 的所有输入都按预期工作,然后事情变得很奇怪。输入 16 会导致系统输出笑脸,输入 18 会导致 2 种不同颜色的笑脸,输入 23 会导致系统发出哔哔声。

这应该是一个相对简单的代码,它只是以我从未见过的方式运行!希望这些信息有帮助

编辑:我知道 std::hex 函数,虽然这是一个类,不幸的是我们不允许使用它。

#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");
 }

【问题讨论】:

  • 您知道吗,您可以使用 std::hex 在 std::out 上将任何整数打印为十六进制?
  • 也许你会考虑换成这样的:cout
  • 没有时间给出完整的答案,但你的代码的最后一部分很糟糕:鉴于A 的 ASCII 码是 65,你可以使用类似 remainderLetter=remainderCount2+55; 或更好的 @ 987654324@

标签: c++ vector beep


【解决方案1】:

尝试改变

userInfo[i] = remainderCount2;

userInfo[i] = remainderCount2 + '0';

('因为数字 0,1,... 与 char '0','1',...不同)

顺便说一句,有一种简单的方法可以将 dec 转换为 hex

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

【讨论】:

    猜你喜欢
    • 2011-12-09
    • 2023-03-14
    • 1970-01-01
    • 2018-07-26
    • 2018-02-23
    • 2014-08-24
    • 1970-01-01
    • 2012-03-10
    • 2016-01-05
    相关资源
    最近更新 更多