试图添加两个整数但向量错误

时间:2015-12-27 18:27:17

标签: c++ string vector

我一直在尝试将两个数字作为字符串读取,将它们转换为int向量,然后将它们添加到我在学校的实验室中。我尝试添加1 + 1来查找此错误:

  

Debug Assertion失败!

     

程序:C:\ windows \ SYSTEM32 \ MSVCP140D.dll文件:c:\ program files   (x86)\ microsoft visual studio 14.0 \ vc \ include \ xstring Line:1681

     

表达式:向量下标超出范围

     

有关程序如何导致断言的更多信息   失败,请参阅关于断言的Visual C ++文档。

     

(按“重试”调试应用程序)

我已尝试重试,但它打开了另一个对话框,说调试到达了一个断点,此时我无法继续调试。这是我感兴趣的代码(Microsoft Visual Studio编译器):

#include <iostream>
#include <vector>
#include <string>

using namespace std;

void input(string &largeString1, string &largeString2);
void convert(string largeString1, string largeString2, vector<int> &largeInt1, vector<int> &largeInt2);
int asciiToInt(char ch);
void add(vector<int> largeInt1, vector<int> largeInt2, vector<int> &finalInt);
void output(const vector<int> finalInt);

int main()
{
    string largeString1;
    string largeString2;
    vector<int> largeInt1(12, 0);
    vector<int> largeInt2(12, 0);
    vector<int> finalInt(13, 0);

    for (int i = 0; i < 4; i++)
    {
        input(largeString1, largeString2);
        convert(largeString1, largeString2, largeInt1, largeInt2);
        add(largeInt1, largeInt2, finalInt);
        output(finalInt);
    }

    system("pause");

    return 0;
}
void input(string &largeString1, string &largeString2)
{
    cout << "Input:" << endl << endl;
    cin >> largeString1;
    cin >> largeString2;
}
void convert(string largeString1, string largeString2, vector<int> &largeInt1, vector<int> &largeInt2)
{
    int size1 = size(largeString1);
    int size2 = size(largeString2);
    for (int i = 0; i < 12; i++)
    {
        int dynamicsize1 = size1 - i;
        largeInt1[11 - i] = asciiToInt(largeString1[dynamicsize1 ]);
    }
    for (int j = 0; j < 12; j++)
    {
        int dynamicsize2 = size2 - j;
        largeInt2[11 - j] = asciiToInt(largeString2[dynamicsize2 ]);
    }
}
int asciiToInt(char ch)
{
    return (ch - '0');
}
void add(vector<int> largeInt1, vector<int> largeInt2, vector<int> &finalInt)
{
    for (int i = 0; i < 12; i++)
    {
        if (largeInt1[11 - i] + largeInt2[11 - i] >= 10)
        {
            finalInt[12 - i] = finalInt[12 - i] + largeInt1[11 - i] + largeInt2[11 - i] - 10;
            finalInt[12 - i - 1] = 1;
        }
        else
            finalInt[12 - i] = finalInt[12 - i] + largeInt1[11 - i] + largeInt2[11 - i];
    }
}
void output(const vector<int> finalInt)
{
    cout << endl << "Output:" << endl << endl << "The sum is: ";
    for (int i = 0; i < 13; i++)
    {
        cout << finalInt[i];
    }
    cout << endl << endl;
}

1 个答案:

答案 0 :(得分:1)

使用它来转换字符串:

void convert(string largeString1, string largeString2, vector<int> &largeInt1, vector<int> &largeInt2)
{
    int size1 = largeString1.size();
    int size2 = largeString2.size();
    for (int i = 0; i < size1; i++)
    {
        largeInt1[11 - i] = asciiToInt(largeString1[size1-1-i]);
    }
    for (int j = 0; j < size2; j++)
    {
        largeInt2[11 - j] = asciiToInt(largeString2[size2-1-j]);
    }
}