🔒 Locked
game loop (C++)
Advertisement
Original Post
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?
what do you mean by "Program here"
do you mean like the name that i saved the program as?
do you mean like the name that i saved the program as?
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
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
// 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
first, using code tags is described here:
http://www.gamedev.net/community/forums/faq.asp
you could solve the problem like this:
i havent tested the code but i guess you to solve the problem easily by now
greets DeviceLost
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
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;}
rycons code is even better as the choice between 6 sided or 20 sided is also repeated. use his version!
greets DeviceLost
greets DeviceLost
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.
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.
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!
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?
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.
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
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
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.
char again = 'y';
cout << "\nGood roll, do you want to roll again? (y/n): ";
cin >> again;
}
while (again == 'y'); // End the loop here.
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.
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.
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
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.
Advertisement