C ++代码陷入无限循环 [英] C++ code stuck in infinite loop

查看:66
本文介绍了C ++代码陷入无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;

struct Player {
    string name;
    int money;
    string one;
};

char secretPhrase[] = "Easy as pie";
char mask[sizeof(secretPhrase) - 1];


void displayBlankPhrase(char p[], char g)
{
        for (int i = 0; i < sizeof(secretPhrase)-1 ; i++)
        {

            if (secretPhrase[i] == g) 
                mask[i] = g;
        }
        for (int i = 0; i < sizeof(mask); i++)
            cout << mask[i];

        cout << endl;
    }

void init()
{
    //Initialize Mask
    for (int i = 0; i < sizeof(secretPhrase) - 1; i++)
    {
        if (secretPhrase[i] == ' ')
            mask[i] = ' ';
        else {
            mask[i] = '*';
        }
    }
}

bool isGameOver() {
    for (int i = 0; i < sizeof(mask) - 1; i++)
        if(mask[i] == '*')
                    return false;

    return true;
}

int main()
{
    // Initializes mask array
    init();

    //Welcome Screen | This is where you enter player information

    Player one;
    one.money = 0;
    cout << "Welcome to 'Wheel of Fortune'!" << endl;
        cout << "What is your name?: ";
        cin >> one.name;
    cout << "Hello " << one.name << 
        ". You starting amount is $" << Player().money << "." << endl;


    // WHEEL
    int wheelSlots[24];
    wheelSlots[0] = 0;
    wheelSlots[1] = 1;
    wheelSlots[2] = 50;
    wheelSlots[4] = 200;    
    wheelSlots[3] = 1000;
    wheelSlots[5] = 3000;
    wheelSlots[6] = 400;
    wheelSlots[7] = 0;
    wheelSlots[8] = 5000;
    wheelSlots[9] = 600;
    wheelSlots[10] = 10;
    wheelSlots[11] = 300;
    wheelSlots[12] = 500;
    wheelSlots[13] = 8000;
    wheelSlots[14] = 25;
    wheelSlots[15] = 400;
    wheelSlots[16] = 125;
    wheelSlots[17] = 10000;
    wheelSlots[18] = 0;
    wheelSlots[19] = 300;
    wheelSlots[20] = 250;
    wheelSlots[21] = 600;
    wheelSlots[22] = 400;
    wheelSlots[23] = 50;

    int playersMenu = 0;
    int playerOptions;
    char playersChoice;
    bool gamePlay = true;
    bool playerWin = false;
    int playerSpins = 0;
    int spin;
    string playerSolve;

;

while (gamePlay == true && playerWin == false) {
    if (playerSpins < 1) {
        //Checks to see if play has spun the wheel
        cout << "Spin the wheel to start the game..." << endl;
        playerOptions = 2;
        for (int i = 0; i < sizeof(secretPhrase) - 1; i++) {
            secretPhrase[i] = '-';
        }
    }
    else {
        cout << "\n\n Choose an option?: " << endl
            << "1. Guess a letter" << endl
            << "2. Spin the wheel" << endl
            << "3. Buy a vowel (- $500)" << endl
            << "4. Solve the puzzle" << endl;
        cin >> playerOptions;
    }
    playersChoice = '*';
    // Evaluate players options options 1-4
    if (playersMenu == 1) {
        cout << "Guess a letter for $: " << playerSpins;
        cin >> playersChoice;

    }
    if (playersMenu == 2){
        playerSpins++;
    // Random wheel number generator
    spin = rand() % 25;
    cout << endl << "The wheel landed on: $" << wheelSlots[spin] << "." << endl;
    if (spin == 0 || spin == 1) {
        one.money = 0;
        cout << "OH NO! Bankrupt!" << endl;
    }
}

    if(playersMenu == 3){
        one.money -= 250;
        cout << "Here is your money now: $" << one.money << ". " << "Guess a vowel: ";
        cin >> playersChoice;
    }
    if(playersMenu == 4){

        cout << "You want to solve (no caps please and use underscores for spaces):"; // Underscores because of time constraints.
        cin >> playerSolve;
        if (playerSolve == secretPhrase) {
            cout << "You win!" << endl;
            playerWin = true;
            gamePlay = false;
        }
    }
}


return  0;
}





我的尝试:



我正在尝试创建一个财富游戏之轮,但在输入玩家名称后我的代码陷入无限循环时遇到问题。请帮忙!!!有人可以查看我的代码或在他们自己的环境中测试它。我现在真的知道我缺少什么或需要改变以突破这个循环



What I have tried:

I am trying to create a wheel of fortune game, but having issues with my code being stuck in an infinite loop after entering the players name. PLEASE HELP!!! Could someone please look over my code or test it in their own environment. I really have now idea what is I am missing or need to change to break out of this loop

推荐答案

<< Player()。money<< ;。<<< endl;


// WHEEL
int wheelSlots [24];
wheelSlots [0] = 0;
wheelSlots [1] = 1;
wheelSlots [2] = 50;
wheelSlots [4] = 200;
wheelSlots [3] = 1000;
wheelSlots [5] = 3000 ;
wheelSlots [6] = 400;
wheelSlots [7] = 0;
wheelSlots [8] = 5000;
wheelSlots [9] = 600;
wheelSlots [10] = 10;
wheelSlots [11] = 300;
wheelSlots [12] = 500;
wheelSlots [13] = 8000;
wheelSlots [14] = 25;
wheelSlots [15] = 400;
wheelSlots [16] = 125;
wheelSlots [17] = 10000;
wheelSlots [18] = 0;
wheelSlots [ 19] = 300;
wheelSlots [20] = 250;
wheelSlots [21] = 600;
wheelSlots [22] = 400;
wheelSlots [23] = 50;

i nt playersMenu = 0;
int playerOptions;
char playersChoice;
bool gamePlay = true;
bool playerWin = false;
int playerSpins = 0;
int spin;
string playerSolve;

;

while(gamePlay == true&& playerWin == false){
if(playerSpins< 1){
//检查游戏是否旋转了wheel
cout<< 旋转方向盘开始游戏......<< ENDL;
playerOptions = 2;
for(int i = 0; i< sizeof(secretPhrase) - 1; i ++){
secretPhrase [i] =' - ';
}
}
else {
cout<< \ n \\ n选择一个选项?:<< endl
<< 1.猜一封信<< endl
<< 2.旋转车轮<< endl
<< 3.购买元音( -
" << Player().money << "." << endl; // WHEEL int wheelSlots[24]; wheelSlots[0] = 0; wheelSlots[1] = 1; wheelSlots[2] = 50; wheelSlots[4] = 200; wheelSlots[3] = 1000; wheelSlots[5] = 3000; wheelSlots[6] = 400; wheelSlots[7] = 0; wheelSlots[8] = 5000; wheelSlots[9] = 600; wheelSlots[10] = 10; wheelSlots[11] = 300; wheelSlots[12] = 500; wheelSlots[13] = 8000; wheelSlots[14] = 25; wheelSlots[15] = 400; wheelSlots[16] = 125; wheelSlots[17] = 10000; wheelSlots[18] = 0; wheelSlots[19] = 300; wheelSlots[20] = 250; wheelSlots[21] = 600; wheelSlots[22] = 400; wheelSlots[23] = 50; int playersMenu = 0; int playerOptions; char playersChoice; bool gamePlay = true; bool playerWin = false; int playerSpins = 0; int spin; string playerSolve; ; while (gamePlay == true && playerWin == false) { if (playerSpins < 1) { //Checks to see if play has spun the wheel cout << "Spin the wheel to start the game..." << endl; playerOptions = 2; for (int i = 0; i < sizeof(secretPhrase) - 1; i++) { secretPhrase[i] = '-'; } } else { cout << "\n\n Choose an option?: " << endl << "1. Guess a letter" << endl << "2. Spin the wheel" << endl << "3. Buy a vowel (-


500)<< endl
<< 4.解决难题<< ENDL;
cin>> playerOptions;
}
playersChoice ='*';
//评估玩家选项选项1-4
if(playersMenu == 1){
cout<< 猜一封信为
500)" << endl << "4. Solve the puzzle" << endl; cin >> playerOptions; } playersChoice = '*'; // Evaluate players options options 1-4 if (playersMenu == 1) { cout << "Guess a letter for


:<< playerSpins;
cin>> playersChoice;

}
if(playersMenu == 2){
playerSpins ++;
//随机轮数生成器
spin = rand()%25;
cout<< endl<< 车轮着陆:
: " << playerSpins; cin >> playersChoice; } if (playersMenu == 2){ playerSpins++; // Random wheel number generator spin = rand() % 25; cout << endl << "The wheel landed on:


这篇关于C ++代码陷入无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