Need help creating a function and error with switch statement - c++

I'm using a switch statement to process through some code, which I will create a function in my main to call. I'm having trouble with the switch quantity with my switch statement. Also, how would I create a function for these 10 cases? Very confused, any help greatly appreciated! Thanks! Here is my current code:
#include <iostream>
using namespace std;
void constitution(); // the function that will call my amendments in my main function
// how will i create this will 10 possible amendments to choose from?
// There are 4 pirates and they will vote either yes or no,
int main()
{
cout << "Below is a list of the 10 amendments that the two pirates will vote on according to the ships constitution" << endl;
string amendments = 0;
cin >> amendments; // selecting the amendment
switch (amendments)
{
// for outputting the amendment(s) voted on, which will
// be passed on to a function in main to call
case 1: cout << "What does the fox say? Whatever WE tell it to"; //case 1-10 are the 10 amendments to be voted on
break;
case 2: cout << "From now on the annual Cinco de Mayo party will be held on March 8th ";
break;
case 3: cout << "Beginning this year, sharks shall have a week dedicated to us";
break;
case 4: cout << "Pirates are now allowed to talk about fight club";
break;
case 5: cout << "When in Rome, the Romans will do as WE do.";
break;
case 6: cout << "Our mothers will immediately get tattoos that say SON";
break;
case 7: cout << "From now on the President will take our birthdays off.";
break;
case 8: cout << "If we say something costs an arm and a leg, it does";
break;
case 9: cout << "Freemasons are ordered to learn OUR secret handshake.";
break;
case 10: cout << "If a tree falls in the forest and no one is around, it will make a sound only with our permission ";
break;
default: cout << "This won't be used since a amendment will always be voted on, thus never be shown or checked I believe.. (Please correct me) ";
break;
}
return 0;
}

