# what to do now?

This topic is 3144 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts


#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int Diceroll();
void Introscreen();
void Tryagain();
void Win();
int Totalbears;
int Dicenumber;
int Totalcorrect;
string PLAYAGAIN;
string NAME;
bool PLAY = true;

int main()
{
srand((unsigned)time(NULL));

Introscreen();

string PLAYAGAIN = "yes";

while (PLAYAGAIN == "yes")

while(PLAY)
{

Totalcorrect = 0;

for (int iCOUNT=1; iCOUNT<11; iCOUNT++)
{

{
Win();
}

Diceroll();

cout<<"How many polar bears do you see around the ice holes?"<<endl;

{
cout<<"Correct! =D"<<endl;
Totalcorrect++;
}
else
{
cout<<"Incorrect! The right answer was: "<<Totalbears<<endl;
}
system("pause");

Tryagain();
}
}
return 0;
}

void LOSE (string NAME)

{
//the losing screen
cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"<< endl;
cout<<"!                                      !"<<endl;
cout<<"!        Sorry,                        !"<<endl;
cout<<"!                 You Lose!            !"<<endl;
cout<<"!                                      !"<<endl;
cout<<"!                                      !"<<endl;
cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"<<endl<<endl;
cout<<endl;

cout<<"SORRY "<<NAME<< " YOU LOST"<<endl<<endl;
cout<<"BETTER LUCK NEXT TIME"<<endl;
}
//the intoduction screen
void Introscreen()
{
cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"<<endl;
cout<<"!                                      !"<<endl;
cout<<"!                                      !"<<endl;
cout<<"!             Ice and Dice             !"<<endl;
cout<<"!                                      !"<<endl;
cout<<"!                                      !"<<endl;
cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"<<endl<<endl;
cout<<endl;

cout<<"Game details:"<<endl;
cout<<"The name is in the game, and the game is in the name."<<endl;
cout<<"and the name of the game is Polar Bears Around An Ice Hole."<<endl;
cout<<"Some people call this Petals Around A Rose."<<endl<<endl;
cout<<endl;
cin>>NAME;
}

int Diceroll()
{
Totalbears = 0;

for (int numRoll = 1; numRoll<4; numRoll++)
{
Dicenumber=1+rand()%6;

switch(Dicenumber)
{
case 1:
{
cout<<"*******"<<endl;
cout<<"*     *"<<endl;
cout<<"*  O  *"<<endl;
cout<<"*     *"<<endl;
cout<<"*******"<<endl;
Totalbears = Totalbears+0;
break;
}

case 2:
{
cout<<"********"<<endl;
cout<<"*    O *"<<endl;
cout<<"*      *"<<endl;
cout<<"* O    *"<<endl;
cout<<"********"<<endl;
Totalbears = Totalbears+0;
break;
}

case 3:
{
cout<<"*******"<<endl;
cout<<"*  O  *"<<endl;
cout<<"*  O  *"<<endl;
cout<<"*  O  *"<<endl;
cout<<"*******"<<endl;
Totalbears = Totalbears+2;
break;
}

case 4:
{
cout<<"*********"<<endl;
cout<<"*  O O  *"<<endl;
cout<<"*       *"<<endl;
cout<<"*  O O  *"<<endl;
cout<<"*********"<<endl;
Totalbears = Totalbears+0;
break;
}

case 5:
{
cout<<"*********"<<endl;
cout<<"* O   O *"<<endl;
cout<<"*   O   *"<<endl;
cout<<"* O   O *"<<endl;
cout<<"*********"<<endl;
Totalbears = Totalbears+4;
break;
}

case 6:
{
cout<<"********"<<endl;
cout<<"* O  O *"<<endl;
cout<<"* O  O *"<<endl;
cout<<"* O  O *"<<endl;
cout<<"********"<<endl;
Totalbears = Totalbears+0;
break;
}

default:
break;
}//end of switch
}
return 0;
}
void Win ()
{
cout<<"Congratulations, "<<NAME<<", You have won! =D"<<endl;
}
void Tryagain()
{
cout<<"Want to play again? (yes or no)"<<endl;
cin>>PLAYAGAIN;

if (PLAYAGAIN == "no")
{
PLAY = false;
cout<<"Okay, Thanks for playing. Bye"<<endl;
}
string PLAYAGAIN = "no";
{
while (PLAYAGAIN == "no")

system("PAUSE");
}
if (PLAYAGAIN == "yes")
{
PLAY = true;
cout<<"Okay!"<<endl;
}
}


