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
#include<iostream>
#include"Dice.h"
#include<string>
usingnamespace std;
int main()
{
constint 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.
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?