Empty_Android 2016-08-30 05:35 采纳率: 70.6%
浏览 1325
已采纳

C++ 十六进制转换输出错误

书上的十六进制转换的例子,输出时出现错误

 #include <iostream>
using namespace std;

int main() {
    const string hexdigits = "0123456789ABCDEF";
    cout << "Enter a series of numbers between 0 - 15" <<
            "separated by space . Hit Enter when finished : " <<endl;
    string result;
    string::size_type n;
    while (cin >> n)
        if (n < hexdigits.size())
            result += hexdigits[n];
    cout << result << endl;
}

错误信息

 GNU gdb (GDB) 7.6.1
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "mingw32".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
No symbol table is loaded.  Use the "file" command.
[New Thread 200.0x1030]
Quit (expect signal SIGINT when the program is resumed)

  • 写回答

3条回答 默认 最新

  • 小灸舞 2016-08-30 05:57
    关注

    你确定你没有打错代码,参考下面这个:

     #include <iostream>  
    #include<string>  
    
    using namespace std;
    
    
    int  main()
    {
        const string hexdigits = "0123456789ABCDEF";//可能的十六进制数值  
        cout << "请输入0-15之间的数值:";
    
        char result;    //用于保存十六进制的字符  
        string::size_type n;    //用于保存从输入流读取的数据  
    
        while (cin >> n)
        if (n<hexdigits.size())   //忽略无效的输入  
        {
            result = hexdigits[n];    //得到对应的16进制的数  
            cout << n << "对应的十六进制数是:" << result << "  " << endl;
        }
        else cout << n << "无效的输入" << endl;
    
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao