而/ For循环不会循环.(C++)

0 c++ loops

我是编程新手.但是,由于我正在制作一个简单的老虎机(它还没有奖品),我似乎无法制作循环循环,并让玩家再试一次!如果我将条件设置为1,它会循环正常,但不是我有一个简单的(i> 0)条件.这是代码:

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

using namespace std;

int slot1 (int low, int high)
{
    return rand() % ( high - low + 2 ) + low;
}

int slot2 (int low, int high)
{
    return rand() % ( high - low + 2 ) + low;
}

int slot3 (int low, int high)
{
    return rand() % ( high - low + 2 ) + low;
}

int main()
{
    int i = 100;
    cout << "\n\n       Welcome to the Slot Machine!\n\n";
    cout << "       You have " << i << " coins!\n\n";
    cout << "       Each spin costs 1 coin! Press ENTER to begin.\n\n";
"\n";

while ( i > 0 )
{
    cin.get();
    "\n";
    --i;
    srand( time( NULL ) );
    cout << "       | " << slot1( 1, 8);
    cout << " | " << slot2( 1, 8);
    cout << " | " << slot3( 1, 8) << " |\n";

    // if statements adding to int i, depending of the values of slot1, slot2 and slot3.

    if (i = 0 )
    {
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

}

Lig*_*ica 5

if (i = 0 )
Run Code Online (Sandbox Code Playgroud)

这应该是==.符号=用于赋值,而不是相等比较.

虽然i = 0错误地进行了评估,因此内部break没有被执行,但这并不重要,因为接下来发生的事情是i > 0失败的,所以你的循环无论如何都会结束.在第一次迭代.每次运行程序.

您还需要将该srand调用移出循环:您应该在程序中使用一次,而不是n次.