[Edited by - Lorraine_B on May 2, 2009 2:38:13 PM]

##### Share on other sites
Experience tells that a person loses a lot of potential help by posting a big lump of unformatted code - You should use the [ source ][ /source ] tags which will colourise keywords and preserve code indents.

##### Share on other sites
the part that im sure is where i have to make changes is

im just like totaly stuck on where to put anything at, i have tried a bunch of things last night and everytime i changed something it just said i had a bunch of errors, so i need to be like taught where and why. nothing i have ever read said like you put this code under this line for this reason lol.

int main(){		srand((unsigned)time(NULL));		Introscreen();		string PLAYAGAIN = "yes";		while (PLAYAGAIN == "yes")		while(PLAY)		{				Totalcorrect = 0;				for (int iCOUNT=1; iCOUNT<11; iCOUNT++)				{						if (Useranswer==3)						{								Win();						}						Diceroll();						cout<<"How many polar bears do you see around the ice holes?"<<endl;						cin>>Useranswer;						if (Useranswer == Totalbears)						{ 								cout<<"Correct! =D"<<endl;								Totalcorrect++;						}						else						{								cout<<"Incorrect! The right answer was: "<<Totalbears<<endl;						}						system("pause");						Tryagain();				}		}		return 0;		}

[Edited by - Lorraine_B on May 2, 2009 2:01:06 PM]

##### Share on other sites
Quote:
 Original post by Lorraine_Bsee it makes it all scrabled up looking when i use the source and /source things
There are two reasons for that, one is that you use actual tabs for indents as opposed to, say, 4 spaces; the other is that if you post code in these forums without tags around then, annoyingly, things get regex'd into oblivion, if you do a fresh paste of your code back in between the tags then it'll fix that one [smile].

##### Share on other sites
Well its quite simple :), I've done all the hard work for you, now make sure you learn from it! If possible :) hehe:

