I have a query that should be simple but I can't discover the answer anywhere.
Term used to describe a software program that is operated using file menus instead of using commands. Below is an example of how a user may quit a menu-driven program, as opposed to a non menu-driven program. Within an open program, the user clicks the 'File' option from the drop-down menu bar and selects 'Quit.' How do I Create a menu driven C# Console application that would simulate and analyse responses from a group of students that rated the food in the cafeteria. Each student rated the food as an integer value from 1 (bad) to 10 (exceptional). There are 100 students that rated the food.
![Menu-driven program on javascript download Menu-driven program on javascript download](/uploads/1/2/5/8/125822846/568358210.png)
I have a menus driven C program that works completely when the menus options are usually quantities but I can't physique out how to change the numbers to characters.
For illustration:
![Table Table](/uploads/1/2/5/8/125822846/164872543.png)
functions good when options are 1, 2, or 3 but not really A, T, Chemical.
Not really sure how I am intended to state the choice. is usually it a char? thanks in progress
KwakKwakKwakKwak
2 Solutions
When examining for a string, you should convert everything to a common situation. In you case you should convert the consumer input to higher case. You can do this by using toupper function
Here is definitely the little bit of code you need to change to make your program function
CaesarCaesar6,99855 gold badges2929 gold badges6161 bronze badges
Once you alter
while((choice != 'M') amp;amp; (option != 'D') amp;amp; (selection != 'Elizabeth')
towhile((choice != 'C') amp;amp; (selection != 'Chemical') amp;amp; (option != 'At the'))
, your program code runs properly. Although I would possess personally used astd::string
rather of achar
.Granted, Caesar's answer is a valid point - and I would modify that simply because well. However, it would become much better as a remark because it doesn't resolve the issue. Wait, keep on. What problem? Your program appears to nevertheless 'operate flawlessly' even with the alphabetic menu options.
The just pest in your program can be when you test and give
balence
(or any of your threedual
s i9000) to a non-numeric entrance. When I style 'D' for preliminary balance, I notice this monstrosity:Over and over and over. That's i9000 not enjoyment. Similar matter happens if I form a notice for deal quantity:
Answer: In no way try out to jam input used directly from the user into a adjustable that is not really a kind of thread. Use a constructed in string-to-number function like
atof
, or(much favored)add mistake handlers like this:By the way, it's spelled balance, not balence ;)
ProxyProxy1,29611 yellow metal logo1111 sterling silver badges2525 bronze badges
Not really the solution you're also searching for? Browse other questions tagged chemical or talk to your very own query.
Instance
Execute a block out of code based on consumer insight:
var text;
var fruit = document.getElementById('myInput').worth;
change(fruit)
case 'Banana':
text message = 'Banana can be great!';
crack;
case 'Fruit':
text = 'I feel not really a fan of tangerine.';
split;
case 'Apple company':
text = 'How you like them pears?';
crack;
default:
text = 'I have never heard of that fruits.';
Attempt it Yourself »var fruit = document.getElementById('myInput').worth;
change(fruit)
case 'Banana':
text message = 'Banana can be great!';
crack;
case 'Fruit':
text = 'I feel not really a fan of tangerine.';
split;
case 'Apple company':
text = 'How you like them pears?';
crack;
default:
text = 'I have never heard of that fruits.';
Even more 'Attempt it Yourself' good examples below.
Definition and Use
The change statement executes a stop of program code based on various cases.
The change statement is certainly a part of JavaScript's 'Conditional' Statements, which are usually utilized to execute different activities structured on various conditions. Make use of change to select one of numerous pads of program code to be performed. This is certainly the perfect answer for very long, nested if/else statements.
The change statement assess an appearance. The value of the manifestation is then likened with the values of each situation in the construction. If there is definitely a complement, the related engine block of code is executed.
The switch statement will be often utilized collectively with a break or a defauIt keyword (or bóth). These are usually both various:
Thesplitkeyword breaks out of the change engine block. This will stop the performance of even more execution of code and/or case tests inside the stop. If crack is omitted, the next code block out in the change statement is usually performed.
Thédefaultkeyword specifies somé code to run if there is certainly no case match up. There can just end up being one default kéyword in a switch. Although this is optional, it is usually recommended that you use it, as it requires treatment of unpredicted cases.Internet browser Support
Declaration | |||||
---|---|---|---|---|---|
switch | Yés | Yés | Yés | Yés | Yés |
Syntax
switch(reflection)
situationn:
code engine block
separate;
situationn:
program code mass
split;
defauIt:
defauIt code block
situationn:
code engine block
separate;
situationn:
program code mass
split;
defauIt:
defauIt code block
Paraméter Ideals
Parameter | Description |
---|---|
<ém>manifestationém> | Needed. Specifies an manifestation to be evaluated. The manifestation is evaluated as soon as. The worth of the phrase is compared with the values of each situation labels in the construction. If there is definitely a fit, the associated stop of code is carried out |
Techie Information
JávaScript Version: | ECMAScript 1 |
---|
More Good examples
Illustration
Make use of nowadays's weekday quantity to estimate the weekday name (Weekend=0, Monday=1, Tuesday=2,.):
var time;
switch (brand-new Date.getDay)
case 0:
day = 'Weekend';
break;
case 1:
day = 'Mon';
bust;
case 2:
day = 'Wednesday';
bust;
situation 3:
day time = 'Thursday';
break up;
case 4:
day time = 'Thursday';
separate;
situation 5:
day = 'Fri';
split;
case 6:
time = 'Saturday';
crack;
default:
day = 'Unknown Day';
Try out it YourseIf »switch (brand-new Date.getDay)
case 0:
day = 'Weekend';
break;
case 1:
day = 'Mon';
bust;
case 2:
day = 'Wednesday';
bust;
situation 3:
day time = 'Thursday';
break up;
case 4:
day time = 'Thursday';
separate;
situation 5:
day = 'Fri';
split;
case 6:
time = 'Saturday';
crack;
default:
day = 'Unknown Day';
Example
If nowadays is usually neither Saturday nor Sunday, create a default information:
var text;
switch (fresh Time.getDay)
case 6:
text message = 'Nowadays is Saturday';
crack;
case 0:
text message = 'Today is Weekend';
crack;
default:
text = 'Searching forwards to the Weekend break';
Attempt it YourseIf »switch (fresh Time.getDay)
case 6:
text message = 'Nowadays is Saturday';
crack;
case 0:
text message = 'Today is Weekend';
crack;
default:
text = 'Searching forwards to the Weekend break';
Example
Occasionally you will wish different instances to use the same program code, or fall-thróugh to a common default.
Note that in this instance, the situations talk about the same code stop, and that the default case does not possess to be the last case in a switch wedge (nevertheless, ifdefaultcan be NOT the final case in the change block, keep in mind to end it with a bust).
var text;
change (new Time.getDay)
situation 1:
situation 2:
case 3:
default:
text = 'Looking forward to the Weekend';
bust;
situation 4:
situation 5:
text = 'Shortly it is certainly Weekend break';
break up;
situation 0:
situation 6:
text message = 'It will be Weekend';
Test it YourseIf »change (new Time.getDay)
situation 1:
situation 2:
case 3:
default:
text = 'Looking forward to the Weekend';
bust;
situation 4:
situation 5:
text = 'Shortly it is certainly Weekend break';
break up;
situation 0:
situation 6:
text message = 'It will be Weekend';
Example
Making use of the change declaration to carry out a wedge of program code based on consumer insight, from a prompt box:
var text message;
var favDrink = fast('What's your preferred cocktail beverage?');
change(favDrink)
situation 'Martini':
text message = 'Superb option! Martini can be good for your spirit.';
bust;
case 'Daiquiri':
text message = 'Daiquiri will be my favorite too!';
bust;
situation 'Cosmopolitan':
text message = 'Really? Are usually you certain the Modern is certainly your favourite?';
break up;
default:
text = 'I have never noticed of that oné.';
Test it Yourself »var favDrink = fast('What's your preferred cocktail beverage?');
change(favDrink)
situation 'Martini':
text message = 'Superb option! Martini can be good for your spirit.';
bust;
case 'Daiquiri':
text message = 'Daiquiri will be my favorite too!';
bust;
situation 'Cosmopolitan':
text message = 'Really? Are usually you certain the Modern is certainly your favourite?';
break up;
default:
text = 'I have never noticed of that oné.';
Related Web pages
JavaScript Tutorial: JavaScript If.Else Statements
JavaScript Tutorial: JavaScript Switch Declaration
JavaScript Reference: JavaScript if/else Statement
JavaScript Guide: JavaScript split Declaration
❮ JavaScript Claims Benchmark