What You Will Do In Your Lab
Consider the following programming problem from CPP w/o Fear, by Overland:
Exercise 05.03.01.txt, p. 126.
Write a program that randomly selects from a bag of eight
objects. Each object can be red, blue, orange, or green, and it can be
a ball or a cube. Assume the bag contains one object for each
combination (one red ball, one red cube, one orange ball, one orange
cube, and so on). Write code similar to dealer1.cpp, using two string
arrays-- one to identify colors and the other to identify shapes.
The following c++ source file solves the problem: Ex-5-3-1-126.cpp, but uses c-style headers, does not use a eof-controlled while loop, and uses c-strings (char*) rather than c++ strings.
In your lab this week, follow your lab instructor's directions to modify Ex-5-3-1-126.cpp as follows:
- Download Ex-5-3-1-126.cpp to your local machine, open it in Emacs and save it as "randShapes.cpp." Compile and run it.
- Replace the c-style headers with c++ headers. Replace
char* with string. Compile and run the new version.
- Replace the while loop in main with an eof-controlled while loop that generates this I/O interface:
% a.out
Enter no. of items to draw (c-d to exit): 3
red cube
red ball
blue ball
Enter no. of items to draw (c-d to exit): 2
green cube
green ball
Enter no. of items to draw (c-d to exit): 0
Exiting..
- Modify draw_an_item() so that it returns a value of type string and does not use cout. Modify main() to call draw_an_item() correctly. Compile and run the new version. (randShapes.cpp now uses the same control structure, headers, and array types as dealer1.cpp.)
- The only difference between dealer1.cpp and dealer2.cpp is that dealer2.cpp makes a single call to draw_an_item(). Compare lines 55-56 in dealer1.cpp with lines 50-52 in dealer2.cpp.
Modify randShapes.cpp to use dealer2.cpp's approach. Compile and run the new version.
|