I am just doing it as a hobby (in my spare time).
Please would you have a look at this code for me when i try to compile it i keep getting the message "[Linker error]undefined reference to 'mult (int,int),[Linked error]undefined reference to 'divi (int,int)' etc...."
Code:#include <iostream> #include <cstdio> #include <cstdlib> #include <string> using namespace std; int selection; int mult (int a,int b); int divi (int c,int d); int add (int e,int f); int subt (int g,int h); int main() { cout<<"1. Multiplication"; cout<<"2. Division"; cout<<"3. Addition"; cout<<"4. Subtraction"; cout<<"5. Exit"; cin>> selection; cin.ignore(); switch (selection) { case 1: int mult (int a,int b); int a; int b; { cout<<"Please input two numbers to be multiplied: "; cin>> a >> b; cin.ignore(); cout<<"The product of your two numbers is "<< mult (a,b) <<".\n"; cin.get(); } int mult (int a,int b); { return a*b; } break; case 2: int divi (int c,int d); int c; int d; { cout<<"Please input two numbers to be divided: "; cin>> c >> d; cin.ignore(); cout<<"The product of your two numbers is "<< divi (c,d) <<".\n"; cin.get(); } int divi (int c,int d); { return c/d; } break; case 3: int add (int e,int f); int e; int f; { cout<<"Please input two numbers to be added: "; cin>> e >> f; cin.ignore(); cout<<"The product of your two numbers is "<< add (e,f) <<".\n"; cin.get(); } int add (int e,int f); { return e+f; } break; case 4: int subt (int g,int h); int g; int h; { cout<<"Please input two numbers to be subtracted: "; cin>> g >> h; cin.ignore(); cout<<"The product of your two numbers is "<< subt (g,h) <<".\n"; cin.get(); } int subt (int g,int h); { return g-h; } break; case 5: return 0; } }
Code:#include <iostream> using namespace std; int mult(int a, int b); int main(){ int selection; int num1; int num2; //prompt user for selection //read user input //prompt user for numbers //read user input switch(selection){ case 0: cout << "The answer is: " << mult(num1,num2) << "\n"; break; } return 0; } int mult(int a, int b){ return (a*b); }
I recommend you declare just 2 ints, a and b, and do so just after the opening braces in main(). Then in each case option obtain user input into a and b and pass a and b to the desired funtion. Function calls are made by typing the name of the function and the parameters to be sent, such as add(a, b); or subt(a, b). A function call should not have any type information in it like the declaration and first line of the definition need.
You're only born perfect.
I tried it with just the two intergers before and i just kept getting an error whenever o tried to compile the source code.
Here's your latest post of code with a few comments outlining
what you did wrong:
Now here is your corrected code - this compiles now:Code:#include <iostream> //Dont need these 3 because you dont use any functions from them #include <cstdio> #include <cstdlib> #include <string> using namespace std; int selection; //You should declare this inside main int mult (int a,int b); int divi (int c,int d); int add (int e,int f); int subt (int g,int h); int main() { cout<<"1. Multiplication"; //These are probably printing all on the same line - use cout << endl cout<<"2. Division"; //to move to next line cout<<"3. Addition"; cout<<"4. Subtraction"; cout<<"5. Exit"; cin>> selection; cin.ignore(); switch (selection) { //Watch your braces - its a style issue - not critical case 1: int mult (int a,int b); //Here is your problem - the stuff that int a; //the function is supposed to do int b; //shouldn't be here - it should { //be defined outside main cout<<"Please input two numbers to be multiplied: "; cin>> a >> b; cin.ignore(); cout<<"The product of your two numbers is "<< mult (a,b) <<".\n"; cin.get(); } int mult (int a,int b); { return a*b; } break; case 2: int divi (int c,int d); //As above int c; int d; { cout<<"Please input two numbers to be divided: "; cin>> c >> d; cin.ignore(); cout<<"The product of your two numbers is "<< divi (c,d) <<".\n"; cin.get(); } int divi (int c,int d); { return c/d; } break; case 3: int add (int e,int f); //As above int e; int f; { cout<<"Please input two numbers to be added: "; cin>> e >> f; cin.ignore(); cout<<"The product of your two numbers is "<< add (e,f) <<".\n"; cin.get(); } int add (int e,int f); { return e+f; } break; case 4: int subt (int g,int h); //Once again, as above int g; int h; { cout<<"Please input two numbers to be subtracted: "; cin>> g >> h; cin.ignore(); cout<<"The product of your two numbers is "<< subt (g,h) <<".\n"; cin.get(); } int subt (int g,int h); { return g-h; } break; case 5: return 0; } }
Hope that this clarifies it for you, sorry about the long postCode:#include <iostream> using namespace std; int mult (int a,int b); int divi (int a,int b); int add (int a,int b); int subt (int a,int b); int main() { int selection; int a; //You won't need different variables for each function int b; //These to are used for all operations cout<<"1. Multiplication" << endl; cout<<"2. Division" << endl; cout<<"3. Addition" << endl; cout<<"4. Subtraction" << endl; cout<<"5. Exit" << endl; cin>> selection; cin.ignore(); switch (selection) { case 1: cout<<"Please input two numbers to be multiplied: "; cin>> a >> b; cin.ignore(); cout<<"The product of your two numbers is "<< mult (a,b) <<".\n"; cin.get(); break; case 2: cout<<"Please input two numbers to be divided: "; cin>> a >> b; cin.ignore(); cout<<"The product of your two numbers is "<< divi (a,b) <<".\n"; cin.get(); break; case 3: cout<<"Please input two numbers to be added: "; cin>> a >> b; cin.ignore(); cout<<"The product of your two numbers is "<< add (a,b) <<".\n"; cin.get(); break; case 4: cout<<"Please input two numbers to be subtracted: "; cin>> a >> b; cin.ignore(); cout<<"The product of your two numbers is "<< subt (a,b) <<".\n"; cin.get(); break; case 5: return 0; } } //now define what the functions do int mult (int a,int b) { return a*b; } int divi (int a,int b) { return a/b; } int add (int a,int b) { return a+b; } int subt (int a,int b) { return a-b; }
No No's:
fflush (stdin); gets (); void main ();
Goodies:
Example of fgets (); The FAQ, C/C++ Reference
My Gear:
OS - Windows XP
IDE - MS Visual C++ 2008 Express Edition
ASCII stupid question, get a stupid ANSI
Thanks sooooo much! That prog. has caused me nothing but problems but thanks to you the things that i did wrong have been clarified thanx to you!
Is there any way i can loop it so that it goes back to the menu after each calculation?
Yes, very easily you'll be glad to know. the most convenient
way is a while loop - as shown below. It is very important
to initialise the variable "selection" in circumstances such as these
because as you may or may not know, variables are not
automatically set to zero when you declare them, they have an
undefined value, which is usually the value last stored in the
particular piece of memory - which if that happened to be the
same as the value that the whle loop checks for, then your
program would just exit immediately.
Now i'll briefly explain how this loop works - the while keyword
is a lot like the if keyword - you enter a logical expression into
the parentheses, and if the expression evaluates to true, the
program runs the code inside the braces. if it is false, it skips that
block of code. the condition is checked at the start of each loop,
so that the loop does not exit the moment that the expression
becomes false.
in the code below, the while loop ckecks to see whether the
value stored in selection is 5. if it is not 5, the code inside
the loop is run - allowing the user to make a selection. if they
choose 5, then the switch statement jumps to the label "case 5:"
but the oply thing contained there is a break statement which
exits the switch statement. then the loop itself comes to an
end (there is no code between the end of the switch statement
and the end of the looping block), so the program performs
the while loop check. selection is still 5, so the expression
evaluates to false (it is true for any value except 5, remember?)
and the program skips the code block for the loop. the next line
of code encountered is return 0, so the program exits.
Code:int main() { int selection = 0; //Important to initialise this int a; //for the loop to work properly int b; while (selection != 5) { cout<<"1. Multiplication" << endl; cout<<"2. Division" << endl; cout<<"3. Addition" << endl; cout<<"4. Subtraction" << endl; cout<<"5. Exit" << endl; cin>> selection; cin.ignore(); switch (selection) { . . . . case 5: break; } } return 0; }
No No's:
fflush (stdin); gets (); void main ();
Goodies:
Example of fgets (); The FAQ, C/C++ Reference
My Gear:
OS - Windows XP
IDE - MS Visual C++ 2008 Express Edition
ASCII stupid question, get a stupid ANSI
Thanx for all of your help my project is now completely done!
By the way how do i clear the screen of any previous text?