The only thing you need to change is making amendments an integer, as switch expects an int.
int main()
{
cout << "Below is a list of the 10 amendments that the two pirates will vote on according to the ships constitution" << endl;
int amendments = 0;
cin >> amendments; // selecting the amendment
switch (amendments)
{
...
}
If you want to use amendments as a String, I am afraid you are not going to be able to check it via switch. Instead, you should use if/else if statements:
if(amendments == '1') {
...
} else if (amendments == '2') {
...
}

Related

C++; Is there a way to get my Switch function to reject a valid and invalid answer together?

So I've been learning how to use the "switch" statement recently. I decided to do some practice exercises. The code I show in my sample code below is one I came up with myself as a solution for a "Cola Machine" beginner exercise I found on a website "cplusplus". The exercise problem text can be found as a multi-line comment at the top of my code. Though, I did decide to try to make my code do more than what was required of the exercise.
For the most part, this code works exactly how I want it to. At first I even struggled to figure out how to get the code to repeat the user input in the line switch (cin.get()), if they had entered an invalid answer, but I solved this issue by nesting the "switch" statment inside of a "for" statement, for (int x = 0; x < 1; x++), and having the invalid answers decrement the counter ( x--; ). Not sure if that's a practical solution, but it's the one I came up with.
The only time my program doesn't run as intended is when a multiple character input that contains both valid answers and invalid answers is entered into the program (i.e. "-1" or "17"). The latter example outputs the statement for only the valid part of the answer, the statement for case '1', where as the former example outputs the statements for invalid answers and valid answers, so the default case and case '1'. Example for input "-1"
I'm wondering if that's just a problem that is inherent to using using "switch" statements in general, or if there's a practical solution I've yet to learn. I'm using a book to learn how to code, "C++ How To Program" by H.M. Deitel/P.J. Deitel, and they had given me a sample code for a letter grade counting program that can be found in my previous question on here. And I found that this same problem occurs in that sample code.
I'd be happy to hear any and all solutions, thank you ! :)
/* Write a program that presents the user w/ a choice of your 5 favorite beverages (Coke, Water, Sprite, ... , Whatever).
Then allow the user to choose a beverage by entering a number 1-5.
Output which beverage they chose.
>> Modify the program so that if the user enters a choice other than 1-5 then it will output "Error. choice was not valid, here is your money back." */
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main()
{
string drink1 = "Water", drink2 = "Coke", drink3 = "Pepsi",
drink4 = "Orange Juice", drink5 = "Powerade";
string choose_text = "\n\nChoose your preferred drink: ", chosen_text = "\nYou have chosen: ";
cout << "1.) " << drink1.append(17 - drink1.length(), ' ') << "2.) " << drink2.append(17 - drink2.length(), ' ')
<< "3.) " << drink3.append(17 - drink3.length(), ' ') << "4.) " << drink4.append(17 - drink4.length(), ' ')
<< "5.) " << drink5.append(17 - drink5.length(), ' ') << choose_text;
for (int x = 0; x < 1; x++) {
switch (cin.get()) {
case '1':
cout << chosen_text << drink1 << endl;
break;
case '2':
cout << chosen_text << drink2 << endl;
break;
case '3':
cout << chosen_text << drink3 << endl;
break;
case '4':
cout << chosen_text << drink4 << endl;
break;
case '5':
cout << chosen_text << drink5 << endl;
break;
case '\n': case ' ':
x--;
break;
default:
cout << "\nError. Choice was not valid.\nPlease enter an option 1 - 5." << choose_text;
x--;
break;
}
}
return 0;
}
You can do this:
int answer;
cin >> answer;
That will retrieve an integer value from the user instead of a single character. That solves part of your problem.
Now, let's look at your loop. Ick. Let's try this:
bool keepWorking = true;
do {
keepWorking = false;
int answer;
cin >> answer;
switch (answer) {
...
set keepWorking to true on the bad answers
}
} while (!cin.eof() && keepWorking);

c++ avoiding boolean flag to leave loop while using return 0

I have a do while loop which consists of two switch statements, some output code and some nested while loops to check for input errors. The thing is I want to break the switch statement when the user inputs 'Q' for quit and skip the rest of the code. So I've essentially got two problems.
If i use a do while, then it turns into being a return 0 and a boolean flag while(true) which logically goes against itself.
if i drop the do while and only use return 0, the code can't be executed multiple times.
I've come to terms with this being a flow problem rather than a syntax problem and was wondering how I should structure the flow to make it "clean code".
A quick example:
do {
char answer;
cout << "Type answer: ";
cin >> answer;
switch (answer) {
case A:
cout << "hello";
break;
case B:
cout << "more weird output";
break;
case Q:
cout << "Goodbye";
return 0;
}
cout << "more useless output that I want to skip";
cout << "how does this even work";
} while (run);
Here I've a return 0 which completely negates the need for a while(run) flag. This is bad coding practice I've been told, so I was wondering how one would go about structuring this in a good manner?
Here I think i fixed the code. Make sure you are typing a capital Q not lowercase. Also you forgot ' ' around your chars. Your logic was right - just small errors :) Goodluck!
#include <iostream>
using namespace std;
int
main ()
{
bool run = true;
do
{
char answer;
cout << "Type answer: ";
cin >> answer;
switch (answer)
{
case 'A':
cout << "hello";
break;
case 'B':
cout << "more weird output";
break;
case 'Q':
cout << "Goodbye";
return 0;
}
cout << "more useless output that I want to skip";
cout << "how does this even work";
}while (run);
return 0;
}

loop not working?