#include <iostream>#include <string>#include <cstdlib>#include <ctime>using namespace std;int Diceroll();void Introscreen();void Tryagain();void LOSE(string name);void Win();		int Totalbears;		int Dicenumber;		int Useranswer;		int Totalcorrect = 0;		string PLAYAGAIN;		string NAME;		bool PLAY = true;int main(){		srand((unsigned)time(NULL));		Introscreen();		string PLAYAGAIN = "yes";		while (PLAYAGAIN == "yes")		while(PLAY)		{				for (int iCOUNT=1; iCOUNT<11; iCOUNT++)				{						Diceroll();						cout<<"How many polar bears do you see around the ice holes?"<<endl;						cin>>Useranswer;						if (Useranswer == Totalbears)						{ 								cout<<"Correct! =D"<<endl;								Totalcorrect++;						}						else						{								cout<<"Incorrect! The right answer was: "<<Totalbears<<endl;						}						system("pause");                        if(iCOUNT == 10)						{                         if(Totalcorrect == 3) Win();						 else LOSE(NAME);						 Totalcorrect = 0;						 Tryagain();						}						if(PLAY == false) return 0;				}		}		return 0;		}void LOSE (string NAME){		//the losing screen		cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"<< endl;		cout<<"!                                      !"<<endl;		cout<<"!        Sorry,                        !"<<endl;		cout<<"!                 You Lose!            !"<<endl;		cout<<"!                                      !"<<endl;		cout<<"!                                      !"<<endl;		cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"<<endl<<endl;		cout<<endl;		cout<<"SORRY "<<NAME<< " YOU LOST"<<endl<<endl;		cout<<"BETTER LUCK NEXT TIME"<<endl;}		//the intoduction screenvoid Introscreen(){		cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"<<endl;		cout<<"!                                      !"<<endl;		cout<<"!                                      !"<<endl;		cout<<"!             Ice and Dice             !"<<endl;		cout<<"!                                      !"<<endl;		cout<<"!                                      !"<<endl;		cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"<<endl<<endl;		cout<<endl;		cout<<"Game details:"<<endl;		cout<<"The name is in the game, and the game is in the name."<<endl;		cout<<"and the name of the game is Polar Bears Around An Ice Hole."<<endl;		cout<<"Some people call this Petals Around A Rose."<<endl<<endl;		cout<<endl;		cout<<"Wish to play? Please enter your name here:"<<endl;		cin>>NAME;}int Diceroll(){		Totalbears = 0;		for (int numRoll = 1; numRoll<4; numRoll++)		{				Dicenumber=1+rand()%6;				switch(Dicenumber)				{						case 1:						{								cout<<"*******"<<endl;								cout<<"*     *"<<endl;								cout<<"*  O  *"<<endl;								cout<<"*     *"<<endl;								cout<<"*******"<<endl;								Totalbears = Totalbears+0;								break;						}						case 2:						{								cout<<"********"<<endl;								cout<<"*    O *"<<endl;								cout<<"*      *"<<endl;								cout<<"* O    *"<<endl;								cout<<"********"<<endl;								Totalbears = Totalbears+0;								break;						}						case 3:						{								cout<<"*******"<<endl;								cout<<"*  O  *"<<endl;								cout<<"*  O  *"<<endl;								cout<<"*  O  *"<<endl;								cout<<"*******"<<endl;								Totalbears = Totalbears+2;								break;						}						case 4:						{								cout<<"*********"<<endl;								cout<<"*  O O  *"<<endl;								cout<<"*       *"<<endl;								cout<<"*  O O  *"<<endl;								cout<<"*********"<<endl;								Totalbears = Totalbears+0;								break;						}						case 5:						{								cout<<"*********"<<endl;								cout<<"* O   O *"<<endl;								cout<<"*   O   *"<<endl;								cout<<"* O   O *"<<endl;								cout<<"*********"<<endl;								Totalbears = Totalbears+4;								break;						}						case 6:						{								cout<<"********"<<endl;								cout<<"* O  O *"<<endl;								cout<<"* O  O *"<<endl;								cout<<"* O  O *"<<endl;								cout<<"********"<<endl;								Totalbears = Totalbears+0;								break;						}						default:						break;				}//end of switch		}		return 0;}void Win (){		cout<<"Congratulations, "<<NAME<<", You have won! =D"<<endl;}void Tryagain(){		cout<<"Want to play again? (yes or no)"<<endl;		cin>>PLAYAGAIN;		if (PLAYAGAIN == "no")		{		PLAY = false;		cout<<"Okay, Thanks for playing. Bye"<<endl;		}		if (PLAYAGAIN == "yes")		{		PLAY = true;		cout<<"Okay!"<<endl;		}}

I got very confused because there were many things wrong with the code and I couldn't wrap my head around it. Well it wasn't exactly wrong... but just should have not been done like that. I'm not sure if I got what you needed done but there it is.

:D.

##### Share on other sites
I don't think the game's logic is completely defined yet. If I answer correctly on the first three attempts do I win straight away or do I have to sit through 7 more questions? If I answer incorrectly 8 times, then I can't possibly win, do I have to answer the remaining questins before being told I've lost?

##### Share on other sites
Well I just did what Lorraine asked me to :). And that was to be able to quit if the user types "no" when asked whether or not to continue and to make sure that the user goes through 10 rounds and then be told whether he or she has won (which would be three correct answers), I didn't do anything else.

##### Share on other sites
Quote:
 Original post by VirtualProgrammerWell I just did what Lorraine asked me to :).
Yeah, I can see how you've interpreted the task based on your code. I'd love to make a complete and detailed post on how I would have written this program but, unfortunately, I'm really too swamped with work to even consider it without knowing exactly what Lorraine_B wants - and even then I may simple be too busy to get round to it. It is possible Zahlman will step in and do it, but looking at his recent posting history he might be otherwise preoccupied too; perhaps if I dare someone to...?

Not bad for two weeks though Lorraine_B [smile]

##### Share on other sites
Quote:
Original post by dmatter
Quote:
 Original post by VirtualProgrammerWell I just did what Lorraine asked me to :).
