简体   繁体   中英

How can I do to keep the value of a variable after a do while loop?

I have been coding a program to simulate a roulette of a casino, thing is that every time I try to repeat the game after is finished I want the game to keep going and the money to be the same, so if you have lost money you start with that certain money, here is the code (It's in Spanish but I think it's pretty clear):

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

using namespace std;
int num, pri, randum, num2, op, num3 = 10000, col = randum, rep, clear;
int main() {
    do {
        int num4 = op;
        cout << "Escoja la opción de la que apostar.\n";
        cout << "1 - Apostar a un número. \n2 - Apostar a un color \n";
        cout << "Elija opción: ";
        cin >> pri;
        cout << " \n";
        cout << " \n";

        switch (pri) {
        case 1: {
            srand(time(0));
            randum = rand() % 37 + 1; //si poner 37 + 1 te va cojer números hasta el 37 no?
            if (num4 != 10000) {
                cout << "Su saldo actual es " << num3 << " €\n";
            } else {
                cout << "Su saldo actual es 10000 €\n";
            }
            cout << "Ha elegido apostar a un número\n";
            cout << "Introduzca el dinero que quiere apostar -->\n";
            cin >> num;
            cout << "Ahora introduzca el número que desee entre el 0 y 36 -->\n";
            cin >> num2;

            if (num2 == randum) {
                op = num3 + num;
                cout << "\n¡Enhorabuena! Has ganado! Ahora tienes " << op << " €\n";
            } else {
                op = num3 - num;
                cout << "\nLo sentimos... Has perdido la apuesta, ahora tienes " << op << " €\n";
                cout << "¿Quieres volver a jugar?\n- Sí -> 1\n- No -> 2\n";
                cin >> clear;
                if (clear == 1) {} else if (clear == 2) {
                    cout << "Bien, suerte en la próxima tirada.\n\n";
                }
            }
            break;
        }
        case 2: {
            if (num3 == 10000) {
                cout << "Su saldo actual es 10000 €\n";

            } else {
                cout << "Su saldo actual es " << num3 << " €\n";
            }
            cout << "Ha elegido apostar a un color\n";
            cout << "Introduzca el dinero que quiere apostar -->\n";
            cin >> num;
            srand(time(0));
            randum = rand() % 2 + 1;
            cout << "Ahora escoja rojo (1) o negro (2) -->\n";
            cin >> col;
            if (col == randum) {
                op = num3 + num;
                cout << "\n¡Enhorabuena! Has ganado! Ahora tienes " << op << " €";
            } else {
                op = num3 - num;
                cout << "\nLo sentimos... Has perdido la apuesta, ahora tienes " << op << " €";

            }
            cout << "¿Quieres volver a jugar?\n- Sí -> 1\n- No -> 2\n";
            cin >> clear;

            if (clear == 1) {} else if (clear == 2) {
                cout << "Bien, suerte en la próxima tirada.\n\n";
            }
        }
        }
    } while (clear == 1);

    return 0;
}

I suggest you store the money into a file
Like this:

 #include <fstream>
    
 ofstream myfile ("money.txt");
  if (myfile.is_open())
  {
    myfile << "put the money in the bag here";
    myfile.close();
  }
  else cout << "Unable to open file";

And whenever you want to read the value
Use this:

  string line;
  ifstream myfile ("money.txt");
  if (myfile.is_open())
  {
      getline (myfile,line);
      cout << line << '\n';
      myfile.close();
  }

  else cout << "Unable to open file"; 

So, it should be pretty easy to do that.

Initialize the starting amount outside the loop before the betting begins.

At the end of the loop, ask if user wants to bet more.

Would that work for you? Or do you need it to be initialized when you start the code itself? You could use static

I am just changing a few things from your code:

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

using namespace std;

int main()
{
    int money = 10000, bet_amount = 0, clear, pri;
    cout << "Su saldo inicial es " << money << " €\n";

    do
    {
        cout << "Escoja la opción de la que apostar.\n";
        cout << "1 - Apostar a un número. \n2 - Apostar a un color \n";
        cout << "Elija opción: ";
        cin >> pri;
        cout << " \n";
        cout << " \n";

        cout << "Introduzca el dinero que quiere apostar -->\n";
        cin >> bet_amount;

        switch (pri)
        {
        case 1:
        {
            int number_chosen = -1, randum;
            cout << "Ahora introduzca el número que desee entre el 0 y 36 -->\n";
            cin >> number_chosen;

            srand(time(0));
            randum = rand() % 37; // This will give result in the range 0 - 36

            if (randum == number_chosen)
            {
                money += bet_amount;
                cout << "\n¡Enhorabuena! Has ganado! Ahora tienes " << money << " €\n";
            }
            else
            {
                money -= bet_amount;
                cout << "\nLo sentimos... Has perdido la apuesta, ahora tienes " << money << " €\n";
            }

            break;
        }
        case 2:
        {
            int color = 0, randcol;
            cout << "Ahora escoja rojo (1) o negro (2) -->\n";
            cin >> color;
            srand(time(0));
            randcol = rand() % 2 + 1;
            if (randcol == color)
            {
                money += bet_amount;
                cout << "\n¡Enhorabuena! Has ganado! Ahora tienes " << money << " €\n";
            }
            else
            {
                money -= bet_amount;
                cout << "\nLo sentimos... Has perdido la apuesta, ahora tienes " << money << " €\n";
            }
            break;
        }
        default:
            break;
        }
        cout << "¿Quieres volver a jugar?\n- Sí -> 1\n- No -> 2\n";
        cin >> clear;
        if (clear == 2)
        {
            cout << "Bien, suerte en la próxima tirada.\n\n";
        }

    } while (clear == 1);

    cout << "Tu saldo final es " << money << " €\n";
    return 0;
}

It took me a while to figure out the code because I had to use google translate

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