After the call for Back to Main Menu, it returns to the mainMenu but when option or command is typed, the option is not accepted or the loop not working. Wonder where is the mistake? Is it extra call should be added or?
#include <iostream>
using namespace std;
char mainMenu(void);
int factorial(int n);
unsigned long long combination(long nK, long nR);
int main(){
char option;
int shape,function,i,j,k,t,n;
long nK, nR;
unsigned long long COM;
while((option=mainMenu())!='0')
{
switch(option)
{
case '1'://Program 1:
cout<< "*Drawing a shape\n"
<< "(1-Rectangle, 2-Triangle, 3-Inverted Triangle, 4-Letter 'H', 0-Back to Main Menu)\n";
do
{
cout<< "Choose shape >> ";
cin>> shape;
cout<< endl;
switch(shape)
{
case 1: break;
case 2: break;
case 3: break;
case 4: break;
case 0:
//Back to Main Menu
cout<< "Back to main menu\n"
<< endl;
return mainMenu(); //After here, it does back to Main Menu but command or option is not working
}
}while(shape!=0);
case '2': //Program 2
cout<< "*Choose function of calculator\n"
<< "(1-Factorial, 2-Combination, 0-Back to main menu)\n";
do
{
cout<< "Choose function >> ";
cin>> function;
cout<< endl;
switch(function)
{
case 1: break;
case 2: break;
case 0:
cout<< "Back to main menu\n"
<< endl;
return mainMenu();
}
}while(function!=0);
case '0':
cout<< "Program is terminating\n"
<< endl;
return 0;
default:
cout<< "Wrong input. Please choose one of the above options.\n"
<< endl;
return mainMenu();
}
}
}
char mainMenu(void){
char option;
cout<< "##############################\n"
<< "Main Menu\n"
<< "Enter your command!\n"
<< "##############################\n"
<< endl
<< "1. Program1\n"
<< "2. Program2\n"
<< "0. Exit\n"
<< endl
<< "Command >> ";
cin>> option;
cout<< endl;
return option;
}
I'm not sure what your question is, but your code is missing 2 important things. First, you need break statements at the end of each case block, otherwise the program flow will continue on to the next case statement.
Second, the inner menu doesn't ever escape the inner while(1) loop. This is a possible case for a goto use, although in practice it would better to refactor the code to split the top menu and inner menu into two functions, and use a return in the inner menu to return to the outer menu.
I'm not sure what your question is, but your code is missing 2 important things. First, you need break statements at the end of each case block, otherwise the program flow will continue on to the next case statement.
Second, the inner menu doesn't ever escape the inner while(1) loop. This is a possible case for a goto use, although in practice it would better to refactor the code to split the top menu and inner menu into two functions, and use a return in the inner menu to return to the outer menu.
As said, you're code is missing various things. It would be awesome if you distribute the entire code, and additionally the exact error message with line.
void value not ignored as it ought to be?...
...Is not that much of an explanation...
Also, are you sure you included iostream?
#include iostream
That said, you did not declare any of the variables used in the program.
You also missed a space in line 2 of your mainMenu() function.
Also, please tell us what you expected to happen.

Question about using a switch statement

