简体   繁体   English

在 do while 循环后如何保持变量的值?

[英]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你可以使用 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我花了一段时间才弄清楚代码,因为我必须使用谷歌翻译

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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