Making a simple calculator

Oct 27, 2013 at 5:10pm
I'm making a simple calculator using only +,-,*,/, and % as the operations. I'm having trouble as to why my code will not run. When i compile the code, it gives me "[Linker error] undefined reference to `displayResult(int, char, int)'" and "ld returned 1 exit status," as errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <cstdlib>

using namespace std;

void displayResult(int num1, char oper, int num2);

int main ()
{
    int num1, num2;
    char oper;
    
    cout << "Input an operation:";
    cin >> num1 >> oper >> num2;
    
    if(cin.good())
    {
                  displayResult(num1, oper, num2);
    }
    else
    cout << "you must use the proper format to enter the desired calculation!"
         << "\n\tProgram Terminated!" << endl;
         
         system("PAUSE");
         return 0;

void displayResult(int num1, char oper, int num2);
{
     switch (oper)
          {  
                  case '+': cout << num1 << "+" << num2 << "=" << num1 + num2 << endl;
                  break;
                  
                  case '-': cout << num1 << "-" << num2 << "=" << num1 - num2 << endl;
                  break;
                  
                  case '*': cout << num1 << "*" << num2 << "=" << num1 * num2 << endl;
                  break;
                  
                  case '/': if (num2 == 0)
                            cout << "Cannot divide by zero." << endl;
                            
                            else
                            cout << num1 << "/" << num2 << "=" << num1 / num2 << endl;
                  break;
                  
                  case '%': if (num2 == 0)
                            cout << "Modulus cannot be zero." << endl;
                            
                            else
                            cout << num1 << "%" << num2 << "=" << num1 % num2 << endl;
                  break;
                  
                  default: cout << "Invalid operation." << endl;
                  }}}

            
Oct 27, 2013 at 5:16pm
hi leejacob77,

you can do one of 2 things.

1. you can put the actual function of displayResult above the main function and remove the declaration of it.

2. you will need to make a .h file where you put the deceleration.

ad far as i know c++ does not like it the way you have right now. if i'm not mistaken though c would have no problem with it
. it seems it works in visual studio, but i have had problems with different compilers on the subject

on second examination: it seems that the displayResult function is nested int the main function and it has a semicolen after the displayResult function, it wont work like this

so it should look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <cstdlib>

using namespace std;

void displayResult(int num1, char oper, int num2)
{
	switch (oper)
	{
	case '+': 
		cout << num1 << "+" << num2 << "=" << num1 + num2 << endl;
		break;
	case '-': 
		cout << num1 << "-" << num2 << "=" << num1 - num2 << endl;
		break;
	case '*': 
		cout << num1 << "*" << num2 << "=" << num1 * num2 << endl;
		break;
	case '/': if 
		(num2 == 0)
			cout << "Cannot divide by zero." << endl;
		else
			cout << num1 << "/" << num2 << "=" << num1 / num2 << endl;
		break;
	case '%': 
		if (num2 == 0)
			cout << "Modulus cannot be zero." << endl;
		else
			cout << num1 << "%" << num2 << "=" << num1 % num2 << endl;
		break;
	default: 
		cout << "Invalid operation." << endl;
	}
}

int main()
{
	int num1, num2;
	char oper;

	cout << "Input an operation:";
	cin >> num1 >> oper >> num2;

	if (cin.good())
	{
		displayResult(num1, oper, num2);
	}
	else
		cout << "you must use the proper format to enter the desired calculation!"
		<< "\n\tProgram Terminated!" << endl;

	system("PAUSE");
	return 0;
}
Last edited on Oct 27, 2013 at 5:32pm
Oct 27, 2013 at 5:17pm
Get rid of the semicolon on line 27. You also need to put a closing brace on line 26 (for main) and remove an extraneous closing brace from line 55.
Oct 27, 2013 at 5:22pm
Thank you so much Danny Toledo! It works! This may sound like dumb questions but, why did i need to remove the semicolon? and why did i need a closing brace for main?
Last edited on Oct 27, 2013 at 5:25pm
Topic archived. No new replies allowed.