C++ Lab: Choosing Combat

Tue Sep 02 2025
Updated: Thu Sep 18 2025
Facebook share linkTwitter/X share linkLinkedIn share linkReddit share linkReddit share link

Purpose

In this lab you will validate a single integer input and handle type/range errors, drive a tiny menu with a single switch, and use a nested selection inside one case to adjust behavior. These patterns matter because real programs must guard bad inputs first, then branch cleanly based on a user’s choice—think menus, CLI tools, config flags, and game actions. You’ll reuse the same validation-then-branch structure in upcoming work (loops, functions, structs), and this habit of checking inputs early and isolating decisions will make your code easier to test, grade, and debug later. This also maps directly to everyday tasks like form validation, command handling, and event routing in production code.


Task

Create a console program that asks the user to choose a combat style and then, based on that choice, asks for a specific weapon or spell option in that combat style. The weapon or spell will then inform a bonus that will be applied to the base damage of 10 to calculate an actual damage to be caused by the attack.

There are 2 combat styles melee and magic which have the weapons and spells:

combat styleweapon/spellbonus
meleedagger1.3
meleescimitar2.7
magicbolt1.9
magicblast3.0

Requirements & Constraints

  • The first prompt must read an integer for the combat style and validate that it’s a number.

  • The integer read in must be used in a switch statement to determine what weapon or spell is to be used, or if an invalid combat style was chosen.

  • Inside the selected switch case for each combat type, nest a secondary selection for the specific options between the weapons and spells.

  • If an input is invalid at any point, display a clear error message and stop.

  • Calculate a final damage using a base value combined with the selected option’s bonus, and output a brief result line indicating the option and the damage.


Steps to Complete the Lab

  1. Ask for the combat style as an integer. Immediately check that input succeeded as a number. If not, print "Error: Invalid number" and terminate the program.

  2. In a switch statement, use the integer read in to determine to ask for a weapon or spell based on the combat style, or that the value is not one of the allowed styles.

  3. Inside the chosen combat style’s respective case, prompt for the corresponding weapon or spell option (strings listed under that style). Or if an invalid combat style is chosen output "Error: Invalid selection" using the switch statement as well.

  4. Inside the respective cases, determine the bonus to multiply the base damage by based on the weapon or spell selection. If an invalid weapon is chosen print "Error: Invalid weapon" and terminate the program, and if an invalid spell is chosen print "Error: Invalid spell" and terminate the program.

  5. Compute the actual damage by multiplying the base damage of 10 by the bonus produced by the chosen weapon or spell.

  6. Output the results. The output should be in the form The WEAPON TYPE causes ACTUAL_DAMAGE damage with bonus BONUS. Where WEAPON is the name of the weapon or spell chosen, TYPE is either weapon or spell based on the respective combat style chosen, ACTUAL_DAMAGE is how much actual damage is caused by the attack, and BONUS is the bonus applied based on the weapon or spell.


Criteria for Success

See CodeGrade

Example Program Interaction

Choose a style (1=Melee -or- 2=Magic): 0 Error: Invalid selection
Choose a style (1=Melee -or- 2=Magic): 1 Choose a weapon (dagger or scimitar): dagger The dagger weapon causes 13 damage with bonus 1.3.
Choose a style (1=Melee -or- 2=Magic): 1 Choose a weapon (dagger or scimitar): mace Error: Invalid weapon
Choose a style (1=Melee -or- 2=Magic): 2 Choose a spell (bolt or blast): blast The blast spell causes 30 damage with bonus 3.
Choose a style (1=Melee -or- 2=Magic): 2 Choose a spell (bolt or blast): burst Error: Invalid spell

See CodeGrade for more examples.

Note: The line breaks after the inputs in the example output formatting above are the ones placed into the terminal by the user hitting enter on their keyboard to input. CodeGrade does not enter values with a keyboard, but rather supplies input via Linux redirection to programs. So in CodeGrade's auto tests there will not be line breaks after the inputs.


Submission Instructions

  1. Save: Save your code as main.cpp. Do not ignore this step or save your file(s) with different names.

  2. Submit: Your program source code must be submitted via CodeGrade as a properly named .cpp file prior to the deadline to receive full credit. Any submissions after the deadline will be subject to the class’ late policy.