Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 有绳子的问题。Debug声称;“指针不正确”;_C++_String_Error Code - Fatal编程技术网

C++ 有绳子的问题。Debug声称;“指针不正确”;

C++ 有绳子的问题。Debug声称;“指针不正确”;,c++,string,error-code,C++,String,Error Code,我正在写一个程序,它就像一个数字系统转换器,在搞砸之前,我甚至还没有完成有趣的数学和数值部分 最后,我声明了一个字符串“value_array”,当我单步执行程序时,它上面有一个“bad ptr”注释。这阻碍了我继续前进 #include <iostream> #include <ctype.h> #include <stdlib.h> #include <string> using namespace std; int main() { //

我正在写一个程序,它就像一个数字系统转换器,在搞砸之前,我甚至还没有完成有趣的数学和数值部分

最后,我声明了一个字符串“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;
int main()
{
//初始化:
int base=0;
int目标=0;
int i=0;
//在用户输入一个基数的可行数字(包括1到16之间)之前不会退出。
对于(i=0;i<1;i+=0)
{
cout>base;

cout=2&&base您可以在
value\u数组
上使用
std::cin
,但首先删除常量。您不能修改
常量字符串

编辑
您必须
#包括

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

很难说您遇到的坏指针错误的确切原因是什么。据调查,它可能是由于不适当地使用原始指针造成的。如果您在上述代码之后的某个地方使用它们,请确保它们中的任何一个在使用时是
NULL
nullptr
0


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

#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;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
//初始化:
int base=0;
int目标=0;
//PiotrSliwa:可以通过修改if语句来删除循环,如下所示
cout>base;
cin.ignore(1024,'\n');//PiotrSliwa:最多忽略1024个字符或直到找到换行符,以避免由超过所需的用户输入字符引起的错误

您以前是否使用过
std::cin
和输入操作符
>
,是什么让您认为它与
std::string
对象(如
value\u array
)不同?您可以使用
std::cin>>value\u array;
就像您之前在程序中所做的那样,例如
std::cin>
。这是我第一件事ied.我得到“没有运算符'>>“满足这些操作数”如果您遇到构建错误,请在您的问题中包含hem,完整且未编辑。我很抱歉。我想我只是做错了哦,您似乎有两个不同的问题,一个是编译错误,一个是崩溃/异常退出。您应该在这个问题中集中精力解决一个问题,并为另一个专业人士提出另一个问题错误。好的,我删除了常量,但“>>”仍显示错误。“没有运算符“>>”与这些操作数匹配”@rynoclank该错误通常会附带更多信息,请编辑您的问题以包含完整的错误输出。@rynoclank您可以在问题中添加完整的输出吗?就是这样。非常感谢@Alvarey!我会更新该问题。您知道为什么在我逐步执行程序时,value\u数组上有“坏ptr”吗?
#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;
}