Hi there, I have two programs,
Hi there, I have two programs, one I cant figure out the problem in the program so it wont run. The second program
I have, the command window wont stay up long enough to get a screen shot.
Can you help me?
FIRST PROBLEM
// Week Assignment-
// Description:
//———————————-
//**begin #include files************
#include <iostream> // provides access to cin and cout
#include <time.h> // provides access to time() for srand()
//–end of #include files———–
//———————————-
using namespace std;
//———————————-
//**begin function prototypes*******
int func1(int[]);
bool func2();
string func3(int anInt);
//–end of function prototypes——
//———————————-
//**begin global constants**********
const int ArraySize = 10;
//–end of global constants———
//———————————-
//**begin main program**************
int main() {
// seed random number generator
srand(time(NULL));
// create and initialize variables
string myString;
int myInt;
int myIntArray[ArraySize];
// initialize the array with random numbers 1-100
for (int i = 0; i < ArraySize; i++) {
myInt = (rand() % 100) + 1;
myIntArray[i] = i;
}
// calling the functions
for (int i = 0; i < 4; i++) {
myInt = func1(myIntArray);
cout << “Value returned by func1 was ” << myInt << endl;
}
cout << “Flipping a coin. The result is ” << (func2() ? “Heads.” : “Tails.”)
<< endl;
for (int i = 0; i < 4; i++) {
int ranNum = rand() % 3;
cout << “The number ” << ranNum << ” is ” << func3(ranNum) << endl;
}
// Wait for user input to close program when debugging.
cin.get();
return 0;
}
//–end of main program————-
//———————————-
//**begin function definitions******
int func1(int myArray[]) {
return myArray[(int) (rand() % ArraySize)];
}
bool func2() {
return rand() % 2 ? true : false;
}
string func3(int anInt) {
switch (anInt) {
case 0:
return “ZERO.”;
break;
case 1:
return “ONE.”;
break;
case 2:
return “TWO.”;
break;
default:
break;
}
return “Not Found.”;
}
//–end of function definitions——
//———————————-
SECOND PROBLEM
// Description: Horserace – version 2
//———————————-
//**begin #include files************
#include <iostream> // provides access to cin and cout
#include <array> // provides access to std::array
#include <time.h> // provides access to time()
#include <string> // provides access to getline()
#include <iomanip>
//–end of #include files———–
//———————————-
using namespace std;
//———————————-
//**begin global constants**********
// struct for horse (name, distance, eventOffset, ID)
struct Horse {
string name;
int distance;
int offset;
int ID;
};
const int horseCount = 6;
const int trackLength = 100;
//–end of global constants———
//———————————-
//**begin function prototypes*******
// get horse name
void getName(Horse &);
// Racing events
int checkEvents(Horse &);
// Update positions
int updatePos(Horse &);
// Report positions
void reportPos(array<Horse, horseCount>);
// Clear racing data
void clearData(array<Horse, horseCount> &);
//–end of function prototypes——
//———————————-
//**begin main program**************
int main() {
// seed random number generator
srand(time(NULL));
// CREATE AND INITIALIZE VARIABLES
std::array<Horse, horseCount> Horses;
int die1, die2;
// using a range-based for loop, initialize data for each horse
for (Horse &h : Horses) {
getName(h);
h.distance = 0;
h.offset = 0;
h.ID += 1;
}
// have user name horses
// clear distance and offset.
// create ID. (you will probably want to create a index variable external to the loop to do this)
// number of winning horse (init to 0)
int winner = 0;
// distance of leading horse (init to 0)
int lead = 0;
// bet
// cash on hand (init to 100–or whatever value you chose)
int bet;
int cash = 100;
// flag to indicate the race is active in race do-while loop (init to true)
bool racing = true;
// flag to indicate the game is active in game do-while loop (init to true)
bool playing = true;
// horse number bet on
int hNum;
// START OF GAME LOOP
while (!playing) {
// ask for bet amount and get the number of the horse being bet on
cout << “You have $” << cash << “. How much do you want to bet? “;
cin >> bet;
if (bet <= 0) {
playing = false;
}
else {
cout << “Which horse number (from 1-6) do you want to bet on? “;
cin >> hNum;
cout << endl;
racing = true;
bet = (cash – bet);
cin.sync();
}
// if the bet is negative or $0, quit.
while (racing) {
// START OF RACE LOOP
// for each horse–use range-based for
for (Horse &h : Horses) {
h.distance += checkEvents(h);
h.distance += updatePos(h);
}
// check for events using a function and return horse’s offset
// roll dice and, using offset, adjust horse’s distance using a function
// check for race over
if (Horses[0].distance < trackLength
&& Horses[1].distance < trackLength
&& Horses[2].distance < trackLength
&& Horses[3].distance < trackLength
&& Horses[4].distance < trackLength
&& Horses[5].distance < trackLength) {
racing = true;
}
else {
racing = false;
}
// check if this is leading horse and change lead and winner values if it is
reportPos(Horses);
}
for (Horse &h : Horses) {
if (h.distance > lead) {
lead = h.distance;
}
}
cout << “The winning distance was ” << lead << ” units!” << endl;
int index = 0;
for (Horse &h : Horses) {
if (lead == h.distance) {
break;
}
index++;
}
if (hNum == winner) {
cout << “Your horse won!” << endl;
cash = (cash + (bet * 6));
racing = false;
}
else {
cout << “Your horse didn’t win!” << endl;
racing = false;
}
cin.get();
if (cash <= 0) {
cout << “Uh-oh! You ran out of money!” << endl;
system(“pause”);
playing = false;
}
clearData(Horses);
// END OF GAME LOOP
}
// Wait for user input to close program when debugging.
cin.get();
return 0;
system(“pause”);
}
//–end of main program————-
//———————————-
//**begin function definitions******
// Get horse names
void getName(Horse &Nag) {
// ask for horse’s name
cout << “Enter a horse’s name: “;
cin >> Nag.name;
}
// Racing events
int checkEvents(Horse &Nag) {
int die1 = rand() % 6 + 1;
int die2 = rand() % 6 + 1;
int sum; // sum of dice roll
sum = (die1 + die2);
// roll one 16 sided die to check on event (see instructions for event)
// use switch statement to handle event
// case 0, 1: horse breaks stride lose 1-2
// case 2, 3: horse find its stride gain 1-2
// case 4: horse stumbles lose 4-6
// case 5: horse has energy burst gain 4-6
switch (sum) {
case 0 || 1:
sum = (sum – ((rand() % 2) + 1));
cout << “—-” << Nag.name << ” breaks stride!” << endl;
break;
case 2:
sum = (sum + ((rand() % 2) + 1));
cout << “—-” << Nag.name << ” found stride!” << endl;
break;
case 3:
sum = (sum + ((rand() % 2) + 1));
cout << “—-” << Nag.name << ” found stride!” << endl;
break;
case 4:
sum = (sum – ((rand() % 3) + 4));
cout << “—-” << Nag.name << ” stumbles” << endl;
break;
case 5:
sum = (sum + ((rand() % 3) + 4));
cout << “—-” << Nag.name << ” gained a burst of speed!” << endl;
break;
default:
cout << “No changes in the offset for ” << Nag.name << endl;
sum = sum + 0;
break;
}
return sum;
}
// Update positions
int updatePos(Horse &Nag) {
// simulate rolling two dice using rand()%6+1
// sum up the total of the two dice
// add the horse’s offset to the sum
// if the sum is negative make it the new offset
// else add the sum to the distance and clear offset
// return the distance
int die1 = rand() % 6 + 1;
int die2 = rand() % 6 + 1;
int sum; // sum of dice roll
sum = (die1 + die2);
sum += Nag.offset;
if (sum < 0) {
Nag.offset = sum;
}
else {
Nag.distance += sum;
Nag.offset = 0;
}
return Nag.distance;
}
// Report positions
void reportPos(array<Horse, horseCount> HArray) {
// use range-based for loop
// print horse position
for (Horse &h : HArray) {
cout << h.name << ” at marker ” << h.distance << “.” << endl;
}
}
// Clear racing data
void clearData(array<Horse, horseCount> &HArray) {
// use range-base for loop
// clear distance
// clear offset
for (Horse &h : HArray) {
h.offset = 0;
h.distance = 0;
}
}
//–end of function definitions——
//———————————-
Both of these are in C++ in microsoft visual studios