循环C ++不要为Cin停下来

时间:2015-04-26 12:03:08

标签: c++ loops

    shipname[0] = "Aircraft";
    shipname[1] = "Battleship";
    shipname[2] = "Destoryer";
    shipname[3] = "Submarine";
    shipname[4] = "Patrol Boat";

    cout << "Do you wish to place your own ships. Y/N ";
    cin >> Isplaceship;


        if ((Isplaceship = "Y") | (Isplaceship = "y"))
        {
   for (int i = 0; i < 5; i++){
        cout << "Please Enter a location for your " << shipname[i] << endl;
        cout << "type row. col & direction(0 horizontal, 1 vertical) split by spaces: ";
        cin >> x >> y >> dir;
        cout << "your input is " << x << " " << y << " " << dir;
    }


    cout << endl;

}
    else if ((Isplaceship = "N") | (Isplaceship = "n"))
    {

    };


}

所以我想做一个战舰游戏,但在这个阶段我希望用户根据船只输入他们的输入,但它永远不会停止并只显示船名中的所有结果。我永远不能输入任何输入。

2 个答案:

答案 0 :(得分:3)

我自己做了一些编码:

int i = 0;
for (int i = 0; i <= 5; ++i)
{
    cout << "input a number:" << endl;
    cin >> i;
    cout << "The number you input is:" << i << endl;
}

结果是:

input a number:
1
The number you input is:1
input a number:
2
The number you input is:2
input a number:
3
The number you input is:3
input a number:
4
The number you input is:4
input a number:
5
The number you input is:5

我不太清楚为什么你会得到这个结果,但我认为你可以将我的代码与你的代码进行比较或者做一些其他类似的编码,这可能会帮助你弄清楚你错过的一些细节。

答案 1 :(得分:1)

将此更改为if ((Isplaceship = "Y") | (Isplaceship = "y"))if ((Isplaceship == "Y") | (Isplaceship == "y"))