c++ - 如何循环我的程序以不断询问字母,直到用户输入 Q 结束程序
问题描述
完成后我意识到我需要循环它直到他们输入 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;
}
}
解决方案
您可以像这样将代码放入 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
。希望这可以帮助 :)
推荐阅读
- r - 我可以使用光线着色器重新缩放 R 中的 x/y 轴纵横比吗?
- excel - Excel 公式 INDEX+AGREGATE+ORDER 按另一列
- git - Git 从 repo 中删除文件而不删除 WHEN OTHERS PULL
- amazon-web-services - AWS Glue Sagemaker 笔记本“没有名为 awsglue.transforms 的模块”
- c++ - Arduino延迟不精确
- bash - 如何合并两个方括号模式之间的线
- python - TensorFlow2.0.0 Alpha-模块'tensorflow'没有属性'gfile'
- arduino - 使用 VB6 和 Arduino UNO 的指纹传感器登录表单
- typescript - 如何在打字稿中检测 boostrap 卡头悬停?
- linux - Bash 脚本没有扩展我的 $file 变量?