【问题标题】:C++ I cannot cout one string while i successfully cout an another stringC++ 我无法计算出一个字符串,而我成功计算出另一个字符串
【发布时间】:2013-08-31 01:31:45
【问题描述】:

我有一个很奇怪的问题。 我正在做的是试图将字符串中的 8 位二进制数转换为十进制数(也是字符串) 在代码的末尾我计算出二进制字符串和十进制字符串但是当我运行时,我只成功地看到了二进制字符串而不是十进制字符串......

这是我的代码:

#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;


void bintodec(string bin);

int main()
{
    string bin="";
    bintodec(bin);
    return 0;
}

void bintodec(string bin)
{
    int temp;
    int temp_a;
    int temp_b;
    string dec;

    cout << "Enter the binary number: ";
    cin >> bin;

    temp = 128*(bin[0]-48) + 64*(bin[1]-48) + 32*(bin[2]-48) + 16*(bin[3]-48) + 8*(bin[4]-48) + 4*(bin[5]-48) + 2*(bin[6]-48) + (bin[7]-48);
    temp_a = temp%10;
    temp = (temp - temp_a) / 10;
    temp_b = temp%10;
    temp = (temp - temp_b) / 10;

    dec[2]=temp_a+48;
    dec[1]=temp_b+48;
    dec[0]=temp+48;
    dec[3]='\0';

    cout << endl << bin << " in decimal is: " << dec << endl;
}

这是运行结果:

输入二进制数:10101010

十进制的10101010是:

“is”之后应该是我的十进制数;但是什么都没有。我试图单独计算 dec[0] dec[1] 和 dec[2] 并且它有效,但是当我计算整个 dec 时,我每次都失败了......

谁能告诉我我的问题出在哪里?我认为我的代码有问题,但我可以弄清楚...

【问题讨论】:

  • 在盲目地开始写入不存在的元素之前,您是否考虑过为dec 字符串分配空间?一系列简单的push_back() 调用以正确的顺序将解决您的问题(我不建议您解决这个问题,因为您已经编写了代码,但这不是手头的问题)。
  • dec.length() 为零。对dec[N] 的所有分配都是缓冲区溢出,表现出未定义的行为。
  • 为什么你的函数需要一个参数,按值,然后在使用该值之前覆盖它?

标签: c++ string


【解决方案1】:

dec 的大小为零。但是您访问位置 0 到 3 的元素。 例如,您可以创建dec,并使用

将其初始化为适当的大小
string dec(4, ' '); // fill constructor, creates a string consisting of 4 spaces

而不是

string dec;

.

【讨论】:

  • 非常感谢!这真的解决了我的问题。我是字符串新手,在此之前我什至不知道如何定义字符串的大小...在此之前我一直在使用 char 数组...
【解决方案2】:

@SebastianK 已经解决了您的std::string 长度为零的事实。我想添加它而不是以下内容:

dec[2]=temp_a+48;
dec[1]=temp_b+48;
dec[0]=temp+48;
dec[3]='\0';

您可以使用push_back() 成员函数将字符追加到空的std::string

dec.push_back(temp + '0');
dec.push_back(temp_b + '0');
dec.push_back(temp_a + '0');

请注意,您不需要 NULL 终止 std::string,我使用字符文字 '0' 而不是 ASCII 值 48,因为我认为它更清晰。

【讨论】:

    【解决方案3】:

    注意:这只是一个带代码的注释,而不是实际的答案。

    int main()
    {
        string bin="";
        decode(bin);
    }
    
    void decode(string bin)
    {
    }
    

    这会导致在进入“decode”时创建一个新字符串,并将“bin”的内容复制到它。解码结束时,对 decode::bin 所做的任何更改对 main 都是不可见的。

    您可以避免这种情况 - 称为“按值传递”,因为您传递的是“bin”而不是“bin”本身的值 - 通过使用“按引用传递”

    void decode(string& bin)
    

    但在您的代码中,您实际上似乎根本不需要传递“bin”。如果您在解码后需要在 main 中使用“bin”,则可以考虑将其返回:

    int main()
    {
        string bin = decode();
    }
    
    string decode()
    {
        string bin = "";
    
        ...
    
        return bin;
    }
    

    但现在,只需从 main 中删除 bin 并使其成为 decode 中的局部变量。

    void bintodec();
    
    int main()
    {
        bintodec();
        return 0;
    }
    
    void bintodec()
    {
                std::string bin = "";
        cout << "Enter the binary number: ";
        cin >> bin;
    
        int temp = 128*(bin[0]-48) + 64*(bin[1]-48) + 32*(bin[2]-48) + 16*(bin[3]-48) + 8*(bin[4]-48) + 4*(bin[5]-48) + 2*(bin[6]-48) + (bin[7]-48);
        int temp_a = temp%10;
        temp = (temp - temp_a) / 10;
        int temp_b = temp%10;
        temp = (temp - temp_b) / 10;
    
                char dec[4] = "";
        dec[2] = temp_a+48;
        dec[1] = temp_b+48;
        dec[0] = temp+48;
        dec[3] = '\0';
    
        cout << endl << bin << " in decimal is: " << dec << endl;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-13
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      • 2014-11-14
      • 2014-12-25
      • 1970-01-01
      相关资源
      最近更新 更多