Do-while循环运行两次时应该只运行一次(C ++)

时间:2013-11-29 02:15:54

标签: c++ do-while

朋友。我有一个问题。

问题:计算机必须从36个字符串的数组中随机挑选一个字符串。如果它有机会选择字符串#34或#35(最后两个字符串),它必须从同一个数组中抽取两个以上的随机字符串。我尝试了一个do-while解决方案,它“几乎”有效(见下面的代码)。

随机化工作正常 - 在main()中调用srand。有一个强制的“x2”绘图(出于测试原因),因此计算机再绘制两个字符串。这两个新的随机选择不是“x2”,但仍然循环再次踢 - 但只是再一次!这次计算机又选择两个“chits”,它们也不是“x2”,因此,正如预期的那样,它返回“chits have drawn”句子,并且函数终止。为什么相同的代码运行两次具有相同的结果但是if / else行为不同?非常感谢你提前。

        string mortalityCalc ()
{
        string mortalityChits[36] = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","-","-","-","-","x2","x2"};
        int mortalityResult;
        // mortalityResult = rand() %36;
        mortalityResult = 35; // for testing only. Delete afterwards.
        string drawnChit = mortalityChits[mortalityResult];
        string drawnChit1;
        string drawnChit2;
        if (drawnChit != "-" && drawnChit != "x2")
        {
            string returnText = string("The computer has drawn the chit '") + drawnChit + "'.";
            return returnText;
        }
        else if (drawnChit == "-")
        {
            string returnText = string("The computer has drawn the chit '") + drawnChit + "'. No senators died this year.";
            return returnText;
        }
        do
        {
            cout << "The computer has drawn the 'x2' chit." << endl;
            cout << "Two more chits will be drawn.\n" << endl;

            mortalityResult = rand() %36;
            drawnChit1 = mortalityChits[mortalityResult];
            cout << "The first draw is the chit '" << drawnChit1 << "'. ";

            mortalityResult = rand() %36;
            drawnChit2 = mortalityChits[mortalityResult];
            cout << "The second draw is the chit '" << drawnChit2 << "'." << endl;
        } while (drawnChit1 == "x2" || drawnChit2 == "x2");

        return "The mortality chits have been drawn. The corresponding senators are dead.";
}

更新:尝试运行与程序其余部分隔离的代码,它的行为符合预期。所以我想发布之前的内容非常重要:

cout << "If you are a lazy bastard, the computer can pick one senator randomly for you.\nAre you a lazy bastard? [y/n]" << endl;
        string lazyBastard;
        cin >> lazyBastard;
        cout << endl;
        if (lazyBastard == "y" || lazyBastard == "Y" || lazyBastard == "yes" || lazyBastard == "YES" || lazyBastard == "Yes")
        {
            mortalityCalc ();
            cout << mortalityCalc () << endl;
            cout << "Very well. Now, imminent wars become active (Only one of each match)." << endl;
            cout << "Get ready for the next phase." << endl;

我的猜测,从这里阅读其他一些问题,就是不知何故cin正在搞乱循环行为,即使它们没有关联,并且在循环的语句或条件中没有任何用户输入。那可能吗?如果是这样,为什么以及如何补救它?

再次感谢你。

2 个答案:

答案 0 :(得分:0)

在第一个循环中,你强制一个'x2',所以你进入了do-while循环。两个调用'rand())%36'的结果总是19和30到34之间的数字。关键是随机数生成器总是生成相同的数字序列,如果你不给他一个种子'srand(...)'。

    do {
        // ...
        cout << rand()%36;
        // ...
    } while( /*...*/ )

参见http://ideone.com/zl8ggH 您必须创建随机数,并且您的代码符合您的预期。

答案 1 :(得分:0)

最后!我认为这将是一个愚蠢的事情!我刚刚意识到我曾两次调用deathCalc()函数!这就是它循环两次的原因!

感谢所有试图提供帮助的人!