我正在编写一个用作数字系统转换器的程序,但在搞砸之前甚至还没有进入有趣的数学和数值部分。
最后,我声明了一个字符串“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::cin
在 value_array
,但首先删除常量。您不能修改 const string
.
编辑
你必须 #include <string>
.
关于c++ - 字符串有问题。调试声称 "bad pointer",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32623549/