遇到字符串问题。调试声称"坏指针"

时间:2015-09-17 06:07:09

标签: c++ string error-code

我正在编写一个程序,作为一个数字系统转换器,并且在搞砸之前甚至没有把它变成有趣的数学和数字值部分。

最后我宣布了一个字符串" value_array"它有一个坏的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;
}

2 个答案:

答案 0 :(得分:1)

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

修改

您必须#include <string>

答案 1 :(得分:0)

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

很难说出遇到错误指针错误的确切原因是什么。根据{{​​3}},它可能是由于原始指针的使用不当造成的。如果您在上述代码之后的某处使用它们,请确保使用它们时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;
}

this answer

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

#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