Homework 13: Flash Cards
Due: Tuesday 5/8 by 11:59pm
Objectives
Practice working with string functions from string library.
The Assignment
Write a program that helps a middle school student memorize the capitals of the fifty states in the United States. Your program should randomly select 1 of the 50 states and ask the user to type in the name of the capital. Your program should continue to quiz the user until the user types in 'quit'.
A sample run of your program might look like:
PT[131]% ./a.out What is the capital of New Hampshire? Nashua Wrong! The capital of New Hampshire is Concord. What is the capital of Rhode Island? Providence Right! What is the capital of Minnesota? Saint Paul Right! What is the capital of Oklahoma? quit Good-bye! PT[132]%
Use flash.c as the starting point of the program. The main program already has two arrays of strings defined for you: states and capitals. The first one has the names of all 50 states. The second one has the names of the 50 capitals. The two arrays are coordinated, so states[i] is the name of the i-th state and capitals[i] has the name of its capital city. For example, states[3] is "Arkansas" and capitals[3] is "Little Rock".
The main program in flash.c also seeds the random number generator for you.
Notes
-
Recall that some state capitals have a two
word name (e.g., Little Rock, Arkansas and Saint Paul, Minnesota).
Thus you cannot simply use:
scanf("%s", str) ;
since that will read only until the white space character (i.e., space, tab and newline). See the example in upper1.c, where
scanf("%[^\n]s", str) ; // read until newline scanf("%c", &cr) ; // advance past newline will read in the user input int str up to the newline. The second call to scanf() removes the newline character from the input buffer so the next line of input can be read in.
- You may assume that the user will not enter a string that is longer than 100 characters.
- Use the strcmp() function in the string library to compare two strings.
-
You can use the random number generator this way:
i = random() % 50 ;
This will store in i a random number between 0 and 49 (inclusive).
Optional Embellishment
Add this feature to your program: when the user types in a wrong answer, check if the city is the capital of a different state. Then remind the user of this fact. For example:
PT[133]% ./a.out What is the capital of New Hampshire? Providence Wrong! The capital of New Hampshire is Concord. Providence is the capital of Rhode Island.
Submit
Use the script command to record several sample runs of your program. Then, submit your program and sample run as usual:submit cs104_chang hw13 flash.c typescript