0

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;
}
simpl360
  • 34
  • 2
  • 3
    This is not valid code: `-1 < i < loop_amount` Maybe you meant `for (int i = loop_amount; -1 < i; i--) {` When I change it to that it works for me. What `-1 < i < loop_amount` does is `(-1 < i) < loop_amount` which is `true < loop_amount` but the numeric value of `true` in C++ is `1` so that's the same as `1 < loop_amount` which is an infinite loop because loop_amount will always be bigger than `1`. – Jerry Jeremiah Sep 27 '21 at 00:04
  • @JerryJeremiah You're right. It did end up working! Thank you. – simpl360 Sep 27 '21 at 00:09
  • 1
    It's legal C++, but it won't work as intended here. https://godbolt.org/z/ajxfjKhsx I suppose I might be using a broader definition of 'valid.' – sweenish Sep 27 '21 at 00:09
  • @sweenish That's why I wasn't getting any errors in the situation. That's kind of interesting. – simpl360 Sep 27 '21 at 00:16
  • 2
    If you go to my link, you'll see that I enabled some warnings with `-Wall`. That was the minimum needed to get a warning, but you should also use `-Wextra` as well. As your code gets more complex, you'll enable more warnings. Always enable warnings to catch this stuff. – sweenish Sep 27 '21 at 00:18
  • [Is (4 > y > 1) a valid statement in C++? How do you evaluate it if so?](https://stackoverflow.com/q/8889522/995714), [Language support for chained comparison operators (x < y < z)](https://stackoverflow.com/q/4090845/995714), [Why are interval comparisons (e.g: x < variable < y) not supported in most "mainstream" languages?](https://stackoverflow.com/q/11468317/995714), [Languages with Chained Comparison Operators](https://stackoverflow.com/q/56586310/995714) – phuclv Sep 27 '21 at 00:29
  • 1
    The fact that it compiles just means that the syntax is correct. It has nothing to do with whether the code is functional. You can easily write code that does not run or runs totally incorrectly or loops infinitely that will compile because the syntax is correct. – Ken White Sep 27 '21 at 00:32

0 Answers0