In Old School RuneScape, players often keep track of their gear collections, bank tabs, and favorite weapons. Imagine you are building a small tool for a clanmate who wants to quickly search their weapon stash. They don’t always remember the full weapon name—they might just type "sara" or "zam"—and your program should find all weapons that start with those letters.
In this assignment you will work with five famous weapons from Old School RuneScape:
Your program will allow a user to type part of a weapon’s name and see which weapons match.
But this assignment is about much more than RuneScape weapons.
You are learning skills that programmers use every day:
These are the same ideas used in search bars, databases, autocomplete systems, file finders, and even tools like Google search. Learning to carefully compare strings and validate input is an essential skill that will follow you through every programming job you ever have.
Think of this assignment as building your first tiny search engine.
To build this tool, you’ll take the problem apart into small pieces, just like real programmers do. First you’ll store the weapon names in your program, then you’ll read what the player types, compare their search to each weapon name one character at a time, and finally repeat the process until they are finished searching. Each step below corresponds to one part of that logic, so follow them in order and test your program as you go. You will need to:
Create five separate string constants to hold each weapon name. Store the weapon names in all lowercase or uppercase letters, because the search will be case-insensitive.
Read a search string (including whitespaces) the user wants to search the weapons for. If the user enters nothing (just hits the enter key) output "Must enter something to search for...\n" and read in the string again.
Search each weapon name to see if the weapon name starts with the string entered.
tolower() or toupper() so the search is case-insensitive.Example Idea:
weapon = "saradomin godsword" search = "sara" // use loop to compare weapon[0] to search[0], weapon[1] to search[1], etc.
If a weapon starts with the search string, output the weapon name.
Repeat (3) for all five weapons.
If none of the weapons match output "searchFor not found in any weapons.\n" replacing searchFor with the string the user entered.
This will create a program that allows the user to search one time for a string inside the starting of the 5 weapon’s names. The user may want to be able to perform multiple searches so the program should continue execution until the user is done searching. To add this functionality you will need to:
After each search ask "Find another weapon (0)no or (1)yes? " and read the selection from the user. If a non-number is entered or an invalid selection (anything not 0 or 1) is made then output "Invalid selection...\n" and keep asking/reading until a valid number is entered.
Wrap your entire program in a loop so it repeats the program when the selection is 1, and stops looping if the selection is 0.
An example of an interaction with your program is shown below. When interacted with, your program must produce the exact same results.
AS4 % ./a.out Enter a string to search weapons for: sara saradomin godsword saradomin cape Find another weapon (0)no or (1)yes? 0
AS4 % ./a.out Enter a string to search weapons for: Must enter something to search for... Enter a string to search weapons for: zam zamorak staff zamorak rune armor Find another weapon (0)no or (1)yes? 0
AS4 % ./a.out Enter a string to search weapons for: invalid invalid not found in any weapons. Find another weapon (0)no or (1)yes? 0
AS4 % ./a.out Enter a string to search weapons for: sara saradomin godsword saradomin cape Find another weapon (0)no or (1)yes? 2 Invalid selection... Find another weapon (0)no or (1)yes? invalid Invalid selection... Find another weapon (0)no or (1)yes? -1 Invalid selection... Find another weapon (0)no or (1)yes? 0
AS4 % ./a.out Enter a string to search weapons for: Must enter something to search for... Enter a string to search weapons for: zam zamorak staff zamorak rune armor Find another weapon (0)no or (1)yes? 1 Enter a string to search weapons for: error error not found in any weapons. Find another weapon (0)no or (1)yes? 1 Enter a string to search weapons for: sara saradomin godsword saradomin cape Find another weapon (0)no or (1)yes? 0
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.
Save: Save your code as main.cpp. Do not ignore this step or save your file(s) with different names.
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.