这里有一个错误:"string subscript is out of range"

There is an Error here : "string subscript is out of range"

本文关键字:is out of range subscript string 有一个 错误 这里      更新时间:2023-10-16

此代码正在添加两个整数,其中包括附加过程,包括随身携带过程,我在这里有一个错误,但我不知道这里是什么!该程序的目的是使C 要做一个"大于INT大的大小",并进行加法过程。.我真的需要帮助!谢谢 !

#include "stdafx.h"
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
class BigDecimalInt
{
    protected:
    int num1;
    public:
    string s1;
    string getstring()
    {
        return s1;
    }
    void setstring(string s2)
    {
        s1=s2;
    }
    BigDecimalInt (string decStr){s1=decStr;}
    BigDecimalInt (int decInt); // Initialize from integer
    BigDecimalInt operator+ ();
    BigDecimalInt operator- (BigDecimalInt anotherDec);
};
BigDecimalInt operator+ (BigDecimalInt firstDec,BigDecimalInt secDec)
{
    int size,diff,carry=0,x,y,z,counter=0;
    BigDecimalInt temp("");
    string c1,c2,result,smaller,bigger;
    c1=firstDec.getstring();
    c2=secDec.getstring();
    if(c1.length()<c2.length())
        {
            smaller = c1;
            bigger = c2;
        }
    else
       {
           smaller = c2;
           bigger = c1;
       }
    diff = bigger.length()-smaller.length();
    size = smaller.length();
    for(int i=size-1;i>=0;i--)
        {
            y = int(bigger[i+diff]);
            y-=48;
            x = int(smaller[i]);
                x-=48;
            z = x+y+carry;
            if(z<=9)
            {
                result+=char(z+48);
                carry = 0;
            }
            else
            {
                result+=char(z+38);
                carry = 1;
            }
        }
    while((carry==1)&&(counter<bigger.length()))
    {
        counter = size;
        x = int(bigger[counter])-48+carry;
        if(x<=9)
            {
                result+=char(x+48);
                carry = 0;
            }
        else
        {
            result+=char(x+38);
            carry = 1;
        }
        counter++;
    }
    if(counter==bigger.length())
        {
            if(carry==1)
            result+=char(49);
        }
    else
        for(int o=counter;o>=0;o++)
            result+=bigger[o];
    temp.s1=result;
    return temp;
}
int _tmain(int argc, _TCHAR* argv[])
{
     string k1,k2;
    cout<<"Enter first num : n";
    cin>>k1;
    BigDecimalInt c1(k1);
    cout<<"Enter second num : n";
    cin>>k2;
    BigDecimalInt c2(k2);
    BigDecimalInt c3("");
    c3=c1+c2;
    cout<<c3.s1;
    system("pause");
    return 0;
}

循环以不同的

脱颖而出
for(int o=counter;o>=0;o++)

所有其他人都在计算 down 零,但这似乎正在计算。

如果您使用bigger.length()counter == bigger.length() bigger,而最后一个有效索引是bigger.length() - 1