C++ code help. (I'm a beginner...)


#1


In c++, I'm Trying to make a very simple calculator with the "if" function, but I'm new to coding if you couldn't tell.


"Thread 1: EXC_ARITHMETIC (code=EXC_I386_DIV, subcode=0x0)" is the error code that I'm getting on the line with int sum4 = value1 / value2;

Any help from someone would be dope. Thanks homies.


#include <iostream>
using namespace std;

int value1, value2;
int sum1 = value1 + value2;
int sum2 = value1 - value2;
int sum3 = value1 * value2;
int sum4 = value1 / value2;
int sign;



int main ()
{
    cout << "Calculator" << endl;
    cout <<  "Please enter your first number: " << endl;
    cin >> value1;
    cout << "Enter '1' (Addition) '2' (Subtraction) '3' (Multiplication) '4' (Division)" << endl;
    cin >> sign;
    if (sign > 4) cout << "Bruh. Try Again. " << endl;
    else cout << "Please enter your second number" << endl;
    cin >> value2;
    if (sign == 1) cout << value1 << " + " << value2 << " = " << sum1 << endl;
    if (sign == 2) cout << value1 << " - " << value2 << " = " << sum2 << endl;
    if (sign == 3) cout << value1 << " * " << value2 << " = " << sum3 << endl;
    if (sign == 4) cout << value1 << " / " << value2 << " = " << sum4 << endl;
    return 0;
}


#2

Lol... Never mind. I just figured it out. I had the the int sum1 equaled to value1 before value1 had even been defined. I solved it by moving the "sum" ints down by "if" functions that I had set up. Thanks if you were about to help tho. :slight_smile:


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.