C ++的其他循环选项

时间:2013-08-13 17:06:59

标签: c++ loops for-loop while-loop

我想知道是否还有其他类型的“循环”我可以用来创建一个更容易阅读的程序。我正在玩循环,我想知道是否有更快或更简单的方法,而不是在输入单词时重复循环...

#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <string.h>
#include <cstdlib>

using namespace std;

char guess1, guess2, guess3;
int main()
{
    // the word is cat...

    cout << " Please enter your first guess : _ _ _ \n";
    cin >> guess1;

    if (guess1 == 'c')
    {
        cout << " please enter your second guess: c _ _ \n";
        cin >> guess2;
        if (guess2 == 'a')
        {
            cout << " please enter your second guess: c a _ \n"; 
            cin >> guess3;
        } 
        if (guess2 == 't')
        {
            cout << " please enter your second guess: c _ t \n";
            cin >> guess3; 
        }
        else
        {
            cout << " Wrong answer : # ";
            cin >> guess3;
        }

        if (guess1 == 'a')
        {
             cout << " please enter your second guess: _ a _ \n";
             cin >> guess2;
        }
        if (guess1 == 't')
        {
            cout << " please enter your second guess: _ _ t \n";
            cin >> guess2;
        }
        else
        {
            cout << " Wrong answer : # ";
            cin >> guess2;
        }
    }
    return 0;
}

3 个答案:

答案 0 :(得分:4)

当然有。你可以这样做:

bool solved = false;
prompt = "Please enter your guess:";
char guess;
char f1='_', f2='_', f3='_'

while(!solved){
   cout << prompt << f1 << f2 << f3 << std::endl;
   cin.get(guess);
   switch(guess){
      case 'c':
        f1 = 'c';
        break;
      case 'a':
        f2 = 'a';          
        break;
      case 't':
        f3 = 't';
        break;
      default:
        cout << "wrong answer";
        break;
   }
   if(f1=='c' && f2=='a' && f3=='t')
     solved = true;
}

你可以轻松地使用while(1)并休息一下;在if条件结束时。 我没有打算真正运行这个,所以可能会有一些错误,但希望这回答了你将如何做这样的事情的一般问题。

另外我注意到你的猜测没有变量,但在空间上使用了c _ _和_ _ t。当你猜两次时会发生什么?这就是变量使用的好主意。

答案 1 :(得分:2)

这是给你的状态机。它需要一个当前状态和一个输入,并为您提供一个新状态。如果您的输入没有新状态,则用户输入了错误的值。

这样做的好处是可以自动创建状态机以支持更多单词。

std::map<std::pair<std::string, char>, std::string> translation;
translation[std::make_pair("_ _ _", 'c')] = "c _ _";
translation[std::make_pair("_ _ _", 'a')] = "_ a _";
translation[std::make_pair("_ _ _", 't')] = "_ _ t";
translation[std::make_pair("c _ _", 'a')] = "c a _";
translation[std::make_pair("c _ _", 't')] = "c _ t";
translation[std::make_pair("_ a _", 't')] = "_ a t";
translation[std::make_pair("_ a _", 'c')] = "c a _";
translation[std::make_pair("_ _ t", 'c')] = "c _ t";
translation[std::make_pair("_ _ t", 'a')] = "_ a t";
translation[std::make_pair("c _ t", 'a')] = "c a t";
translation[std::make_pair("c a _", 't')] = "c a t";
translation[std::make_pair("_ a t", 'c')] = "c a t";

std::string current_state = "_ _ _";
char input;
while(current_state != "c a t") {
    cout << "Please enter your guess: " << current_state << endl;
    cin >> input;
    std::pair<std::string, char> p = std::make_pair(current_state, input);
    if(translation.find(p) == translation.end()) {
        cout << "wrong answer";
        continue;
    }
    current_state = translation[p];
}

答案 2 :(得分:0)

来自http://en.wikipedia.org/wiki/Loop_(computing)#Loops

  

循环是一系列语句,它们被指定一次但可以连续多次执行。循环内部的代码(循环体,下面显示为xxx)遵循指定的次数,或者对于每个项目集合,或者直到满足某些条件,或者无限期地执行。

您只在代码中使用if语句;那些是不是循环,而是简单的一次性条件选择。

有关C ++循环的一些基本信息,请参阅http://www.cplusplus.com/doc/tutorial/control/

相关问题