字符串有问题。调试正在声明 "bad pointer"

Having trouble with a string. Debug is claiming "bad pointer"

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

最后,我声明了一个字符串 "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;
}

您可以在 value_array 上使用 std::cin,但首先要删除常量。您无法修改 const string.

编辑

你必须#include <string>

您遇到的问题是由 value_array 的常量引起的。你声明它 const 这意味着它在它的生命周期内不能被任何东西修改(它是只读的)。因此,当您尝试将从用户输入获得的字符串分配给它时,编译器会通知您这是不可能的。删除 const 关键字后一切正常。

很难说出您遇到的 错误指针 错误的确切原因是什么。根据 this answer 的说法,这可能是由于原始指针使用不当造成的。如果您在上述代码之后的某处使用它们,请确保它们中的任何一个在使用时是 NULLnullptr0


我稍微修改了你的代码并添加了一些注释,希望它们在你的程序开发过程中有用:

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

using namespace std;

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

    //PiotrSliwa: The loop can be removed with if-statement modified like below
    cout << "Enter the base number system: ";
    cin >> base;
    cin.ignore(1024, '\n'); // PiotrSliwa: ignore up to 1024 characters or until newline is found in order to avoid bugs caused by more-than-required user input characters
    cout << endl;

    if (base<2 || base>16)
    {
        cout << "Invalid value. Please input a value between 1 and 16 inclusively." << endl;
        return 0;
    }

    //PiotrSliwa: The loop can be removed with if-statement modified like below
    cout << "Enter the target number system: ";
    cin >> target;
    cin.ignore(1024, '\n');
    cout << endl;

    if (target<2 && target>16)
    {
        cout << "Invalid value. Please input a value between 1 and 16 inclusively." << endl;
        return 0;
    }

    string value_array = ""; //PiotrSliwa: removed 'const' from string which caused an error
    cout << "Enter value in base (with no spaces): ";
    cin >> value_array; //PiotrSliwa: The simplest method of obtaining user input and redirecting it to 'string' variable
    cin.ignore(1024, '\n');

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

    return 0;
}

Live demo


另外,考虑将功能包含在函数(或对象)中以避免代码重复,并提供可重用代码并提高可读性。例如:

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

using namespace std;

template<typename T>
T getUserInput(string message)
{
    cout << message;
    T input;
    cin >> input;
    cin.ignore(1024, '\n');
    cout << endl;
    return input;
}

bool isValidNumberSystem(int numberSystem)
{
    return numberSystem>=2 && numberSystem<=16;
}

int main()
{
    int base = getUserInput<int>("Enter the base number system: ");
    if (!isValidNumberSystem(base))
    {
        cout << "Invalid value. Please input a value between 1 and 16 inclusively." << endl;
        return 0;
    }

    int target = getUserInput<int>("Enter the target number system: ");
    if (!isValidNumberSystem(target))
    {
        cout << "Invalid value. Please input a value between 1 and 16 inclusively." << endl;
        return 0;
    }

    string value_array = getUserInput<string>("Enter value in base (with no spaces): ");

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

    return 0;
}

Live demo