0

我在这里粘贴了一些代码,每当输入多个数字时,标题中都会出现错误。我找不到 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
4

1 回答 1

0

@杰夫A。您缺少尾随 \0。– 埃里克塞德

是的,先生,在我看到您的回答之前就看到了这个。我需要空终止符的额外元素。谢谢!我知道这个项目很混乱,而且 C 比 C++ 多,但模板就是它。感谢所有帮助男孩和女孩!

于 2013-09-11T20:04:36.713 回答