First, I realize that from a performance perspective, the design of this switch statement is slow because cout is being called several times in certain cases. That aside, is this style of writing a switch statement not good coding practice. In other words, would it be better to handle each case individually and break or is the fall-through better?
int main(void)
{
int number;
cout << "Enter a number between 1 and 10 and I will display its Roman numeral equivalent." << endl
<< "> ";
cin >> number;
cout << "Roman numeral: ";
switch (number)
{
case 3:
cout << "I";
case 2:
cout << "I";
case 1:
cout << "I";
break;
case 4:
cout << "I";
case 5:
cout << "V";
break;
case 6:
cout << "VI";
break;
case 7:
cout << "VII";
break;
case 8:
cout << "VIII";
break;
case 9:
cout << "I";
case 10:
cout << "X";
break;
default:
cout << "Error!\nYou did not enter a number between 1 and 10";
}
cout << endl;
return 0;
}
Switch statements aren't slow, they're usually optimised to jump-tables by the compiler. And if you're sure that switch statement works as expected, it's fine and is a pretty cool way of doing it.
That said, you'd have the same number of cases if you handled each one individually. If that's all you're doing, I'd probably change it to handle each one seperately and not do fall through. It's more comprehendable and easily maintained that way:
switch (number)
{
case 1:
cout << "I";
break;
case 2:
cout << "II";
break;
case 3:
cout << "III";
break;
case 4:
cout << "IV";
break;
case 5:
cout << "V";
break;
case 6:
cout << "VI";
break;
case 7:
cout << "VII";
break;
case 8:
cout << "VIII";
break;
case 9:
cout << "IX";
break;
case 10:
cout << "X";
break;
default:
cout << "Error!\nYou did not enter a number between 1 and 10";
}
And like #paxdiablo suggested, to enhance readability you can put the case, statement, and break all on the same line if it looks better to you:
case 1: cout << "I"; break;
case 2: cout << "II"; break;
// etc.
It's such a simple case, it's hard to say it's really "bad." I've heard differing opinions on lettings cases fall through into other cases. And I'm sure at some point, we've all done that. (I usually document it quite clearly with something like /* FALL-THROUGH */ so the next person reading the code knows I meant to do that.)
I think a more complex example would demonstrate it's not such a great idea. But not really because a fall-through itself is a bad thing. But because a better design wouldn't warrant it. In object oriented design, a case statement might indicate that you've got a "code smell" -- that you're not letting the object do what it needs to based on type rather than based on some other piece of information.
Now, if someone really wanted to get picky about your admittedly simple example, one could say you're mixing controller, model, and view together in bad ways. Your controller should simply get the input. Your model would have better ways of obtaining the alternate representation of the input (a map, or heck, a case statement, I dunno), and your view logic would not be scattered in and around your controller logic. By actually adhering to some other design concepts, the switch statement might vanish entirely. That might happen with other examples, too.
In short, I think if your design is sound, you might find that if a switch statement IS even in fact necessary, then the concern about a fall-through case statement isn't a big deal.
I tend not to use that style since it's often difficult to figure out the flow of control at a single glance. This is one reason why goto is generally considered a bad idea (though some people take that as gospel without understanding why - it's actually quite handy in some circumstances provided it doesn't render the code unreadable).
In other words, I prefer each case to be independent. If they have commonality, I will tend to break that out into separate functions and call those functions from each case.
That doesn't cover situations where the code is identical for different cases, where I just use something like (pseudo-code, obviously):
case 1: case 2: case 3:
print "It's one, two or three"
For your specific use case, I would probably just use a table lookup like:
char *roman[] = {"I", "II", "III", "IV", ... "X"};
if ((n < 1) || (n > 10))
cout << "Urk! I only have ten fingers!";
else
cout << roman[n-1];
just to keep the (source) code compact.
Using the Break for each case should be the perferred option. Using "Fall-Through" will move on to the code for the next case, which could cause errors and hinder performance.
You are right in that calling the '<<'-operator several times incurs a certain amount of overhead. However, you're talking about two statements here so this is probably not the point to optimize.
If you want to optimize the code, why not use a static array of strings containing the roman numbers? Like roman[0] = "I", roman[1] = "II", etc. I doubt this representation will cost you more memory than the above function and you get rid of the bloated switch statement.
That is very clever, and this will be plenty fast. But if you want to improve performance ...
In each switch statement, add the string you are couting to a running string buffer. Then, after the switch statement, cout the buffer.
This is a little confusing to read/understand. Simply handling each of the 10 cases individually would be simpler and would perform better (though using a minuscule amount more memory).
Switch statements can be optimized into jump tables by the compiler, so they're not always slow. And they're definitely better than writing a bunch of if - else if statements. I personally like fall through because it allows you to do some cool stuff in certain cases without having to repeat code; but in general, they are frowned upon because they can be harder to understand than handling each case individually.
As for your example, if you're worried about multiple calls to cout, you can always store the intermediate strings in a stringstream and print the final string. However, output to cout is buffered, so I don't know whether this will have any significant performance improvement.
#include <iostream>
#include <ios>
#include <sstream>
int main(void)
{
using namespace std;
int number = -1;
cout << "Enter a number between 1 and 10 and I will display its Roman numeral equivalent." << endl
<< "> ";
cin >> number;
ostringstream oss( "Roman numeral: ", ios_base::ate );
switch (number)
{
case 3:
oss << "I";
case 2:
oss << "I";
case 1:
oss << "I";
break;
case 4:
oss << "I";
case 5:
oss << "V";
break;
case 6:
oss << "VI";
break;
case 7:
oss << "VII";
break;
case 8:
oss << "VIII";
break;
case 9:
oss << "I";
case 10:
oss << "X";
break;
default:
cout << "Error!\nYou did not enter a number between 1 and 10";
return -1;
}
cout << oss.str() << endl;
return 0;
}

