Hello. As a way of testing out switch, I wrote a simple currency converter. The problem I have is that when one currency is converted to another, it immediately jumps to the next currency to be converted. I have checked the syntax and I have break; in all the correct places as far as I can tell.
Thanks guys, that did the trick.
Now however, after adding a default case, it only applies to case 1 like this:
1 2 3 4 5 6 7 8 9 10 11
switch (currency1)
{
case 1:
cout << "\nYou have selected Euro.\n\n"
<< "Please choose a value in Euro: ";
cin >> value;
cout << "\nYou have chosen " << value << " Euro.\n\n"
<< "Please choose the currency you wish to convert to: ";
cin >> currency2;
default:
cout << "\nInvalid value.\n\n\n\n";
It won't let me use any more default cases, yet the default case I added only applies to case 1, for Euro.
As well as this, I am trying to find a way to stop the program from crashing when a non numeric value is entered. Is there an easy, simple way to do this?
The last thing that I would like to know is what better ways there are of looping the code, aside from the dowhile loop I have implemented.
I know that's a lot, so thanks to anyone who can help me with any of that.
Edit: Actually, default doesn't work properly, even on case 1. switch is new to me, so I'm probably just making simple mistakes, but I can't figure it out.
You're missing break in all cases of your outermost switch statement.
It won't let me use any more default cases, yet the default case I added only applies to case 1, for Euro.
default applies to the switch statement as a whole, not to a specific case. i.e. if currency1 does not match any of the named cases, then the default branch is executed.
Because you have no break statement after lne 9, case 1 will always fall through to the default case.
IMO, nested switch statements are ugly and prone to error. Much better practice to isolate the nested switch statements to their own functions. This also makes the outer switch statement easier to read since the name of the function being called should be meaningful.
Line 9 is the incorrect place. You didn't show the nested switch statement, so line 9 appeared to be the last executable statement in the case.
The break for cases in the outer switch belongs after the inner switch statements.
Referring to your original code, you should have a break at line 63 and after line 104. This is precisely why I said nested switch statements are ugly and error prone. Had the inner switch statements been in their own functions, the correct place for the missing break statements would have been very obvious.
@AbstractionAnon;
I actually do have break; now implemented at the places you referred to in the code. It's still not working, so I think I'll just put this one down to experience, whilst simultaneously fostering my preference for if/elseif/else over switch. As for functions, yes I do believe it's time to start learning how to use them. Otherwise, I'm probably going to end up writing some mighty unwieldy code.
My other questions still stand if anyone is willing to answer them.
Is there a simple, all purpose way of dealing with non-numeric input handling?
Which loop is best for running the same code more than once, with added control, such as "would you like to go again?", followed by a choice of yes or no. At the moment I am using what feels like a crude dowhile loop to make my code run indefinitely. Is there a way to add more control to your loop? (I'm trying to move away from goto).
whilst simultaneously fostering my preference for if/else if/else over switch.
There's nothing wrong with switch statements when implemented correctly.
When used to select between a number of cases, switch statements are often clearer than a jumble of if/else statements. I think you will find with your current program structure with nested statements, nested if/else statements will actually get more unwieldy than switch statements.
Is there a simple, all purpose way of dealing with non-numeric input handling?
The standard way is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int val;
cout << "Enter input:";
do
{ if (! cin >> val)
{ // input failed (not numeric)
cin.clear (); // reset the stream error flags
cin.ignore (1024); // eat any chars left in the buffer
cout << "try again: ";
continue; // next iteration of the loop
}
// have good input
} while (val < 1 || val > 6); // or whatever the valid input range is
// Have range checked value
Which loop is best for running the same code more than once, with added control
do/while is just fine for that.
I'm trying to move away from goto
Good move.
As for functions, yes I do believe it's time to start learning how to use them
Any time you see a lot of repeated code or even code that looks very similar, that's a good indication the repeated or similar code should be moved into a function.
You should consider a table driven approach to your code.
I did this for only 3 currencies, but you should be able to extend it to any number of currencies easily simply by modifying MAX_CURRENCY and the two tables. The code does not change. No nested switch or if statements.
You have been a great help. You have answered all my questions thoroughly and with clarity. It's these kinds of answers that really help learners to attain new insights into the language and its varied concepts. Thank you.