String Subsript Error

I have been looking over my code for half an hour and yet still cant seem to find the "string subscript error." I'm mostly concentrating on my "row variable," but now I believe that's not the right variable to look for. Still not sure, but members, can you help me find the error please. This program is basically reading four digits using a string and then adding them togather. Moreover, I'm then storing the sum of each index of the digit.

for example, 4673+4672. First I look at the laxt index of this string which is 3 and 2. add them which is 5. store the last number which is four in my result array. The rest its just reapeating over.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 void addnumber_formula(string one,string two, int result[],int resultsize)
{
	//Declare sum to store the sum of indexes
	int sum=0;
	int lastnum=0;
	int carry=0;
	int row=0;
	int counter;

	//find the length of each string.
	int length1, length2;
	int max;
	length1=one.length();
	length2=two.length();
	//Determine which one of these is a max

	if (length1>length2)
		max=length1;
	else
		max=length2;

	//make a for loop to get each letter of a string
	for (counter=max;counter>0;counter--)
	{
		sum=(one[counter]-'0')+(two[counter]-'0')+carry;
		
		//Find the last number of sum
		lastnum=sum%10;

		//find first number of sum
		carry=sum/10;

		//store last number in the result
		result[row]=lastnum;
		row++;
		//if max is equal to one then make sure to enter carry in the array
			if (counter==1)
			{
				
				result[row]=carry;
			}
			
	}


}


here I smy main function
1
2
3
4
5
6
7
8
9
	//Cal he function to get the four digit number
	string one,two;
	display(one, two);
	int results[4];

	

	//Call addnumberformula
	addnumber_formula(one, two, results,4);
Last edited on
Any members willing to help out please
Where is the error occurring? If it is during runtime, have you tried using a debugger to find out?
1
2
3
4
5
6
7
8
9
	if (length1>length2)
		max=length1;
	else
		max=length2;

	for (counter=max;counter>0;counter--)
	{
		sum=(one[counter]-'0')+(two[counter]-'0')+carry;
                // ...  


The first time through this for loop, the subscript for both one and two will be invalid. The fact that length1 and length2 may be of different length suggests that even on subsequent iterations of the for loop, counter will be out of range for the string who's length wasn't max.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
#include <algorithm>
#include <deque>
using namespace std ;

deque<int> add_numbers( string one, string two )
{
    static const std::string digits = "0123456789" ;
    std::size_t sz = std::max( one.size(), two.size() ) ;
    if( sz == 0 || // empty strings
        one.find_first_not_of(digits) != std::string::npos || // invalid characters
        two.find_first_not_of(digits) != std::string::npos ) return {0} ;

    // make the strings of the same size by adding zeroes at the front
    while( one.size() < sz ) one = '0' + one ;
    while( two.size() < sz ) two = '0' + two ;

    deque<int> result ; // the logic is much easier to follow
                         // with a deque and push_front

    int carry = 0 ;
    for( int i = sz-1 ; i >=0 ; --i )
    {
        // for this to work, both strings must be of the same size
        int sum = one[i] - '0' + two[i] - '0' + carry ;

        // push_front because we are comuting the less significant digit first
        result.push_front( sum % 10 ) ;

        carry = sum / 10 ;
    }

    if(carry) result.push_front(carry) ; // residual carry, if any

    return result ;
}

int main()
{
    for( int d : add_numbers( "987", "654321" ) ) std::cout << d ;
    std::cout << ' ' << 987+654321 << '\n' ;

    for( int d : add_numbers( "987654", "654321" ) ) std::cout << d ;
    std::cout << ' ' << 987654+654321 << '\n' ;
}

http://coliru.stacked-crooked.com/a/60e563bf277bc630

Once you have understood the simple logic, you can add the complicated stuff - c-style array instead of standard sequence container, c-style string instead of standard string etc.
Boras, thank you for the code you provided. I will definately look at it once I'm up to that level. I haven't learned about string that much yet though. Thank y though!!

Cire, I understand what your saying. The error does occur T runtime. I'm using my sample number 4305,4795. Both are same sizes, so how does that cause an issue
Topic archived. No new replies allowed.