当前位置: 动力学知识库 > 问答 > 编程问答 >

pointers - Segmentation Fault while simulating top-down addition in C++

问题描述:

I'm trying to write a program in C++ that takes in two strings containing numbers and adds them together to return a "sum" (which is also a string).

I've written comparable programs in Java and Python, so I decided that while learning C++, I might as well write something comparable. I don't know why I'm receiving the errors, and I'm not receiving any errors while compiling with Visual Studio or g++.

Below is the reference to the function in question in the main program.

Number base(NULL);

Number r = base.addNums("1", "1");

cout << r.toString() << endl;

I have ensured that I have a constructor as follows for the reference to the addNums function:

Number(void){}

I wrote some comments to try and explain my thought process while writing the header file. The method in question is as follows:

Number addNums(string in1, string in2){

// Calling number 1: X, and number 2: Y

const char* x;

const char* y;

x = in1.c_str();

y = in2.c_str();

// Flag for one number having more digits

bool flag = false;

// Flag for X having more digits

bool xIsBigger = false;

// For storing the sum later

string summ = "";

// Check and see if the flags are needed

if (!(strlen(x) == strlen(y))){

flag = true;

if (strlen(x) > strlen(y)){

xIsBigger = true;

}

}

// Prepend the zeroes to the necessary variable

// to make it work as written addition does

if (flag){

if (xIsBigger){

string zeroes;

for (unsigned int i = 0; i < (strlen(x) - strlen(y)); ++i){

zeroes += "0";

}

string newYStr = zeroes + in2;

const char* newY = newYStr.c_str();

// Add zeroes to Y variable

y = newY;

} else{

string zeroes;

for (unsigned int i = 0; i < (strlen(y) - strlen(x)); ++i){

zeroes += "0";

}

string newXStr = zeroes + in1;

const char* newX = newXStr.c_str();

// Add variables to X value

x = newX;

}

}

// If we encounter x + y > 9, we need this

int carry = 0;

// Current digit being processed

char digitX, digitY;

// Digit to be carried

char toCarry;

// Iterate through the number right to left

// to simulate top-down addition

for (int i = strlen(x) - 1; i >= 0; --i){

digitX = x[i];

digitY = y[i];

// If we're carrying a 1, add it to the top number

if (carry > 0){

digitX += 1;

carry = 0;

}

// Add together the two numbers stored in characters

int currentSum = atoi(&digitX) + atoi(&digitY);

// If x + y > 9, we need to carry

if (currentSum > 9){

string sumString = "" + currentSum;

// Max possible is 9 + 9, so we only have to carry 1

carry = 1;

// Add the second digit in the number to the position in the sum

summ = sumString.at(1) + summ;

}

// Didn't need a carry

else{

string sumString = "" + currentSum;

summ = sumString + summ;

}

}

// Return the object containing the sum

return Number(summ);

}

I'm relatively new to the use of pointers, but in the process of learning some more of the language and writing this program, and through extensive googling of syntax and language-specifics, I've been forced into what I feel is the need to use them.

I'm very sorry I cannot provide more information and I appreciate any help or critique that can help me.

Thanks in advance!

网友答案:

'string newYStr' may be going out of scope? Try declaring it in the top and then check.

分享给朋友:
您可能感兴趣的文章:
随机阅读: