This document contains code for two C++ programs. The first program, betterinputFunc.cpp, defines a function called get_int_range that forces the user to enter an integer within a specified minimum and maximum range. The second program, betterDie.cpp, contains functions for simulating the roll of a six-sided die. It initializes randomness, displays the die with a given number of spots, and returns a pseudorandom number between 1 and 6 to simulate a roll. The main function calls the die functions to simulate three rolls.
This document contains code for two C++ programs. The first program, betterinputFunc.cpp, defines a function called get_int_range that forces the user to enter an integer within a specified minimum and maximum range. The second program, betterDie.cpp, contains functions for simulating the roll of a six-sided die. It initializes randomness, displays the die with a given number of spots, and returns a pseudorandom number between 1 and 6 to simulate a roll. The main function calls the die functions to simulate three rolls.
This document contains code for two C++ programs. The first program, betterinputFunc.cpp, defines a function called get_int_range that forces the user to enter an integer within a specified minimum and maximum range. The second program, betterDie.cpp, contains functions for simulating the roll of a six-sided die. It initializes randomness, displays the die with a given number of spots, and returns a pseudorandom number between 1 and 6 to simulate a roll. The main function calls the die functions to simulate three rolls.
This document contains code for two C++ programs. The first program, betterinputFunc.cpp, defines a function called get_int_range that forces the user to enter an integer within a specified minimum and maximum range. The second program, betterDie.cpp, contains functions for simulating the roll of a six-sided die. It initializes randomness, displays the die with a given number of spots, and returns a pseudorandom number between 1 and 6 to simulate a roll. The main function calls the die functions to simulate three rolls.
Download as DOCX, PDF, TXT or read online from Scribd
Download as docx, pdf, or txt
You are on page 1of 2
BES 043: Computer Fundamentals and Programming Student Activity Sheet #36
Listing 9.12: betterinputFunc.cpp
#include <iostream.h> /* get_int_range(first, last) * Forces the user to enter an integer within a * specified range * first is either a minimum or max acceptable value * last is the corresponding other end of the range, * either a maximum or minimum * value * Returns an acceptable value from the user */ int get_int_range(int first, int // If the larger number is provided first, // switch the parameters if (first > last) { int temp = first; first = last; last = temp; } // Insist on values in the range first...last "Please enter a value in the range " << first << "..." << last << ": "; int in_value; // User input value bool bad_entry; do { cin >> in_value; bad_entry = (in_value < first || in_value > last); if (bad_entry) { cout<< in_value << in the range " << first << "..." << last << '\n' cout<< "Please try again: " } } while (bad_entry); // in_value at this point is guaranteed to // be within range return in_value; } // main // Tests the get_int_range function main() { cout<< get_int_range(10, 20) << '\n'; cout<< get_int_range(20, 10) << cout<< get_int_range(5, 5) << '\n' get_int_range(-100, 100) << '\n' }
Listing 9.13: betterDie.cpp
#include <cstdlib> #include <ctime> #include <iostream> using namespace std; /* initialize_die * Initializes the randomness of the die */ void initialize_die() { // Set the random seed value srand(static_cast<unsigned>(time(0))); } // show_die(spots) // Draws a picture of a die with number // of spots indicated // spots is the number of spots // on the top face void show_die(int spots) { cout << "+-------+\n"; switch (spots) { case 1: cout<< "| |\n"; cout<< "| * |\n"; cout<< "| |\n"; break; case 2: cout<< "| * |\n"; cout<< "| |\n"; cout<< "| * |\n"; break; case 3: cout<< "| * |\n"; cout<< "| * |\n"; cout<< "| * |\n"; break; case 4: cout<< "| * * |\n"; cout<< "| |\n"; cout<< "| * * |\n"; break; case 5: cout<< "| * * |\n"; cout<< "| * |\n"; cout<< "| * * |\n"; break; case 6: cout<< "| * * * |\n"; cout<< "| |\n"; cout<< "| * * * |\n"; break; default: cout<< " *** Error: \ illegal die value ***\n"; } cout<< "+-------+\n"; } // roll // Returns a pseudorandom // number in the range 1...6 int roll() { return rand() % 6 + 1; } // main Simulates the roll of // a die three times void main() { // Initialize the die initialize_die(); // Roll the die three times for (int i = 0; i < 3; i++) show_die(roll()); }