如何检查如果cin缓冲区包含相同的值0.5sec(C ++)

时间:2018-01-15 08:32:40

标签: c++

我想创建一个程序,这样当我输入1时,它会打印go ahead无限次,就像那样2-go left 3-go right。当我输入0时,它应该停止所有内容并打印!!!stop!!!

我尝试了很多东西,但我甚至无法使1命令工作。请帮忙。

    #include "stdafx.h"
    #include<iostream>
   using std::cout;
   using std::cin;
   using std::endl;


    int main()
    {
        int value;
        cout << "Would you please enter the command " << endl;
        cout << " _______________________" << endl;
        cout << " 1- go ahead           |" << endl;
        cout << " _______________________" << endl;
        cout << " 2- make a left turn   |" << endl;
        cout << " _______________________" << endl;
        cout << " 3- make a right turn  |" << endl;
        cout << " _______________________" << endl;
        cout << " 4- go back            |" << endl;
        cout << " _______________________" << endl;
        cout << " 0- stop everything    |" << endl;
        cout << " _____________________________________________________" << endl;

        cin >> value;

        switch (value)
        {
        case 1:
            while (int value == 1)
            {
                cout << "  * go ahead " << endl;
                if (value == 0)
                {
                    break;
                }
            }
            break;
        case 2:
            cout << " * turn left " << endl;
            break;
        case 3:
            cout << " * turn right " << endl;
            break;
        case 4:
            cout << " * reverse " << endl;
            break;
        case 0:
            cout << " !!!Stop!!! " << endl;
            break;

        }


        return 0;
    }

3 个答案:

答案 0 :(得分:3)

for(value==1)没有有效的C ++语法,即使是这样,您的代码也会在输入1后无限地打印“继续”。
除此之外,您的switch个案例正在检查ASCII 1而不是数字1.将'1'更改为1

您需要使用某种循环来包围输入switch语句:

int value = 0;
do{
    cin >> value;
    switch(value){
        case 1:
            cout << "go ahead" << endl;
            break;
        case 2:
            cout << "turn left" << endl;
            break;
        case 3: 
            cout << "turn right" << endl;
            break;
        case 4:
            cout << "reverse" << endl;
            break;
        case 0:
            cout << "stop" << endl;
            break;            
    }    
}while(value!=0);

此代码将要求输入并在之后打印方向。

答案 1 :(得分:3)

你有三个问题。首先,'1'是一个字符文字,1是一个整数文字(我知道没有实现它们具有相同的值)。你需要摆脱报价。

其次,for (int value == 1)无效C ++。您需要将其更改为while (value == 1)

最后,最重要的是,循环内的任何内容都不允许value更改。它将是一个无限循环(因而是未定义的行为)。您需要在一个线程上运行无限循环,并在另一个线程的循环中进行输入。

(另外,请不要使用using namespace std;

答案 2 :(得分:1)

在案例陈述中删除''

switch (value)
    {
    case 1:
        for (value == 1)
        {
            cout << "  * go ahead " << endl;
            if (value == 0)
            {
                break;
            }
        }
        break;
    case 2:
        cout << " * turn left " << endl;
        break;
    case 3:
        cout << " * turn right " << endl;
        break;
    case 4:
        cout << " * reverse " << endl;
        break;
    case 0:
        cout << " !!!Stop!!! " << endl;
        break;

    }