如何编写一个程序读入键盘中的一组字符并将它们输出到控制台。数据随机输入,但有选择地输出。只有唯一的字符显示在控制台上。因此,无论数组出现多少次,每个字符都应该显示一次。C++:数组函数
例如,如果一个数组
Char letterArray[ ] = {B,C,C,X,Y,U,U,U};
输出应该是:
B,C,X,Y,U
这是我迄今所做的......
char myArray [500];
int count = 0;
int entered = 0;
char num;
while (entered < 8)
{
cout << "\nEnter a Character:";
cin >> num;
bool duplicate = false;
entered++;
for (int i = 0; i < 8; i++)
{
if (myArray[i] == num)
duplicate=true;
}
if (!duplicate)
{
myArray[count] = num;
count++;
} // end if
else
cout << num << " character has already been entered\n\n";
// prints the list of values
cout<<"The final Array Contains:\n";
for (int i = 0; i < count; i++)
{
cout << myArray[i] << " ";
}
}
问题或疑问是什么? – Zohaib
角色是否需要按照输入的顺序出现? –
你的代码似乎做得很好 –