简体   繁体   中英

How do i tell my app “stop running” after set amount rounds?

So i'm making a turn based dice game that's modeled after this game called "underground chinchiro" which was taken from an anime called "Kaiju". I need to set a limit to my program so that it only runs for a set number of rounds, I'm only a beginner in coding so sorry for anything unusual you see in my code.

#include <iostream>
#include <string>
#include <time.h>
using namespace std;

void roll_3_dice(int &dice1, int &dice2, int &dice3) 
{
    dice1 = rand() % 6 + 1;
    dice2 = rand() % 6 + 1;
    dice3 = rand() % 6 + 1;
    return;
}


int main()
{
int cash = 90000;
int wager; 
int r;


//dealer's die
int dealer1;
int dealer2;
int dealer3;

// your die
int mdice1;
int mdice2;
int mdice3;


for (int i = 0; i < 10; i++)
{
cout << "Wager up boy!"<< endl;
cin >> wager;
while (wager < 100 || wager > 90000)
{
cout << "Minimum wager is 100; Maximum wager is 90000 ";
cin >> wager;
}

cout << "You wagered: " << wager << endl;
cout << "You have " << cash - wager << " remaining" << endl;
cash = cash - wager;

cout << endl;
cout << "Dealer will now roll the dice" << endl;

roll_3_dice(dealer1, dealer2, dealer3);


cout << "Dealer rolled the following: " << endl;
cout << dealer1 << "-" << dealer2 << "-" << dealer3 << endl;

cout << "It's your turn to roll the dice." << endl;
cout << endl; 
cout << "Press any key to roll the dice" << endl;
cin >> r;

roll_3_dice(mdice1, mdice2, mdice3);

cout << "You rolled the following: " << endl;
cout << mdice1 << "-" << mdice2 << "-" << mdice3 << endl;


system ("pause");
 }
}

Take a look at for loops. For loops will allow you to run your code for a set number iteration.

eg iterate over some code 7 times.

int number_of_iterations = 7;
for(int i = 0; i < number_of_iterations; i++) {
    // Your code that you would like to iterate over goes here.
 }

EDIT: As has been specified by the OP (in comments below), the issue is appears to be with the program not stopping to receive input from the user through each iteration of the for loop.

This could be for a number of reasons. My best guess would be that the stdin buffer is not clear and that std::cin continues to read in from the buffer. This could be solver by calling cin.clear() before reading in your input.

is time to learn how to use constants...

define one doing

const int max_round = 5;

and the do a while so long round is <= than max_round

Your problem is pretty unclear. Edit your code, find the section where problems occurring and paste that part only.

like:

while(cin>>wager)
{
   if(condition fails) 
   { 
       break; 
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM