Switches and functions

This is a discussion on Switches and functions within the C++ Programming forums, part of the General Programming Boards category; Can anyone tell me what I'm doing wrong in this program? I think it has to do with trying to ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    75

    Question Switches and functions

    Can anyone tell me what I'm doing wrong in this program? I think it has to do with trying to call a function in a switch... but I don't know how else to put it. Help? (I'm just putting in up to the end of the function main cuz the other void functions are pretty long.

    Code:
    #include <iostream>
    
    void menu(char&, int&, int&, int&, int&);
    void addFractions(int, int, int, int, int, int);
    void subtractFractions(int, int, int, int, int, int);
    void multiplyFractions(int, int, int, int, int, int);
    void divideFractions(int, int, int, int, int, int);
    
    using namespace std;
    
    int main()
    {
    	char a, s, m, d, x, A, S, M, D, X;
    	char operation;
    	int numeratorOne, denominatorOne, numeratorTwo, denominatorTwo;
    	int numeratorResult, denominatorResult;
    
    	menu(operation, numeratorOne, denominatorOne, numeratorTwo, denominatorTwo);
    	
    	switch(operation)
    	{
    	case A: case a: addFractions(numeratorOne, denominatorOne, numeratorTwo, denominatorTwo,
    								numeratorResult, denominatorResult);
    	case S: case s: subtractFractions(numeratorOne, denominatorOne, numeratorTwo, denominatorTwo,
    								numeratorResult, denominatorResult);
    	case M: case m: multiplyFractions(numeratorOne, denominatorOne, numeratorTwo, denominatorTwo,
    								numeratorResult, denominatorResult);
    	case D: case d: divideFractions(numeratorOne, denominatorOne, numeratorTwo, denominatorTwo,
    								numeratorResult, denominatorResult);
    	case X: case x: break;
    	case default: cout << "Invalid selection";
    	}
    
    	return 0;
    }

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Code:
    //Switch
    
    switch (intVariable)
    {
       Case 1:
                     //....
       break;
       Case 2:
                    //....
       break;
       default:
                   //....
       break;
    };
    Last edited by mrafcho001; 07-16-2005 at 05:45 PM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Nope... that's not the problem. I'm still getting the same errors.

  4. #4
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Quote Originally Posted by MyntiFresh
    Nope... that's not the problem. I'm still getting the same errors.
    Post the errors..

  5. #5
    *this
    Join Date
    Mar 2005
    Posts
    498
    It's not
    Code:
    Case 1:
    It's ...
    Code:
    case 1:
    Remember c++ is case sensitive.

  6. #6
    *this
    Join Date
    Mar 2005
    Posts
    498
    If you don't put a break statement after each case, it leaks into the next case and perfoms the operations for each case it leaks into.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    These are the errors that are showing up. And I have added the break statements and this is what I'm still getting.

    C:\Program Files\Microsoft Visual Studio\MyProjects\Ch8_Ex6_West\Ch8_Ex6_West.cpp(31 ) : error C2051: case expression not constant

    warning C4060: switch statement contains no 'case' or 'default' labels


    There's 10 all together of the first error, plus the warning I have listed. Suggestions?

  8. #8
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Quote Originally Posted by MyntiFresh
    These are the errors that are showing up. And I have added the break statements and this is what I'm still getting.

    C:\Program Files\Microsoft Visual Studio\MyProjects\Ch8_Ex6_West\Ch8_Ex6_West.cpp(31 ) : error C2051: case expression not constant

    warning C4060: switch statement contains no 'case' or 'default' labels


    There's 10 all together of the first error, plus the warning I have listed. Suggestions?

    Ok seems like you can't use a variable after case:

    Code:
    switch (blah)
    {
       case variable:                                        // error
                            cout << "blah" << endl;
                            break;
       default:
                           cout << "blah2" << endl;
                           break;
    }

  9. #9
    *this
    Join Date
    Mar 2005
    Posts
    498
    OK so in your first example you use variables for cases, you can't do that. You use the variable for the switch then decide what to do depending on what that variable (used in the switch) is. so if you wanted a case for character A you would do this:

    Code:
    switch (operation) 
    {
       case 'A': break;
       default:
    }
    [EDIT]
    Sorry mrafcho001 didn't see your post.
    [/EDIT]

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Ok, been doing some revisions. I'm still getting one error and a warning....
    ~~~~C:\Program Files\Microsoft Visual Studio\MyProjects\Ch8_Ex6_West\Ch8_Ex6_West.cpp(23 ) : warning C4091: '' : ignored on left of 'char' when no variable is declared

    ~~~~C:\Program Files\Microsoft Visual Studio\MyProjects\Ch8_Ex6_West\Ch8_Ex6_West.cpp(23 ) : error C2143: syntax error : missing ';' before 'constant'

    Here's the revised code.

    Code:
    #include <iostream>
    
    void menu(char&, int&, int&, int&, int&);
    void addFractions(int, int, int, int, int, int);
    void subtractFractions(int, int, int, int, int, int);
    void multiplyFractions(int, int, int, int, int, int);
    void divideFractions(int, int, int, int, int, int);
    
    using namespace std;
    
    int main()
    {
    	char operation;
    	char 'a', 's', 'm', 'd', 'x', 'A', 'S', 'M', 'D', 'X';
    	int numeratorOne, denominatorOne, numeratorTwo, denominatorTwo;
    	int numeratorResult, denominatorResult;
    
    	menu(operation, numeratorOne, denominatorOne, numeratorTwo, denominatorTwo);
    	
    	switch(operation)
    	{
    	case 'A': case 'a': addFractions(numeratorOne, denominatorOne, numeratorTwo, 
    					denominatorTwo, numeratorResult, denominatorResult); break;
    	case 'S': case 's': subtractFractions(numeratorOne, denominatorOne, numeratorTwo, 
    					denominatorTwo, numeratorResult, denominatorResult);break;
    	case 'M': case 'm': multiplyFractions(numeratorOne, denominatorOne, numeratorTwo, 
    					denominatorTwo, numeratorResult, denominatorResult); break;
    	case 'D': case 'd': divideFractions(numeratorOne, denominatorOne, numeratorTwo, 
    					denominatorTwo, numeratorResult, denominatorResult); break;
    	case 'X': case 'x': break;
    	default: cout << "Invalid selection"; break;
    	}
    
    	return 0;
    }

  11. #11
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    What does this:
    Code:
    char 'a', 's', 'm', 'd', 'x', 'A', 'S', 'M', 'D', 'X';
    do?
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Quote Originally Posted by Epo
    What does this:
    Code:
    char 'a', 's', 'm', 'd', 'x', 'A', 'S', 'M', 'D', 'X';
    do?
    Duh.... brain fart on that one. Thanks for pointin that out. OK... so I got my program working almost perfectly. But... I want it to keep executing until the user inputs 'x'. Right now it's ending after I run the program once. Here's the code... it's pretty long, but I'm not sure what part needs to be dealt with to make it repeat the way it should.

    Code:
    #include <iostream>
    
    void menu(char&, int&, int&, int&, int&);
    void addFractions(int, int, int, int, int, int);
    void subtractFractions(int, int, int, int, int, int);
    void multiplyFractions(int, int, int, int, int, int);
    void divideFractions(int, int, int, int, int, int);
    
    using namespace std;
    
    int main()
    {
    	char operation;
    	int numeratorOne, denominatorOne, numeratorTwo, denominatorTwo;
    	int numeratorResult = 0, denominatorResult = 0;
    
    	menu(operation, numeratorOne, denominatorOne, numeratorTwo, denominatorTwo);
    	
    	switch(operation)
    	{
    	case 'A': case 'a': addFractions(numeratorOne, denominatorOne, numeratorTwo, 
    					denominatorTwo, numeratorResult, denominatorResult); break;
    	case 'S': case 's': subtractFractions(numeratorOne, denominatorOne, numeratorTwo, 
    					denominatorTwo, numeratorResult, denominatorResult);break;
    	case 'M': case 'm': multiplyFractions(numeratorOne, denominatorOne, numeratorTwo, 
    					denominatorTwo, numeratorResult, denominatorResult); break;
    	case 'D': case 'd': divideFractions(numeratorOne, denominatorOne, numeratorTwo, 
    					denominatorTwo, numeratorResult, denominatorResult); break;
    	case 'X': case 'x': break;
    	default: cout << "Invalid selection"; break;
    	}
    	return 0;
    }
    void menu(char& operation, int& numeratorOne, int& denominatorOne,
    		  int& numeratorTwo, int& denominatorTwo)
    {
    	cout << "This program performs operations on fractions. Enter"
    		 << "\na : To add fraction"
    		 << "\ns : To subtract fraction"
    		 << "\nm : To multiply fraction"
    		 << "\nd : To divide fraction"
    		 << "\nx : To exit the program" << endl;
    	cin >> operation;
    	cout << endl;
    	cout << "For fraction 1"
    		 << "\nEnter the numerator: ";
    	cin >> numeratorOne;
    	cout << endl;
    	cout << "Enter the denominator: ";
    	cin >> denominatorOne;
    	cout << endl;
    		if (denominatorOne == 0)
    		{
    			cout << "The denominator must be nonzero"
    				 << "\nEnter the denominator: ";
    			cin >> denominatorOne;
    			cout << endl;
    		}
    	cout << "For fraction 2"
    		 << "\nEnter the numerator: ";
    	cin >> numeratorTwo;
    	cout << endl;
    	cout << "Enter the denominator: ";
    	cin >> denominatorTwo;
    	cout << endl;
    		if (denominatorTwo == 0)
    		{
    			cout << "The denominator must be nonzero"
    				 << "\nEnter the denominator: ";
    			cin >> denominatorTwo;
    			cout << endl;
    		}
    }
    void addFractions(int numeratorOne, int denominatorOne, int numeratorTwo,
    				  int denominatorTwo, int numeratorResult, int denominatorResult)
    {
    	numeratorResult = (numeratorOne * denominatorTwo) + (numeratorTwo * denominatorOne);
    	denominatorResult = denominatorOne * denominatorTwo;
    	cout << numeratorOne << "/" << denominatorOne << " + " << numeratorTwo
    		 << "/" << denominatorTwo << " = " << numeratorResult << "/" 
    		 << denominatorResult << endl;
    }
    void subtractFractions(int numeratorOne, int denominatorOne, int numeratorTwo,
    				  int denominatorTwo, int numeratorResult, int denominatorResult)
    {
    	numeratorResult = (numeratorOne * denominatorTwo) - (numeratorTwo * denominatorOne);
    	denominatorResult = denominatorOne * denominatorTwo;
    	cout << numeratorOne << "/" << denominatorOne << " - " << numeratorTwo
    		 << "/" << denominatorTwo << " = " << numeratorResult << "/" 
    		 << denominatorResult << endl;
    }
    void multiplyFractions(int numeratorOne, int denominatorOne, int numeratorTwo,
    				  int denominatorTwo, int numeratorResult, int denominatorResult)
    {
    	numeratorResult = numeratorOne * numeratorTwo;
    	denominatorResult = denominatorOne * denominatorTwo;
    	cout << numeratorOne << "/" << denominatorOne << " * " << numeratorTwo
    		 << "/" << denominatorTwo << " = " << numeratorResult << "/" 
    		 << denominatorResult << endl;
    }
    void divideFractions(int numeratorOne, int denominatorOne, int numeratorTwo,
    				  int denominatorTwo, int numeratorResult, int denominatorResult)
    {
    	numeratorResult = numeratorOne * denominatorTwo;
    	denominatorResult = denominatorOne * numeratorTwo;
    	cout << numeratorOne << "/" << denominatorOne << " / " << numeratorTwo
    		 << "/" << denominatorTwo << " = " << numeratorResult << "/" 
    		 << denominatorResult << endl;
    }

  13. #13
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Currently, when 'X' or 'x' are entered, it will break out of the switch() statement and proceed to the "return 0;"

    You'll need to throw in a loop that keeps repeating around your main piece of code, perhaps a do/while loop:
    Code:
    ...
    do
    {
    	menu();
    	switch()
    	{
    	...
    	}
    }while( ( operation != 'x' ) && ( operation != 'X' ) );
    
    return 0;
    Very abbreviated, but I think it'd do the trick. The key is that you want it repeating your menu options, and also the swtich statement to handle the user's input.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Thanks. It's close now.... but now it's not exiting when I input x or X. Here's the main function....

    Code:
    #include <iostream>
    
    void menu(char&, int&, int&, int&, int&);
    void addFractions(int, int, int, int, int, int);
    void subtractFractions(int, int, int, int, int, int);
    void multiplyFractions(int, int, int, int, int, int);
    void divideFractions(int, int, int, int, int, int);
    
    using namespace std;
    
    int main()
    {
    	char operation;
    	int numeratorOne, denominatorOne, numeratorTwo, denominatorTwo;
    	int numeratorResult = 0, denominatorResult = 0;
    
    	do
    	{
    	menu(operation, numeratorOne, denominatorOne, numeratorTwo, denominatorTwo);
    	
    	switch(operation)
    	{
    	case 'A': case 'a': addFractions(numeratorOne, denominatorOne, numeratorTwo, 
    					denominatorTwo, numeratorResult, denominatorResult); break;
    	case 'S': case 's': subtractFractions(numeratorOne, denominatorOne, numeratorTwo, 
    					denominatorTwo, numeratorResult, denominatorResult);break;
    	case 'M': case 'm': multiplyFractions(numeratorOne, denominatorOne, numeratorTwo, 
    					denominatorTwo, numeratorResult, denominatorResult); break;
    	case 'D': case 'd': divideFractions(numeratorOne, denominatorOne, numeratorTwo, 
    					denominatorTwo, numeratorResult, denominatorResult); break;
    	case 'X': case 'x': break;
    	default: cout << "Invalid selection"; break;
    	}
    	}while((operation != 'x') || (operation != 'X'));
    
    	return 0;
    }

  15. #15
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    void menu(char&, int&, int&, int&, int&);
    Remember, when using pointers:
    Code:
    void MyFunc(char *MyChar)
    {
    //Notice the *MyChar, which means that MyChar is a pointer (*) to a character.
    //Not char&...
    }
    
    //Then, when calling the function
    MyFunc(&operation);
    //The "&" symbolizes that you are passing the address of operation as your parameter.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

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

Similar Threads

  1. benchmarking?
    By cs32 in forum C Programming
    Replies: 5
    Last Post: 02-14-2008, 06:37 AM
  2. Mutiple switches
    By quiet_forever in forum C++ Programming
    Replies: 2
    Last Post: 04-26-2007, 03:19 PM
  3. Unicode file I/O functions
    By MiamiCuse in forum C Programming
    Replies: 8
    Last Post: 11-04-2005, 12:09 PM
  4. error LNK2001: unresolved external symbol
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 07-12-2002, 08:45 PM

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