I had it down, then I thought I could condense the program using functions. Well, it's broken now and looking for some help. I got "An Access Violation (Segmentation Fault) raised in your program." I was like neato....but how.... I'm trying to add to integers with the array of 30 characters.

any direction would help.
Thanks....

#include <iostream>
#include <string>


using namespace std;

const int counter=31;

int value1(int[]);
int value2(int[]);
int sum(int[]);

int main()
{
    int i;
    int a[counter];
    int b[counter];
    int c[counter]; 
    
    value1(a); 
	value2(b); 
	sum(c); 
    
    system("pause");
    return 0;
}
int value1 (int a[]) 
    {
       int i;
       cout << "Enter a integer less than 30 ";
       cin >> a[i];
       i=0; i < counter-1; i++;
       return a[i];
       
}
int value2 (int b[]) 
    {
       int i;
       cout << "Enter a integer less than 25 ";
       cin >> b[i]; 
       i=0; i < counter-1; i++;
       return b[i];   
}

int sum (int c[])
{
    int i;
    int a[30],b[30];
	/*for(i=0; i<counter; i++)
		c[i] = a[i] + b[i];*/
		for(i = counter; i>=0;i--)


if ((b[i]+a[i]) > 9)
{
	 b[i-1] = b[i-1] + 1;
}

c[i] = (b[i]+a[i]) % 10;


	cout << "The sum of these two integers is: " << c[i];   
	return c[i];
}

Edited 6 Years Ago by sunrunner23: n/a

int value1 (int a[]) 
    {
       int i;
       cout << "Enter a integer less than 30 ";
       cin >> a[i];
       i=0; i < counter-1; i++;
       return a[i];
       
}

No clue what you are trying to do here. Line 6 doesn't make any sense to me. Is this a for loop? If so, it should be this:

for(i = 0; i < counter - 1; i++)
{
    // code to repeat
}

Anyway, you have a seg fault in line 54 for sure. counter is 31. In line 48, you define arrays a and b to have 30 elements, so valid indexes are 0 to 29. You can't access index 31, yet you try to in line 54.

That's your seg fault, but there are also logic problems beyond that. Line 54 references a[] and b[], which are declared on line 48 and are uninitialized, yet you are adding them and comparing them to 9. Using uninitialized variables in comparisons is asking for trouble. You need to get rid of the declaration on line 48 and instead pass a[] and b[] to the sum function.

This article has been dead for over six months. Start a new discussion instead.