1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <cctype>
#include <fstream>
#include <string>
using namespace std;
void playGame (int random);
void getGuess(int guessArray[], int numElements, int& guess, int attemptNumber);
int searchList(const int list[], int numElems, int value);
bool playAgain();
void selectionSort(int array[], int size);
void writeOutput(int numElems, int outputArray[]);
bool openOutputFile(ofstream &outfile);
void idFunction(int hwNum);
int main ()
{
const int MAX = 100; // max value of random generated number
const int MIN = 1; // min number of random generated number
int random; // the randomly generated number the user is trying to guess
int gameNumber = 0; // the number of games the user has played so far
int answerArray[MAX]; // the array holding the multiple randomly generated numbers
idFunction(6);
srand(time(0));
do{
random = ((rand() % (MAX-MIN+1)) + MIN);
answerArray[gameNumber] = random;
gameNumber++;
playGame(random);
}while (playAgain() == true && gameNumber < 50);
if (gameNumber > 49)
cout << "You can play a maximum of 50 games." <<endl;
cout << "Thank You for playing!" << endl << endl;
selectionSort(answerArray, gameNumber);
return 0;
}
void playGame (int random)
{
int attemptNumber = 0; const int maxAttempts = 19; int guessArray[maxAttempts]; int guess;
do
{
getGuess (guessArray, attemptNumber, guess, attemptNumber);
if (guessArray[attemptNumber] < random)
cout << "Your number is too low." << endl;
else if (guessArray[attemptNumber] > random)
cout << "Your number is too high." << endl;
else
{cout << "Congratulations, you got the number!" << endl;
cout << "You guessed " << random << " in " << attemptNumber + 1 << " tries" << endl;
}
++attemptNumber;
}while(attemptNumber <= maxAttempts && guess != random);
if (attemptNumber > maxAttempts)
cout << "Sorry, you ran out of tries. The number was " << random << " ." << endl;
}
void getGuess(int guessArray[], int numElements, int& guess, int attemptNumber)
{
bool invalidGuess; // this is true if the user entered a guess out of the range
do
{
invalidGuess = false;
cout << "Please guess a number from 1 to 100. " << endl;
cin >> guess;
if (guess < 1 || guess > 100)
{
cout << "That guess is out of range. " << endl;
invalidGuess = true;
}
if (searchList(guessArray, numElements, guess) != -1)
{
cout << "You already guessed that number." << endl;
invalidGuess = true;
}
}while (invalidGuess == true);
guessArray[attemptNumber] = guess;
}
int searchList(const int list[], int numElems, int value)
{
int index = 0; // Used as a subscript to search array
int position = -1; // To record position of search value
bool found = false; // Flag to indicate if the value was found
while (index < numElems && !found)
{
if (list[index] == value) // If the value is found
{
found = true; // Set the flag
position = index; // Record the value's subscript
}
index++; // Go to the next element
}
return position;
// Return the position, or -1
}
bool playAgain()
{
char wantsToRepeat; // keeps track of whether or not the user wants to play again
bool invalidInput; // this is true if the user enters a value other than y or n (yes or no)
do
{
cout << endl << endl;
cout << "Do you want to play another game? Enter y for yes or n for no." << endl;
cin >> wantsToRepeat;
wantsToRepeat = tolower(wantsToRepeat);
invalidInput = false;
cout << endl << endl;
if (wantsToRepeat == 'y')
return true;
else if (wantsToRepeat == 'n')
return false;
else
{
invalidInput = true;
cout << "Please enter y for yes or n for no." << endl;
}
}while (invalidInput == true);
}
void selectionSort(int array[], int size)
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = array[startScan];
for(int index = startScan + 1; index < size; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
writeOutput(size, array);
}
void writeOutput(int numElems, int outputArray[])
{
ofstream outputFile; // The random numbers are listed in numerical order in an outside file
openOutputFile(outputFile);
if (openOutputFile(outputFile) == true)
{
outputFile << "Your randomly generated numbers in \n"
<< " numerical order are as follow: " << endl;
}
for (int i=0; i < numElems ; i++)
{
outputFile<< outputArray[i] << " ";
if ((i+1)%6==0)
outputFile << endl;
}
}
bool openOutputFile(ofstream &outfile)
{
string filename; // The name of the file the user wants to send the random numbers to
cout<<"Enter output filename: " ;
getline(cin, filename);
outfile.open(filename.c_str());
if(outfile.fail()){
cout<<"Unable to open output file\n";
return false;
}
return true;
} // end openOutputFile
void idFunction(int hwNum)
{
cout<<"CIS 22A Programming Homework # 6" << hwNum << endl;
cout <<"My name"<< endl << endl;
}
|