重复数组C++
repeating array C++
我有一个问题无法继续我的工作,我的问题是使数组总是重复。这是我的程序
#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 << "nnInput 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 :
总是重复自己,这样我就可以多次输入字符。
您需要一个循环在下面的例子中,点击"回车"键可以使脱离循环
#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 << "nnInput 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');
}
您的最佳选择可能是使用"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 << "nnInput 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";
}
}
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '