### Making a simple calculator

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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````#include #include 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; }}} ``````
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:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354`` ``````#include #include 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
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.
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
Topic archived. No new replies allowed.