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条)

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