//Program Name: mystuff.h //Description: all of our cool function prototypes. //Author: Mr. Weeks //Date: September 31, 2009 #ifndef MYSTUFF_H #define MYSTUFF_H using std::string; int random(int low, int high); string reverse(string str); #endif |
//Program Name: mystuff.cpp //Description: all of our cool functions defined. //Author: Mr. Weeks //Date: September 31, 2009 #ifndef MYSTUFF_H #define MYSTUFF_H #include <iostream> #include <math.h> #include "mystuff.h" using namespace std; int random(int low, int high) { //return a random int between // low and high inclusive return rand() % (high - low + 1) + low; } string reverse(string str) { //reverse the order of the characters in a string string temp; for (int ii = 0 ; ii < str.length() ; ++ii) { temp = str[ii] + temp; } return temp; } #endif |
//Program Name: guess.cpp //Description: Number guessing game. //Author: Mr. Weeks //Date: September 31, 2009 #include <iostream> #include "mystuff.h" using namespace std; int main(void) { int count = 0; srand(time(0)); //have the computer pick a random # from 1 to 100 int magic_number=random(1,100); cout << endl; cout << "GUESS A NUMBER (1-100)" << endl; int guess = -1000; while (guess != magic_number) { cout << "Enter a guess: " << endl; cin >> guess; ++count; if (guess < magic_number) cout << "Too small!" << endl; if (guess > magic_number) cout << "Too big!" << endl; if (guess == magic_number) cout << { "You got it in " << count << " guesses." << endl; } } return 0; } |
We will add prototypes to mystuff.h and the actual functions to mystuff.cpp as we go along. For now we will use this layout to make our number guessing game. To compile, you will need to do the following: