I am writing a small program to simulate simple arithmetic. My issue is that my program will compile (on a Linux machine) and start running, but no output is generated nor any completion.
I have tried to change the stoi function multiple times (which used to emit errors in the for loop inside the Arithmetic_ADD function), and I have attempted to debug some spots in the program.
#include <string>
#include <iostream>
#include <math.h>
using namespace std;
string Add_Padding(string val, int padding_amount) {
string result = "";
for (int i = 0; i < padding_amount; i++) {
result += "0";
}
return result.append(val);
}
string *Auto_Padding(string val1, string val2) {
int val1L = val1.length();
int val2L = val2.length();
if (val1L > val2L) {
val2 = Add_Padding(val2, val1L - val2L);
val1.insert(0, "0");
val2.insert(0, "0");
static string to_return[3] = {val1, val2, to_string(val1L)};
return to_return;
}
else if (val1L < val2L) {
val1 = Add_Padding(val1, val2L - val1L);
val1.insert(0, "0");
val2.insert(0, "0");
static string to_return[3] = {val1, val2, to_string(val2L)};
return to_return;
}
else {
val1.insert(0, "0");
val2.insert(0, "0");
static string to_return[3] = {val1, val2, to_string(val1L)};
return to_return;
}
}
string Arithmetic_Add(string tempVal1, string tempVal2) {
string *values = Auto_Padding(tempVal1, tempVal2);
string val1(values[0]);
string val2(values[1]);
int loop_amount = stoi(values[2]) + 1;
int carry = 0;
int currentResult;
int tempVar;
string result = "";
for (int i = loop_amount; -1 < i < loop_amount; i--) {
currentResult = carry + stoi(val1) + stoi(val2);
if (currentResult > 9) {
carry = currentResult % 10;
tempVar = floor(currentResult / 10);
result.insert(0, to_string(tempVar));
}
else {
carry = 0;
result.insert(0, to_string(currentResult));
}
}
return result;
}
int main() {
cout << Arithmetic_Add("21000", "1000");
cout << endl;
cout << flush;
return 0;
}