Switch statement that calls functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jla1357
    New Member
    • Sep 2007
    • 4

    Switch statement that calls functions

    I have to create a switch statement that calls a statement when one of the choices is selected. i.e.

    Code:
    cout << "Please select a choice from the menu below: " << endl;
    	cout << "F)ind a word by index" << endl;
    	cout << "S)earch for a word" << endl;
    	cout << "O)utput the number of words" << endl;
    	cout << "E)xit"<< endl;
    	cin >> choice;
    	
    	while(choice != 'E' || 'e')
    	{
    		switch (choice)
    		{
    			case 'F' || 'f':  cout << readWords(wordList, nWords);
    				break;
    		}

    My question is, what should I have the function return, since when the choice is made, it calls a function.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Is this switch statement embedded in a function? If not, then I only see one function (readWords), which should presumably return something that can be cout'd.

    If the switch statement is embedded in a function, then you don't have to return anything inside the switch statement, as indicated by your loop (which will keep going while the user has not entered 'e' or 'E' to exit). You will have to return something outside of the loop.

    Comment

    • jla1357
      New Member
      • Sep 2007
      • 4

      #3
      Originally posted by Ganon11
      Is this switch statement embedded in a function? If not, then I only see one function (readWords), which should presumably return something that can be cout'd.

      If the switch statement is embedded in a function, then you don't have to return anything inside the switch statement, as indicated by your loop (which will keep going while the user has not entered 'e' or 'E' to exit). You will have to return something outside of the loop.
      thanks, this is a start to what I was going for, it's incomplete but hopefully it can give the idea of what I am trying to do. If you see anything wrong please tell me.

      Code:
      bool userChoice(WordInfo list[LIST_SIZE])
      {
      	char choice;
      
      	cout << "Please select a choice from the menu below: " << endl;
      	cout << "F)ind a word by index" << endl;
      	cout << "S)earch for a word" << endl;
      	cout << "O)utput the number of words" << endl;
      	cout << "E)xit"<< endl;
      	cin >> choice;
      	
      	while(choice != 'E' || 'e')
      	{
      		switch (choice)
      		{
      			case 'F': readWords(wordList, nWords);
      				break;
                  case 'S': 
      				break;
      			case 'O': ;
      		}
      	}
      return false;
      };

      Comment

      • deoashish
        New Member
        • Sep 2007
        • 2

        #4
        Originally posted by jla1357
        I have to create a switch statement that calls a statement when one of the choices is selected. i.e.

        Code:
        cout << "Please select a choice from the menu below: " << endl;
        	cout << "F)ind a word by index" << endl;
        	cout << "S)earch for a word" << endl;
        	cout << "O)utput the number of words" << endl;
        	cout << "E)xit"<< endl;
        	cin >> choice;
        	
        	while(choice != 'E' || 'e')
        	{
        		switch (choice)
        		{
        			case 'F' || 'f':  cout << readWords(wordList, nWords);
        				break;
        		}



        My question is, what should I have the function return, since when the choice is made, it calls a function.
        --------------------------------------------------------------------------------------------------------------------
        Well , you can return anything from a function right from a pointer to any built in data type.. but you cannot return a reference to any of the user defined datatype as the cout will not know what you have defined as the definition for aything can vary from person to person

        Comment

        • gnanapoongothai
          New Member
          • Jun 2007
          • 62

          #5
          hi,
          u r only calling a function inside a switch so the return value u can store it in a variable and the continue with ur case condtions anf finally break. use a variable to store the return value.
          Hope i answered ur question.

          Comment

          Working...