Segmentation Fault

This is essentially all the code. I am fairly new and am stuck. ReadBig will read a number as a string, It then converts each element of the string to an integer and stores it in an integer array. Finally, it reverses the elements of the array so that the ones digit is in element zero, the tens digit is in element 1, the hundreds digit is in element 2, etc. I will not be using any numbers with more than 10 digits for early testing

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <string>
using namespace std;

// Do not change these function prototypes:
void readBig(int[]);
void printBig(int[]);
void addBig(int[], int[], int[]);

// This constant should be 100 when the program is finished.
const int MAX_DIGITS = 100;

//There should be no changes made to the main program when you turn it in.
int main()
     {
     // Declare the three numbers, the first, second and the sum:
     int number1[MAX_DIGITS]={}, number2[MAX_DIGITS]={}, sum[MAX_DIGITS]={};
     bool finished = false;     
     char response;
     while (! finished)
         {
         cout << "Please enter a number up to " << MAX_DIGITS << " digits: ";
         readBig(number1);
         cout << "Please enter a number up to "<<MAX_DIGITS<< " digits: ";
         readBig(number2);
         addBig(number1, number2, sum);
         printBig(number1);
         cout << "\n+\n";
         printBig(number2);
         cout << "\n=\n";
         printBig(sum);
         cout << "\n";
         cout << "test again?";
         cin>>response;
         cin.ignore(900,'\n');
         finished = toupper(response)!= 'Y';
         }
     return 0;
     }
//ReadBig will read a number as a string,
//It then converts each element of the string to an integer and stores it in an integer array.
//Finally, it reverses the elements of the array so that the ones digit is in element zero,
//the tens digit is in element 1, the hundreds digit is in element 2, etc.

//AddBig adds the corresponding digits of the first two arrays and stores the answer in the third.
//In a second loop, it performs the carry operation.

//PrintBig uses a while loop to skip leading zeros and then uses a for loop to print the number.
//FUNCTIONS GO BELOW
void readBig(int number[MAX_DIGITS])// This function below will read an input number as a string then input that
				   // into an array.
{
    string read="";
    cin>>read;
    //number[0] = read.length();
    
    for (int i=0; i < MAX_DIGITS && i < read.length(); i++){
        number[i] = int (read[i]-'0');
}
}
// This function below will display the number.
void printBig(int number[MAX_DIGITS])
{
	int digit=0;  //first val counter
	for (int i=MAX_DIGITS+1; i>=1; i--) {//i=highest val, while i isnt equal to 1, decrease by one til.
	if(number[i]==0 && digit>=1)cout <<""; // clears leading zeros and checks for placeholder zeros
	else  cout << number[i]; digit++;  // else print val and check for beginning of number.
}
}
void addBig(int number1[MAX_DIGITS], int number2[MAX_DIGITS], int sum[MAX_DIGITS])
//The code below sums the arrays.
{
	int c=0;
	for(int i=1;i<=MAX_DIGITS;i++){
	c=number1[i] + number2[i];
	sum[i]=c;
	if(c<number1[i] && c<number2[i]) sum[i+1]=number1[i] +number2[i]+1;
}
}
Last edited on
Line 28 is very confusing. I would try to explain what it is actually doing, but I'm getting lost, and an explanation would be very confusing. (Indexing the array by a value calculated from the value of the array at a different location...yuck!)

Make life simpler for yourself. Create more variables (memory is cheap) and do your algorithm is steps.

Step 1: calculate the value of the digit being considered.
Step 2: Calculate the index of where the digit goes.
Step 3: Put the value into the array at the proper index.

When you simplify your code you will more easily be able to determine where you messed up. Good luck trying to do that with line 28.
Idk what the issue is any help is good help.
thanks for your input.
afaik as simple as i can make line 28 is
i.e using 12345
int numLength=read.length; // will be 5 long
number[[numLength]-i+1] = read.at(i)-48;//reverses order of string for array
then i use
1
2
3
4
5
6
7
8
void printBig(int number[MAX_DIGITS])
{
	int digit=0;  //first val counter
	for (int i=number[0]; i>=1; i--) {//i=highest val, while i isnt equal to 1, decrease by one til.
	if(number[i]==0 && digit>=1)cout <<""; // clears leading zeros and checks for placeholder zeros
	else  cout << number[i]; digit++;  // else print val and check for beginning of number.
}
}


to print
Last edited on
1
2
3
  for (int i = 0; i < MAX_DIGITS && i < read.length(); i++) {
    number[i] = int(read[i] - '0');
  }

Then reverse it later.
Do not ever do i <= read.length(); if you are indexing that array in that loop - you will go out of bounds.
We count from 0, not 1, so the loop should start at 0.

Also initialize your array, otherwise it'll just be holding a bunch of crap values.
int number1[MAX_DIGITS]{0};

You might want to store how many digits the user entered so you don't have to print all 100 elements of that array.

And cout << number1; isn't how you should print an integer array. Use a loop.
thanks for the useful tips. when you say store, do you mean create an int var that will be
int value = read.length();
and that can be used to replace read.length in the second parameter for the for loop?
Last edited on
Any idea why i might be getting this in the console :

Please enter a number up to 100 digits: 1234
Please enter a number up to 100 digits: 1234
03-48432
+
2-48432
=
23-96864
test again?


Are those negative numbers??? On the bright side my carry function seems to work on the wierd numbers that are produced above and below the + sign.
Thanks again for replying. "Do not ever do i <= read.length(); if you are indexing that array in that loop - you will go out of bounds." I spent at least an hour playing with the code and that was apparently my main issue.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
int readBid(int number[MAX_DIGITS)
{
  string read;
  cin >> read;

  for (int i = 0; i < read.length() && i < MAX_DIGITS; i++)
  {
    number[i] = int(read[i] - '0');
  }

  return read.length();
}

Will read the string, place each digit into the array and return the length of the string entered by the user.
I don't know what code you're using to add the numbers, so, no, I have no idea why you'd be getting those results.
Topic archived. No new replies allowed.