替代嵌套循环中的goto?

时间:2017-11-11 05:09:31

标签: c++ while-loop switch-statement do-while goto

此代码工作正常,但是我一直试图避免使用您将在switch(goto)语句中看到的dice_total语句。

如果没有goto语句,程序将不会循环回while (again=='y' || again=='Y')的开头,而是在到达do-while循环时保持循环。

但是,我认为同样重要的是,如果dice_total是=第一次的point_total那么程序将正常运行,并循环回到开头。例如,当程序启动时,第一轮将生成point_total,我们将其说为10.这是一个允许程序继续下一轮的值,如果dice_total也得到相同的数字,10 ,该程序会说你赢了,循环将正常工作。但是,如果程序到达do while循环,并生成一个不是10的数字,但在几个循环后生成10,那么程序将不会循环到开头。那么我想问一下,我的switch(dice_total)语句出了什么问题,以及如何修复它,在不使用goto语句的情况下为程序提供相同的效果?

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

int main()
{
//Declared Variables***********************************
char again = 'y';
int point1;
int point2;
int point_total;
int round_1=1;
int dice1;
int dice2;
int dice_total;
//*****************************************************
//RANDOM SEED******************************************
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int>dist(1, 6);
//*****************************************************

start://TEMPORARY
while (again == 'y'||again=='Y')
{
    int round_1 = 1;
    system("CLS");
    cout << "WELCOME TO THE CRAPS GAME" << endl;
        cout << "THROWING ROUND:" << round_1 << " DICES.............." << endl;
        point1 = dist(mt);
        point2 = dist(mt);
        point_total = point1 + point2;
        cout << "ROUND: " << round_1 << " First dice is: " << point1 << " and second dice is: " << point2 <<" and the total is:"<<point_total<< endl;
        switch (point_total)
        {
        case 7:
        case 11:
            cout << "YOU WON CONGRATS PRESS Y TO PLAY AGAIN!!" << endl;
            cin >> again;
            break;
        case 2:
        case 3:
        case 12:
            cout << "YOU LOST, PRESS Y TO TRY AGAIN" << endl;
            cin >> again;
            break;
        default:
            do
            {
                ++round_1;
                cout << "ROUND " << round_1 << endl;
                dice1 = dist(mt);
                dice2 = dist(mt);
                dice_total = dice1 + dice2;
                cout << "THROWING ROUND: " << round_1 << " DICES.............." << endl;
                cout << "ROUND 1 DICE TOTAL IS: " << point_total << endl;
                cout << "ROUND: " << round_1 << " First dice is: " << dice1 << " and second dice is: " << dice2 << " and the total is:" << dice_total << endl;
                switch (dice_total)
                {
                case 11:
                    cout << "YOU WON CONGRATS PRESS Y TO PLAY AGAIN!!" << endl;

                    cin >> again;
                    goto start;
                case 2:
                case 3:
                case 7:
                case 12:
                    cout << "YOU LOST, PRESS Y TO TRY AGAIN" << endl;

                    cin >> again;
                    goto start;

                default:
                    if (dice_total == point_total)
                    {
                        cout << "YOU WON CONGRATS PRESS Y TO PLAY AGAIN!!<<endl;

                        cin >> again;
                        break;
                    }//if
                    else
                    {
                        cout << "Going to next round" << endl;

                    }

                }//dice_total
            }//do
            while (dice_total != point_total);
            break;
        }//switch point
    }//again while
}//main

2 个答案:

答案 0 :(得分:1)

当你在同一个函数中有太多嵌套循环时,你遇到的问题很常见,并且你需要将代码的一部分重构为自己的函数。

如果你这样做,那么你有更多的可能来控制你的代码流:在每个函数中你有breakreturn,并且你可以返回自定义值,你可以使用如果您需要再次breakreturn,请在周围的函数中确定。

此外,这使您有机会为您的函数添加不言自明的名称,这使得您的代码第一次看到它的人更清楚(因为它写的,它是如此密集,我无法理解它除非我盯着它看几分钟。)

我在代码中的含义示例:

之前

int main() {
    start:
    while (a) {
        b1();
        switch(c) {
            case 1:
                do {
                    d();
                    if (cond) goto start;
                } while(e);
            break;
        }
        b2();
    }
}

int main() {
    while (a) {
        if (!doStuff1())
            break;
    }
    ...
}

bool doStuff1() {
    b1();
    while (a) {
        bool res = doStuff2();
        if (res) return true;
    }
    b2();
    ...
}

bool doStuff2() {
     switch(c) {
         case 1: 
             if (doStuff3()) return true;
     }
     return false;
}

bool doStuff3() {
     do {
         d();
         if (cond) return true;
     } while (e);

     return false;
}

答案 1 :(得分:0)

这个设计怎么样?

bool stop=false;
while(!stop && (again == 'y'||again=='Y'))
{
   while(again == 'y'||again=='Y')
   {

        //  ...
        break; /* breaks inner while*/

        //  ...
        stop=true;
        break; /* breaks inner while, and prevents running outer loop*/


   }
}