Fishing Game

Good Afternoon everyone. I have been working on a fishing game that bases your caught fish off of a die that is being rolled. I know the code is not perfect. I know that there are other ways to do get the outcomes I am trying to achieve. That being said, I am struggling with the inner do loop to keep going until the user types either n or N when asked if they'd like to continue.

Here is the Dice.h file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef DICE_H
#define DICE_H

class Dice
{
private:
		int sides;
		int value;

public:
		Dice(int=6);
		void roll ();
		int getSides();
		int getValue();
};
#endif 


Here is the ccp file for dice.cpp

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
#include<cstdlib>
#include<ctime>
#include "Dice.h"


using namespace std;

Dice::Dice(int numSides)
{
	unsigned seed = time (0);
	srand(seed);
	sides = numSides;
	roll();
	

}

void Dice::roll()

{
	
	const int MIN_VALUE = 1;
	value = (rand() % (sides - MIN_VALUE +1 ))+MIN_VALUE;

}

int Dice::getSides()
{
return sides;
}

int Dice::getValue()
	
{
return value;
}


and finally here is my source code cpp

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
#include<iostream>
#include"Dice.h"
#include<string>


using namespace std;

int main()

{
	const int DIE1_SIDES = 6;
	Dice die1(DIE1_SIDES);
	int huge=20, old=-7, tiny=5, normal=10, mud=-1, toilet=-20;
	int hugeT=0, oldT=0, tinyT=0, normalT=0, mudT=0, toiletT=0, round=0;
	double total=0;
	char choice;
	string fish, Tround;
	

	cout<<"*****************************************************************************\n";
	cout<<"*                         FISHING GAME SIMULATOR                            *\n";
	cout<<"*****************************************************************************\n";
	cout<<"*            This game simulates fishing and uses a random number           *\n";
	cout<<"*             between 1 and 6 to determine what type of fish you            *\n";
	cout<<"*           have have hooked. It will also display what you caught          *\n";
	cout<<"*          and the points awarded when you decide to leave the game.        *\n";
	cout<<"*                         Have fun and good luck!                           *\n";
	cout<<"*****************************************************************************\n";

	system("pause");
	system("cls");

	cout<<"\t\t Would you like to play the fishing game?\n";
	cout<<"\t\t   Please enter Y for YES or N for NO :";
	cin>>choice;




	do
	{
		die1.roll();

		if (die1.getValue()==1)
		{
			fish = "HUGE FISH";
			total += huge;
			round = huge;
			hugeT++;
		}
			if (die1.getValue()==2)
		{
				fish = "Old Shoe";
				total +=old;
				round = old;
				oldT++;
		}
			if (die1.getValue()==3)
		{
				fish = "Tiny Fish";
				total +=tiny;
				round = tiny;
				tinyT++;
		}
			if (die1.getValue()==4)
		{
				fish = "Normal Sized Fish";
				total +=normal;
				round = normal;
				normalT++;
		}
			if (die1.getValue()==5)
		{
				fish = "Mud Puppy";
				total +=mud;
				round = mud;
				mudT++;
		}
			if (die1.getValue()==6)
		{
				fish = "Toilet Seat";
				total += toilet;
				round = toilet;								
				toiletT++;

		}


		cout<<"\n";
		cout<<"\tThe die landed on "<<die1.getValue()<< " resulting in you catching a "<<fish<<endl;
		cout<<"\t            Your "<<fish<<" is worth "<<round<<" points\n";
		cout<<"\t               Your total score so far is "<<total<<endl;
		cout<<"\n";
		cout<<"\t\t   Would you like to continue playing?\n";
		cout<<"\t\t   Please enter Y for YES or N for NO :";
		cin>>choice;
	}

	while (choice !='n' || choice !='N');
	
	 {
		 if ( choice == 'n' || choice =='N')
		 {
			cout<<"*******************************************************************************\n";
			cout<<"*                        FISHING GAME SCORE TALLY                             *\n";
			cout<<"*******************************************************************************\n";
			cout<< "          You caught a HUGE FISH "<<hugeT<<" times equaling : "<<(hugeT*20)<<" points\n" ;
			cout<< "          You caught a Old Shoe "<<oldT<<" times equaling : "<<(oldT*-7)<<" points\n" ;
			cout<< "          You caught a Tiny Fish "<<tinyT<<" times equaling : "<<(tinyT*5)<<" points\n" ;
			cout<< "          You caught a Normal Fish "<<normalT<<" times equaling : "<<(normalT*10)<<" points\n" ;
			cout<< "          You caught a Mud Puppy "<<mudT<<" times equaling : "<<(mudT*-1)<<" points\n" ;
			cout<< "          You caught a Toilet Seat "<<toiletT<<" times equaling : "<<(toiletT*-20)<<" points\n" ;
			cout<<"*******************************************************************************\n";
			cout<< "                     Your total score is :"<<total<<"\n";
			cout<<"*******************************************************************************\n";
			cout<<"*******************************************************************************\n";
		 }
	system("pause"); 
	return 0;
	 }	
}
Hello, I believe that your error is on line 99 of your main source file. All you need to do is to change || to &&. As far as I can see, that's your only problem with the do loop.

Tom
Line 99: You want && here, not ||.

Line 101,120: These braces are superfulous.

Line 102: This if statement is unnecessary. You should only get here is choice was 'n' or "N'.

dice.cpp line 11: srand() does not belong here. srand() should only be called ONCE in a program, otherwise it resets the RNG to generate the same sequence. The call to srand() should be the first executable statement in main(). Granted you're only creating a single instance of Dice currently, but if you were to create a second instance of Dice, you would have undesired behavior.

AWESOME!! Thanks for the replies! One more question.. If I wanted some type of validation like a while loop that makes them input y or Y or n or N at the beginning where could I throw that? Just before the do?
You would want a while loop around lines 34 and 35.

Your do loop at line 40 is problematic. If the user enters 'n' or an invalid choice at line 35, you continue into the game anyway.
Topic archived. No new replies allowed.