Yeah, I can see how you've interpreted the task based on your code. I'd love to make a complete and detailed post on how I would have written this program but, unfortunately, I'm really too swamped with work to even consider it without knowing exactly what Lorraine_B wants - and even then I may simple be too busy to get round to it. It is possible Zahlman will step in and do it, but looking at his recent posting history he might be otherwise preoccupied too; perhaps if I dare someone to...?

Not bad for two weeks though Lorraine_B [smile]

The only problem is that this program, from evidence in earlier posts, is homework, so we can't show how we would write it. I would have already done so if not for this fact.

##### Share on other sites
Quote:
 Original post by rip-offThe only problem is that this program, from evidence in earlier posts, is homework, so we can't show how we would write it. I would have already done so if not for this fact.
Ah, thanks for sharing that; it looks like you've been helping a lot with this already.

##### Share on other sites
Just to throw something helpful into this:
Quote:
 im just like totaly stuck on where to put anything at, i have tried a bunch of things last night and everytime i changed something it just said i had a bunch of errors, so i need to be like taught where and why. nothing i have ever read said like you put this code under this line for this reason lol.

Its a hard thing to teach, and to grasp. I will show you a way of writing your requirements in English, and after following a few transformations you will end with C++. After you practise this for a while, you will be able to do it in your head.

This is how you would do it from scratch. Of course, you've written most of your program already, but hopefully you will see how to apply some of the ideas to updating your program rather than writing a new one.

Quote:
 how ever, i need it to let them have 10 guesses , if they get 3 guesses correct then they win the game (shows win screen) then to ask if they like to play again. orr if they guess 10 times and they dont get 3 correct, it needs to show the loose screen, and then ask if they wana play again.

A good way to start writing the program is to find the "actions" that the user will do and model them as functions. So, here is a quick rewrite of the above, paying attention for specific action words and formalising the language used a bit:
The user has 10 attempts to guess().If they  guess() correctly 3 times then they win().If they guess() 10 times and they don't get 3 correct, they lose().Either way, they have the choice to playAgain().

I think it is a little easier now to see how to translate this into C++.
bool guess();void win();void lose();bool playAgain();int main(){    do    {        int correct = 0;                for(int i = 0 ; i < 10 ; i++)        {            if( guess() )            {                correct++;                if(correct == 3)                {                    // break means "stop looping now, jump to the closing brace"                    break;                }            }        }        // This is where the break statement will jump to.        if(correct == 3)        {            win();        }        else        {            lose();        }    }    while( playAgain() );}

I think that if you look at the C++ and compare it to the revised requirements in English, the two are pretty similar in structure.

This is just the "bare bones" of the application. It doesn't take into account things that were not explicity stated in the requirements. For example, it might be nice to inform the user when they guess correctly. There are also implementation details omitted, like calling srand() just at the start of main().

Notice that I haven't really specified the functions "win", "lose", "guess" and "playAgain" in any detail. We have an intuitive understanding of how they work based on their names. The only specifics included is that both "guess" and "playAgain" must return booleans because of the way they are to be used.

When we move to implementing these functions, we can do the same thing again. Write out in English what happens during a guess(), then transform it until it you can write it in C++. Obviously, some of these functions might turn out to be complex in their own right and should be broken into yet more functions. You can never have too many functions. [grin]

Quote:
 ALSO, how do i make the program close after they say no they dont want to play again?(after it says bye ofcourse)

The best way is to structure your code so that closing the program coincides with control flowing off the end of main().

However, you can also use the return keyword anywhere inside main() to do the same thing. The C++ standard specifies that main() returns an integer. It is convention to return 0 if everything went well, and to return a non-zero result if there was an error. So if the user chooses not to retry, you can write "return 0;" if you are in main(). However, if you look carefully you will see the skeleton code I presented above achieves the same thing without using the return keyword. The outer do...while loop will terminate when the user doesn't want to retry, leaving main().

##### Share on other sites
@Ripoff:

I posted a thread over a month ago asking how I could force myself into using the OOP paradigm and I think your post was probably the best explanation I've ever seen. Just wanted to mention that. +Rating for you.

##### Share on other sites

This topic is 3144 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628645
• Total Posts
2984015

• 9
• 9
• 10
• 21
• 20