【问题标题】:How do I loop my program to continuously ask for a letter until the user enters a Q to end the program如何循环我的程序以不断询问字母,直到用户输入 Q 结束程序
【发布时间】:2020-05-02 21:58:59
【问题描述】:

完成后我意识到我需要循环它直到他们输入 q,但我不知道该怎么做。我正在寻找的是直到最后一个选项('q'或'Q')被选中,主程序回到 开头,要求用户插入一个字母。

#include <ctime>    // For time()
#include <cstdlib>  // For srand() and rand()
#include <iostream>

using namespace std;

int main()
{
    int array[5];
    int i, num, x;
    char c;
    float average = 0;
    srand(time(0)); //This makes the set of numbers differnet every time
    for (i = 1; i <= 5; i++)
    {
        array[i] = (rand() % 5) + 15;
    }
    cout << "Array elements are: \n";
    for (i = 1; i <= 5; i++)
    {
        cout << array[i] << endl;
    }
    cout << "\nEnter your choice:\n      MENU\n[P]osition\n[R]everse\n[A]verage\n[S]earch\n[Q]uit\n";
    cin >> c;
    cout << "\nYou chose option " << c << endl;
    switch (c)
    {
        case 'P':
            cout << "This is the array with each element's position" << endl;
            for (i = 1; i <= 5; i++)
            {
                cout << "Value at position " << i << " is " << array[i] << endl;
            }
            break;
        case 'R':
            cout << "The array in reverse order is:" << endl;
            for (i = 5; i >= 1; i--)
            {
                cout << array[i] << endl;
            }
            break;
        case 'A':
            cout << "The average of the array is:" << endl;
            for (i = 1; i <= 5; i++)
            {
                average = average + array[i];
            }
            cout << average / 5;
            break;
        case 'S':
            cout << "Enter a number to search the array:" << endl;
            cin >> num;
            x = 0;
            for (i = 1; i <= 5; i++)
            {
                if (array[i] == num)
                {
                    x = 1;
                    break;
                }
            }
            if (x == 1)
            {
                cout << num << " is found at position " << i;
            }
            else
            {
                cout << "This number is not in the array";
            }
            break;
        case 'Q':
            exit(1);
            return 0;
    }
}

【问题讨论】:

  • 请修正缩进,以便您的代码可读。
  • 旁注由于这个函数是main,你可以不用自己调用exitreturn来代替。
  • @BessieTheCow 我不确定你的意思是什么,你能详细说明一下吗?
  • 现在已修复,但如果您查看问题的早期版本,代码缩进不符合代码结构,因此难以阅读。
  • 抱歉,您知道如何循环播放程序吗?

标签: c++ arrays loops


【解决方案1】:

你可以像这样把你的代码放在一个while循环中:

    int array[5];
    int i, num, x;
    char c = '\0';
    float average = 0;
    srand(time(0)); //This makes the set of numbers differnet every time
    for (i = 1; i <= 5; i++)
    {
        array[i] = (rand() % 5) + 15;
    }
    while (c != 'q')
    {
        cout << "Array elements are: \n";
        for (i = 1; i <= 5; i++)
        {
            cout << array[i] << endl;
        }
        cout << "\nEnter your choice:\n      MENU\n[P]osition\n[R]everse\n[A]verage\n[S]earch\n[Q]uit\n";
        cin >> c;
        cout << "\nYou chose option " << c << endl;
        switch (c)
        {
        case 'P':
            cout << "This is the array with each element's position" << endl;
            for (i = 1; i < 5; i++)
            {
                cout << "Value at position " << i << " is " << array[i] << endl;
            }
            break;
        case 'R':
            cout << "The array in reverse order is:" << endl;
            for (i = 5; i >= 1; i--)
            {
                cout << array[i] << endl;
            }
            break;
        case 'A':
            cout << "The average of the array is:" << endl;
            for (i = 1; i <= 5; i++)
            {
                average = average + array[i];
            }
            cout << average / 5;
            break;
        case 'S':
            cout << "Enter a number to search the array:" << endl;
            cin >> num;
            x = 0;
            for (i = 1; i <= 5; i++)
            {
                if (array[i] == num)
                {
                    x = 1;
                    break;
                }
            }
            if (x == 1)
            {
                cout << num << " is found at position " << i;
            }
            else
            {
                cout << "This number is not in the array";
            }
            break;
        case 'Q':
            exit(1);
            return 0;
        }
    }

所以代码只有在c 不等于q 时才会运行。希望这会有所帮助:)

【讨论】:

  • 我应该把它放在哪里?
  • 那里,我编辑了它,以便您了解它的位置。
  • 什么不起作用?我包括了完整的代码。这对我来说可以。我还将 while 循环向后移动了一点,以包括显示数组
  • 哦,我弄错了,现在可以了,感谢您的帮助
猜你喜欢
  • 2022-09-29
  • 1970-01-01
  • 1970-01-01
  • 2021-07-19
  • 1970-01-01
  • 1970-01-01
  • 2018-05-03
  • 2021-04-25
  • 1970-01-01
相关资源
最近更新 更多