问题描述:

I'm trying to write a program that will allow two numbers of up to 100 digits be added together and C++. I've gone through the code a bunch of times, but the compiler is telling me that there is a problem initializing the sum array in line 61. Any ideas as to why this is or how to fix it?

#include <iostream>

#include <string>

#include <cstdlib>

using namespace std;

void add_nums(string number1, string number2)

{

int min = (number1.length() < number2.length() ?

number1.length():number2.length());

int max = (number1.length() < number2.length() ?

number2.length():number1.length());

int *n1 = new int[max];

int *n2 = new int[max];

for (unsigned int i=0; i < number1.length(); i++)

{

n1[i] = number1.at(number1.length() - 1 -i) - 48;

}

cout << number1 << endl;

for (unsigned int i=0; i < number2.length(); i++)

{

n2[i] = number2.at(number2.length()-1 -i) - 48;

}

cout << number2 << endl;

int carry = 0;

int sum[] = new int[max];

int k=0;

for (k = 0; k < max; k++)

{

sum[k] = (n1[k] + n2[k] + carry) % 10;

if ( (n1[k] + n2[k] + carry) >= 10)

carry = 1;

else carry = 0;

}

sum[max] = carry;

for (int j= max; j >= 0; j--)

{

cout << sum[j] << endl;

}

}

int main()

{

string number1;

string number2;

cout << "Enter a number and press return (100 digits maximum): ";

cin >> number1;

cout << "Enter a number and press return (100 digits maximum): ";

cin >> number2;

add_nums(number1, number2);

return 0;

}

网友答案:

As Mike already mentioned your problem is in this line:

int sum[] = new int[max];

The way you are coding it, is the Java style.

In C(++) it's done a bit different: For the compiler there is no way to know the size of the array $sum$ you want to create, since max is calculated at runtime and depends on the input. This means you have two different possibilities to overcome this problem:

First, but ugly method: Define a constant maximum size, create the array with this size and later use just the size you need:

const int MAX_SIZE = 101;
int sum[MAX_SIZE];

Second, and most time the better method: Do it, as you did some lines before and use a pointer:

int* sum = new int[max]

This dynamically allocates memory of the size you need on the HEAP.

Attenion! You used new[] in your code, but never used delete[]. This means you allocate some memory, but never free is, once you do not need it any longer. In your small program this might not take effect, but you should always keep care to avoid it. If you can use C++11 it might be the best solution to use "unique pointers":

unique_ptr<int[]> n1(new int[max]);

These pointers delete themselves if the last reference is destroyed.

网友答案:

I got this error on compiling this program

error: array initializer must be an initializer list

You can't declare like this

int sum[] = new int[max];

You need this line

int sum[max]

or

int *sum = new int[max]

And don't forget delete objects created by new

网友答案:

int sum[]=new int[max] is not a C++ style of coding. Instead use it as

int* sum = new int[max]

Since you have the value of max available, you can also declare it statically using

int sum[max]

If you are using the former method, be sure that you free the pointer at some point of time, to avoid memory leaks.

网友答案:
int sum[] = new int[max];

is not a good syntax. This should be

int *sum = new int[max];

or, better:

std::array<int, max> sum;

Prefer STL than C-style arrays.

Edit


This is not correct:

sum[max] = carry;

it should be:

sum[max - 1] = carry;

Because index begins at 0 in C++.

相关阅读:
Top