如何編寫一個程序讀入鍵盤中的一組字符並將它們輸出到控制檯。數據隨機輸入,但有選擇地輸出。只有唯一的字符顯示在控制檯上。因此,無論數組出現多少次,每個字符都應該顯示一次。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
角色是否需要按照輸入的順序出現? –
你的代碼似乎做得很好 –