Skip to main content
GameDev.net gamedev.net
🔒 Locked

game loop (C++)

Started by shadowmark May 22, 2005 at 10:09 AM 21 replies 2.7k views
Original Post
shadowmark
shadowmark
Hi, Ive created this program where it asks you what kind of dice you would like to roll. their a two options a 6 sided dice or a 20 sided dice. Once it rolls it then asks if you want to roll again. (y/n). I have this statement here: if (again == 'y') cout << "You want to roll again.\n"; break; and my question is when they select to roll again how do i get it to do the whole program again starting with which dice you would like to roll?
rycon
rycon
You would need to place the program in a loop. Something like this

do
{


Program here


Roll again?

}
while(again == 'y');
mike25025
mike25025
These are the two ways that I can think of.
int main(){beginning:	// Some code here.	if(again == 'y'){		goto beginning;	}	return 0;}// Orint main(){	do{		// Some code here.	}	while(again == 'y');	return 0;}
shadowmark
shadowmark
what do you mean by "Program here"

do you mean like the name that i saved the program as?
DeviceLost
DeviceLost
Quote:
Original post by shadowmark
what do you mean by "Program here"

do you mean like the name that i saved the program as?


i guess he means the program source code. the part where the "dice is rolled".

i would advice not to use GoTo´s.


greets DeviceLost
shadowmark
shadowmark
I still don't see what you mean. here's my code.

// Which Die Roll

#include
#include
#include
#include

using namespace std;

int main()
{
cout << "Dice choices\n\n";
cout << "1 - d6\n";
cout << "2 - d20\n\n";

int choice;
cout << "Choice: ";
cin >> choice;

switch (choice)
{
case 1:
cout << "You picked a d6.\n";
{
srand(time(0));

int randomRoll = rand();

int die = (randomRoll % 6) + 1;
cout << "You rolled a " << die << endl;
}

break;
case 2:
cout << "you picked a d20.\n";
{
srand(time(0));

int randomRoll = rand();

int d20 = (randomRoll % 20) + 1;
cout << "You rolled " << d20 << endl;
}

break;
default:
cout << "You made an illegal choice.\n";
}

char again = 'y';
while (again == 'y')
{
cout << "\nGood roll, do you want to roll again? (y/n): ";
cin >> again;

while (again == 'y')
cout << "You want to roll again.\n ";
break;

}
if (again != 'y')
cout << "\nOkay, bye.";

getch();
return 0;
}

sry it takes up so much space, i don't kno how to put it in one of those scrolling menus
DeviceLost
DeviceLost
first, using code tags is described here:
http://www.gamedev.net/community/forums/faq.asp

you could solve the problem like this:

// Which Die Roll#include <iostream>#include <cstdlib>#include <ctime>#include <conio.h>using namespace std;void Roll6SidedDice(){srand(time(0));int randomRoll = rand();int die = (randomRoll % 6) + 1;cout << "You rolled a " << die << endl;}void Roll20SidedDice(){srand(time(0));int randomRoll = rand();int d20 = (randomRoll % 20) + 1;cout << "You rolled " << d20 << endl;}int main(){cout << "Dice choices\n\n";cout << "1 - d6\n";cout << "2 - d20\n\n";int choice;cout << "Choice: ";cin >> choice;switch (choice){case 1:cout << "You picked a d6.\n";Roll6SidedDice();}break;case 2:{cout << "you picked a d20.\n";Roll20SidedDice();}break;default:cout << "You made an illegal choice.\n";}char again;bool roll_again =true;while(roll_again){cout << "\nGood roll, do you want to roll again? (y/n): ";cin >> again;if(again == 'y')  {   Roll6SidedDice(); //....  }else{cout << "\nOkay, bye.";roll_again = false;}}getch();return 0;}



i havent tested the code but i guess you to solve the problem easily by now


greets DeviceLost
rycon
rycon
Sorry my first post was a bit unclear. Try this

// Which Die Roll#include <iostream>#include <cstdlib>#include <ctime>#include <conio.h>using namespace std;int main(){// Start Loop Heredo{   cout << "Dice choices\n\n";   cout << "1 - d6\n";   cout << "2 - d20\n\n";   int choice;   cout << "Choice: ";   cin >> choice;   switch (choice)   {   case 1:   cout << "You picked a d6.\n";   {   srand(time(0));   int randomRoll = rand();   int die = (randomRoll % 6) + 1;   cout << "You rolled a " << die << endl;   }   break;   case 2:   cout << "you picked a d20.\n";   {   srand(time(0));   int randomRoll = rand();   int d20 = (randomRoll % 20) + 1;   cout << "You rolled " << d20 << endl;   }   break;   default:   cout << "You made an illegal choice.\n";   }//  Ask if you want to roll again   char again;   cout << "\nGood roll, do you want to roll again? (y/n): ";   cin >> again;}while (again == 'y');  // End the loop here.  // Loop will continue as long as again is equal to 'y'.// Exited loop so we quit.cout << "\nOkay, bye.";return 0;}
DeviceLost
DeviceLost
rycons code is even better as the choice between 6 sided or 20 sided is also repeated. use his version!



greets DeviceLost
Meagermanx
Meagermanx
I think you should effectively learn to use GoTos. Not only do they make your program neater, and easier to read, but they also help organize your code in a logical fashion.

---There are 2 kinds of people: those who know hexadecimal, and those who don't.
shadowmark
shadowmark
sry um just really new at this I havn't gotton far enough in my begginer programming book to use gotos I don't even think im far enough to even be making this game loop. im only on chapter 2. lol.
DeviceLost
DeviceLost
Quote:
Original post by shadowmark
sry um just really new at this I havn't gotton far enough in my begginer programming book to use gotos I don't even think im far enough to even be making this game loop. im only on chapter 2. lol.


keep on coding man!
Stompy9999
Stompy9999
Quote:
Original post by Meagermanx
I think you should effectively learn to use GoTos. Not only do they make your program neater, and easier to read, but they also help organize your code in a logical fashion.



You realize if you wouldn't have put at the end of that you would have been hit by a nuke, right?

Crawl
Crawl
btw, srand() only needs to be called once otherwise the rand() results will become predictable (sort of :P you won't notice it though in this particular situation). In any case calling it more than once is redundant.
--------
Allmight
Allmight
Quote:
Original post by shadowmark
sry um just really new at this I havn't gotton far enough in my begginer programming book to use gotos I don't even think im far enough to even be making this game loop. im only on chapter 2. lol.


Meagermanx was joking there. What he realy ment and most coders agree on, never, ever use goto's unless you really have a good reason to. And even then you should try to find a better way. There are times a goto could be accepted, but those are very rare. So make a habit of not using them and try to find other ways to solve the problem instead.

// Allmight.
-------------------------------------------------Founder and DirectorAllSoft Studios
ProPuke
ProPuke
Erm Shadowmark I do hope you realise he was joking (I nearly had a heartattack @ first, before I noticed)

It's just abit of coder sarcasm, read the sentence asif it were in reverse

The use of goto is strictly taboo

It breaks structure

edit: lol look @ the way everyone paniced @ that joke
____ _ ___ ___ __ _`By offloading cognitive load to the computer, programmers are able to design more elegant systems' - Unununium OS regarding Python
shadowmark
shadowmark
oh. I also have another question. this theirs something wrong with the while part and i don't kno what it is. and help would be greatly appreciated again.

char again = 'y';

cout << "\nGood roll, do you want to roll again? (y/n): ";
cin >> again;
}
while (again == 'y'); // End the loop here.
shadowmark
shadowmark
oh. I also have another question. this theirs something wrong with the while part and i don't kno what it is. and help would be greatly appreciated again.

char again = 'y';

cout << "\nGood roll, do you want to roll again? (y/n): ";
cin >> again;
}
while (again == 'y'); // End the loop here.

oops i posted twice.
UltimaX
UltimaX
Quote:
Original post by mike25025
These are the two ways that I can think of.
*** Source Snippet Removed ***


Please do not ever advise a goto statement. Shadowmark, rycon suggested a really good solution so please avoid the spaghetti code and forget goto was even mentioned. Where I work now the original system was designed with nothing but spaghetti code and to do even an update on one of the base programs is ridiculous.

Best of luck
UltimaX
UltimaX
Quote:
Original post by shadowmark
oh. I also have another question. this theirs something wrong with the while part and i don't kno what it is. and help would be greatly appreciated again.

char again = 'y';

cout << "\nGood roll, do you want to roll again? (y/n): ";
cin >> again;
}
while (again == 'y'); // End the loop here.


Are you getting the error: error C2065: 'again' : undeclared identifier

Do this:
int main(){char again = 'y'; //--Decalre again before the loop                  //--(Be sure to take "char again = 'y';" out of the loop)// Start Loop Heredo{   cout << "Dice choices\n\n";   cout << "1 - d6\n";   cout << "2 - d20\n\n";   int choice;   cout << "Choice: ";   cin >> choice;   switch (choice)   {   case 1:   cout << "You picked a d6.\n";   {   srand(time(0));   int randomRoll = rand();   int die = (randomRoll % 6) + 1;   cout << "You rolled a " << die << endl;   }   break;   case 2:   cout << "you picked a d20.\n";   {   srand(time(0));   int randomRoll = rand();   int d20 = (randomRoll % 20) + 1;   cout << "You rolled " << d20 << endl;   }   break;   default:   cout << "You made an illegal choice.\n";   }//  Ask if you want to roll again   cout << "\nGood roll, do you want to roll again? (y/n): ";   cin >> again;}while (again == 'y');  // End the loop here.  // Loop will continue as long as again is equal to 'y'.// Exited loop so we quit.cout << "\nOkay, bye.";return 0;}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.