c++ - 字符串有问题。调试声称 "bad pointer"

标签 c++ string error-code

我正在编写一个用作数字系统转换器的程序,但在搞砸之前甚至还没有进入有趣的数学和数值部分。

最后,我声明了一个字符串“value_array”,当我单步执行程序时,它上面有一个“bad ptr”注释。这阻碍了我继续前进。

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

using namespace std;

int main()
{
//Initialization:
int base = 0;
int target = 0;
int i = 0;

//Won't exit until the user inputs a viable number for a base (between 1 and 16 inclusively).
for (i = 0; i < 1; i += 0)
{
    cout << "Enter the base number system: ";
    cin >> base;
    cout << endl;

    if (base>=2 && base<=16)
    {
        i++;
    }

    else
    {
        cout << "Invalid value. Please input a value between 1 and 16 inclusively." << endl;
    }
}


//Same as before but makes sure the target is a valid number.

for (i = 0; i < 1; i += 0)
{
    cout << "Enter the target number system: ";
    cin >> target;
    cout << endl;

    if (target>=2 && target<=16)
    {
        i++;
    }

    else
    {
        cout << "Invalid value. Please input a value between 1 and 16 inclusively." << endl;
    }
}

string value_array = ""; //editted

cout << "Enter value in base (with no spaces): ";
cin >> value_array; //editted 

//int k = basevalue(value_array,base);//Please disregard. Can't use this function until the strings are usable.

return 0;
}

最佳答案

您可以使用 std::cinvalue_array ,但首先删除常量。您不能修改 const string .

编辑

你必须 #include <string> .

关于c++ - 字符串有问题。调试声称 "bad pointer",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32623549/

相关文章:

c++ - 两次使用命名空间

c++ - 在 const 成员不良做法或 UB 中修改 const 变量?

c++ - 从数组构造 vector

java - Java中如何将十六进制字符串转换为字节数组

字谜 C 程序如何(忽略大写字母)

c++ - 为什么这不是 C++ 中的内存泄漏?

Java 8 字符串垃圾收集

c++ - 有没有办法将 winapi 错误标志作为字符串获取?

mysql - 错误号 : 3780 Referencing column '%s' and referenced column '%s' in foreign key constraint '%s' are incompatible

c++ - 在 C++ 中定义错误代码常量的位置