Grouping switch statement cases together?

I may be over looking something but is there a simple way in C++ to group cases together instead of writing them out individually? I remember in basic I could just do:
SELECT CASE Answer
CASE 1, 2, 3, 4
Example in C++ (For those that need it):
#include <iostream.h>
#include <stdio.h>
int main()
{
int Answer;
cout << "How many cars do you have?";
cin >> Answer;
switch (Answer)
{
case 1:
case 2:
case 3:
case 4:
cout << "You need more cars. ";
break;
case 5:
case 6:
case 7:
case 8:
cout << "Now you need a house. ";
break;
default:
cout << "What are you? A peace-loving hippie freak? ";
}
cout << "\nPress ENTER to continue... " << endl;
getchar();
return 0;
}
AFAIK all you can do is omit the returns to make things more compact in C++:
switch(Answer)
{
case 1: case 2: case 3: case 4:
cout << "You need more cars.";
break;
...
}
(You could remove the other returns as well, of course.)
Sure you can.
You can use case x ... y for the range
Example:
#include <iostream.h>
#include <stdio.h>
int main()
{
int Answer;
cout << "How many cars do you have?";
cin >> Answer;
switch (Answer)
{
case 1 ... 4:
cout << "You need more cars. ";
break;
case 5 ... 8:
cout << "Now you need a house. ";
break;
default:
cout << "What are you? A peace-loving hippie freak? ";
}
cout << "\nPress ENTER to continue... " << endl;
getchar();
return 0;
}
Make sure you have "-std=c++0x" flag enabled within your compiler
No, but you can with an if-else if-else chain which achieves the same result:
if (answer >= 1 && answer <= 4)
cout << "You need more cars.";
else if (answer <= 8)
cout << "Now you need a house.";
else
cout << "What are you? A peace-loving hippie freak?";
You may also want to handle the case of 0 cars and then also the unexpected case of a negative number of cars probably by throwing an exception.
PS: I've renamed Answer to answer as it's considered bad style to start variables with an uppercase letter.
As a side note, scripting languages such as Python allow for the nice if answer in [1, 2, 3, 4] syntax which is a flexible way of achieving what you want.
You can't remove keyword case. But your example can be written shorter like this:
switch ((Answer - 1) / 4)
{
case 0:
cout << "You need more cars.";
break;
case 1:
cout << "Now you need a house.";
break;
default:
cout << "What are you? A peace-loving hippie freak?";
}
You can use like this:
case 4: case 2:
{
//code ...
}
For use 4 or 2 switch case.
Your example is as concise as it gets with the switch construct.
If you're willing to go the way of the preprocessor abuse, Boost.Preprocessor can help you.
#include <boost/preprocessor/seq/for_each.hpp>
#define CASE_case(ign, ign2, n) case n:
#define CASES(seq) \
BOOST_PP_SEQ_FOR_EACH(CASE_case, ~, seq)
CASES((1)(3)(15)(13))
Running this through gcc with -E -P to only run the preprocessor, the expansion of CASES gives:
case 1: case 3: case 15: case 13:
Note that this probably wouldn't pass a code review (wouldn't where I work!) so I recommend it be constrained to personal use.
It should also be possible to create a CASE_RANGE(1,5) macro to expand to
case 1: case 2: case 3: case 4: case 5:
for you as well.
gcc has a so-called "case range" extension:
http://gcc.gnu.org/onlinedocs/gcc-4.2.4/gcc/Case-Ranges.html#Case-Ranges
I used to use this when I was only using gcc. Not much to say about it really -- it does sort of what you want, though only for ranges of values.
The biggest problem with this is that only gcc supports it; this may or may not be a problem for you.
(I suspect that for your example an if statement would be a more natural fit.)
No, unless you want to break compatibility and your compiler supports it.
#include <stdio.h>
int n = 2;
int main()
{
switch(n)
{
case 0: goto _4;break;
case 1: goto _4;break;
case 2: goto _4;break;
case 3: goto _4;break;
case 4:
_4:
printf("Funny and easy!\n");
break;
default:
printf("Search on StackOverflow!\n");
break;
}
}