重复数组C ++

时间:2015-11-25 03:07:58

标签: c++ arrays

我有一个问题,不让我继续我的工作,我的问题是让一个数组总是重复。这是我的程序

#include <iostream>
#include <windows.h>
#include <conio.h>

using namespace std;

int main()
{
    system("color 0B");
    char huruf[5] = {'a', 'b', 'c', 'd', 'e'}, a;
    int i, x;
    cout << "\n\nInput a character : ";
    cin >> a;
    for (i = 0 ; i < 5 ; i++)
        if (huruf[i] == a)
            x = 1;
        if (x == 1)
            cout << "THERE IS";
        else
            cout << "THERE IS NO";
}

我希望Input a Character :总是重复,所以我可以多次输入这个角色。

2 个答案:

答案 0 :(得分:0)

你需要一个循环 在下面的例子中,点击“输入”会让你退出循环

    #include <iostream>
    #include <windows.h>
    #include <conio.h>

    using namespace std;
    int main()
    {
        system("color 0B");
        char huruf[5] = {'a', 'b', 'c', 'd', 'e'}, a;
        int i, x;
        do
    {
        x = 0;
        cout << "\n\nInput a character : ";
        cin >> a;
        for (i = 0 ; i < 5 ; i++)
            if (huruf[i] == a)
                x = 1;
            if (x == 1)
                cout << "THERE IS";
            else
                cout << "THERE IS NO";
    } while (a != '\n');
  }

答案 1 :(得分:0)

您最好的选择可能是使用“while”循环。

所以你要在你的程序中添加这样的东西:

bool choice = true;
while (choice)
{
    [stuff you want to repeat]
    std::cout << "Do you want to enter another character? Enter 1 for yes or 2 for no." << endl;
    int num;
    cin >> num;
    if (num == 2)
    {
        choice = false;
    }
}

或者你可以稍微改变它以减少麻烦:

int main()
{
    system("color 0B");
    char huruf[5] = {'a', 'b', 'c', 'd', 'e'}, a;
    int i, x;
    bool choice = true;
    while (choice)
    {
        cout << "\n\nInput a character, or -1 to quit: ";
        cin >> a;
        if (a == -1)
        {
            choice = false;
        }
        for (i = 0 ; i < 5 ; i++)
            if (huruf[i] == a)
                x = 1;
            if (x == 1)
                cout << "THERE IS";
            else
                cout << "THERE IS NO";
    }
}