473,396 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. cout << "Please select a choice from the menu below: " << endl;
  2.     cout << "F)ind a word by index" << endl;
  3.     cout << "S)earch for a word" << endl;
  4.     cout << "O)utput the number of words" << endl;
  5.     cout << "E)xit"<< endl;
  6.     cin >> choice;
  7.  
  8.     while(choice != 'E' || 'e')
  9.     {
  10.         switch (choice)
  11.         {
  12.             case 'F' || 'f':  cout << readWords(wordList, nWords);
  13.                 break;
  14.         }

My question is, what should I have the function return, since when the choice is made, it calls a function.
Sep 11 '07 #1
4 6789
Ganon11
3,652 Expert 2GB
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.
Sep 11 '07 #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.
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.

Expand|Select|Wrap|Line Numbers
  1. bool userChoice(WordInfo list[LIST_SIZE])
  2. {
  3.     char choice;
  4.  
  5.     cout << "Please select a choice from the menu below: " << endl;
  6.     cout << "F)ind a word by index" << endl;
  7.     cout << "S)earch for a word" << endl;
  8.     cout << "O)utput the number of words" << endl;
  9.     cout << "E)xit"<< endl;
  10.     cin >> choice;
  11.  
  12.     while(choice != 'E' || 'e')
  13.     {
  14.         switch (choice)
  15.         {
  16.             case 'F': readWords(wordList, nWords);
  17.                 break;
  18.             case 'S': 
  19.                 break;
  20.             case 'O': ;
  21.         }
  22.     }
  23. return false;
  24. };
Sep 11 '07 #3
I have to create a switch statement that calls a statement when one of the choices is selected. i.e.

Expand|Select|Wrap|Line Numbers
  1. cout << "Please select a choice from the menu below: " << endl;
  2.     cout << "F)ind a word by index" << endl;
  3.     cout << "S)earch for a word" << endl;
  4.     cout << "O)utput the number of words" << endl;
  5.     cout << "E)xit"<< endl;
  6.     cin >> choice;
  7.  
  8.     while(choice != 'E' || 'e')
  9.     {
  10.         switch (choice)
  11.         {
  12.             case 'F' || 'f':  cout << readWords(wordList, nWords);
  13.                 break;
  14.         }



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
Sep 12 '07 #4
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.
Sep 12 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Skip Montanaro | last post by:
Stephen> { Stephen> 'one': lambda x:x.blat(), Stephen> 'two': lambda x:x.blah(), Stephen> }.get(someValue, lambda x:0)(someOtherValue) One thing to remember is that function calls in Python...
26
by: Joe Stevenson | last post by:
Hi all, I skimmed through the docs for Python, and I did not find anything like a case or switch statement. I assume there is one and that I just missed it. Can someone please point me to the...
35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
17
by: prafulla | last post by:
Hi all, I don't have a copy of C standard at hand and so anyone of you can help me. I have always wondered how switch statements are so efficient in jumping to the right case (if any)? Can...
2
by: MJ | last post by:
Hi We can use the switch statement, or if else statement instead of switch One more method is there which can replace the switch using the function pointer or the array of function pointer. If...
7
by: Colin King | last post by:
Amusingly, one can use a while(0) statement to allow one to perform a switch statement without breaks. The while (0) enables the continue statements to break out of the switch. Ugly and...
13
by: PeterZ | last post by:
Hi, Back to basics! My understanding is that the only way to exit a For-Next loop prematurely is with the 'break' keyword. How are you supposed to do that if you're inside a Switch...
2
by: Phillip B Oldham | last post by:
What would be the optimal/pythonic way to subject an object to a number of tests (based on the object's attributes) and redirect program flow? Say I had the following: pets = {'name':...
6
HaLo2FrEeEk
by: HaLo2FrEeEk | last post by:
I'm trying to make a switch statement that will handle hotkeys assigned to different functions in my application. The hotkeys will be user-changable, so I've got them saved as application settings. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.