Calculator program help please?
Oct 1, 2012 at 5:52pm UTC
Ok, so I am writing a calculator program for class, but I keep getting an error that says:
1 2
Calculator.cpp: In function 'int main()' :
Calculator.cpp:40: error: 'multiply' was not declared in this scope
I am confused... divide works, not multiply though. Some help please?
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
/* Luke Lyon
Assignment 5
Section 2
10-1-2012 */
#include<iostream>
#include<cmath>
using namespace std;
//---------- Function Prototypes -----------
void print_menu();
float get_value();
float divide(float ,float );
//-------------- Main -------------------
int main()
{
float operand1, operand2, answer;
int choice, valid_choice;
do {
print_menu();
cin >> choice;
valid_choice = 1; // assume choice is valid
switch (choice){
case 0:
break ;
case 1:
operand1 = get_value();
operand2 = get_value();
answer = operand1 + operand2;
break ;
case 2:
operand1 = get_value();
operand2 = get_value();
answer = operand1 - operand2;
break ;
case 3:
operand1 = get_value();
operand2 = get_value();
answer = multiply(operand1,operand2);
break ;
case 4:
operand1 = get_value();
operand2 = get_value();
answer = divide(operand1,operand2);
break ;
case 5:
operand1 = get_value();
operand2 = get_value();
answer = 3.14159 * (pow(operand1,2)) * operand2;
break ;
case 6:
operand1 = get_value();
answer = divide(4,3) * 3.14159 * (pow(operand1,3));
break ;
case 7:
operand1 = get_value();
answer = pow(operand1,3);
break ;
default :
valid_choice = 0;
cout << "Invalid Choice." << endl;
}
if (valid_choice){
cout << endl << "Answer = " << answer << endl;
}
}while (choice != 0);
return 0;
}
//-------------- Functions -------------------
float divide(float dividend, float divisor){
if (divisor == 0)
return 0; // avoids divide by zero errors
else
return (dividend/divisor);
}
float multiply(float multi, float multiplier){
return (multi*multiplier);
}
float get_value(){
float temp_value;
cout << "Enter a value: " ;
cin >> temp_value;
return temp_value;
}
void print_menu(){
cout << endl;
cout << "Add (1)" << endl;
cout << "Subtract (2)" << endl;
cout << "Multiply (3)" << endl;
cout << "Divide (4)" << endl;
cout << "Volume of a Right Cylinder (5)" << endl;
cout << "Volume of a Sphere (6)" << endl;
cout << "Volume of a Cube (7)" << endl;
cout << "Exit (0)" << endl;
cout << "Enter you choice: " ;
}
Oct 1, 2012 at 5:55pm UTC
You did neither declare the function prototype of multiply at the function prototype section nor did you provide the implementation of multiply before the calling function, so multiply is unknown when the compiler parses the main function
Oct 1, 2012 at 5:57pm UTC
You're missing the prototype for it.
multiply has to be declared before main calls it. This means either moving the function so it's above main, or making a prototype for it.
Oct 1, 2012 at 6:11pm UTC
Oh crap, thanks guys, haha. My bad
Topic archived. No new replies allowed.