Problems with a menu

This is a discussion on Problems with a menu within the C++ Programming forums, part of the General Programming Boards category; I am just doing it as a hobby (in my spare time)....

  1. #16
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    I am just doing it as a hobby (in my spare time).

  2. #17
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    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;
    }
    }

  3. #18
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    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);
    
    }

  4. #19
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

  5. #20
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    I tried it with just the two intergers before and i just kept getting an error whenever o tried to compile the source code.

  6. #21
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Here's your latest post of code with a few comments outlining
    what you did wrong:

    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;
    }
    }
    Now here is your corrected code - this compiles now:

    Code:
    #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;
    }
    Hope that this clarifies it for you, sorry about the long post
    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

  7. #22
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    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!

  8. #23
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    Is there any way i can loop it so that it goes back to the menu after each calculation?

  9. #24
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    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

  10. #25
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    Thanx for all of your help my project is now completely done!

  11. #26
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    By the way how do i clear the screen of any previous text?

  12. #27
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738

  13. #28
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    Thanx all sorted.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ncurses menu question
    By mak2k in forum C Programming
    Replies: 2
    Last Post: 11-07-2006, 02:04 PM
  2. Creating a menu that reads input via an array?
    By Nalif in forum C Programming
    Replies: 6
    Last Post: 09-29-2006, 09:21 PM
  3. menu - options Problems
    By wakish in forum C Programming
    Replies: 3
    Last Post: 07-05-2006, 03:23 AM
  4. Problems with my menu
    By Olidivera in forum Windows Programming
    Replies: 2
    Last Post: 07-07-2005, 12:44 PM
  5. Menu stuff
    By Shadow in forum C Programming
    Replies: 10
    Last Post: 04-28-2002, 09:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21