变量'inputValue'周围的堆栈已损坏

时间:2013-09-11 17:30:06

标签: c++ stack corrupt

我有一些代码粘贴here,只要输入多个数字,就会在标题中给出错误。我找不到一个不正确访问inputValue的地方,但我可以使用另一组(少数)眼睛。谢谢你提前帮忙!

// descriptions of declared functions listed with implementation
int ProcessInput(const int fromBase, const int toBase, char inputValue[],
                 const int length);

void ConvertToBase(int decimal, const int inBase, char outPut[]);

double Exponent(const int number, const int exponent);

int main(int argsc, char** argsv)
{
    int fromBase(0), toBase(0);

    // set bases and initialize vector for storing output until end of input
    fromBase = atoi(argsv[1]);
    toBase = atoi(argsv[2]);
    vector <char *> outPutValues;
    int count(0);

    while (!cin.eof())
    {
        // array to hold input value
        char inputValue[8] = {' '};

        // receive input from keyboard and convert input to decimal and new base
        while (cin >> inputValue)
        {
            int length(strlen(inputValue));

            // using dynamic arrays in vector allows for multiple input
            //    with no output until all input received
            outPutValues.push_back(new char[8]);

            int decimalValue = ProcessInput(fromBase, toBase, inputValue,
                                            length);

            // if the new base is decimal, simply output decimalValue;
            //    else process the new base and store in an output array;
            ConvertToBase(decimalValue, toBase, outPutValues[count]);

            count++;
        }


        for (int c = 0; c < outPutValues.size(); c++)
        {
            int k(0);
            // display only set values of the output array
            while ((outPutValues[c][k] == '\0') || (outPutValues[c][k] == '0'))
                k++;

            while (k < 8)
            {
                cout << outPutValues[c][k];
                k++;
            }

            cout << endl;
        }
    }

    // free up dynamic arrays
    for (int d = 0; d < outPutValues.size(); d++)
        delete outPutValues[d];

    return 0;
} // end main

// ProcessInput receives the input array and translates the char values
//    into a decimal value to be returned to calling function
int ProcessInput(const int fromBase, const int toBase, char inputValue[],
                 const int length)
{
    int exponent(0), decimalValue(0);

    for (int j = 0; j < length; j++)
    {
        exponent = (length - j - 1);

        // convert inputValue from char to int to be processed into decimal;
        //    negative signs are ignored
        switch (inputValue[j])
        {
            case 'a':
            case 'A': decimalValue += (10 * Exponent(fromBase, exponent)); break;
            case 'b':
            case 'B': decimalValue += (11 * Exponent(fromBase, exponent)); break;
            case 'c':
            case 'C': decimalValue += (12 * Exponent(fromBase, exponent)); break;
            case 'd':
            case 'D': decimalValue += (13 * Exponent(fromBase, exponent)); break;
            case 'e':
            case 'E': decimalValue += (14 * Exponent(fromBase, exponent)); break;
            case 'f':
            case 'F': decimalValue += (15 * Exponent(fromBase, exponent)); break;
            case '-': cout << "Negative Number Received. Converted to unsigned int.\n"; break;
            default:  decimalValue += ((inputValue[j] - '0') * Exponent(fromBase, exponent)); break;
        }
    }
    return decimalValue;
} // end ProcessInput


// ConvertToBase converts the decimal form of a number into
//    a new base by dividing the decimal until 0 and
//    storing the remainders in the output array
void ConvertToBase(int decimal, const int inBase, char outPut[])
{
    int remainder(0);

    // char variable used to convert int back into char
    //    for output
    char intToChar('0');

    for (int i = 7; i >= 0; i--)
    {
        remainder = decimal % inBase;
        decimal /= inBase;

        // account for hex and if remainder is an int,
        //    store as char in output array using char intToChar
        switch (remainder)
        {
            case 10: outPut[i] = 'A'; break;
            case 11: outPut[i] = 'B'; break;
            case 12: outPut[i] = 'C'; break;
            case 13: outPut[i] = 'D'; break;
            case 14: outPut[i] = 'E'; break;
            case 15: outPut[i] = 'F'; break;
            default: for (int j = 0; j < remainder; j++) {intToChar++;}
                outPut[i] = intToChar;
                intToChar = '0';
                break;
        }
    }
}  // end ConvertToBase


// converts input to decimal form by multiplying
//    each number place by its base and exponent
double Exponent(const int number, const int exponent)
{
    double value(1);
    for (int i = 1; i <= exponent; i++)
        value *= number;
    return value;
} // end Exponent

1 个答案:

答案 0 :(得分:0)

  

@JeffA。你错过了尾随\ 0。 - erikced

是的先生,在我看到你的答案之前就看到了这一点。我需要null终止符的额外元素。谢谢!我知道这个项目比C ++更乱,更C,但模板就是这样。感谢所有帮助人员和女孩们!