I wrote some code in C++ to display duplicate characters in a string, but if a character is repeated more than three times, the code prints the repeated character more than once.
For example if the string is aaaddbss
, it should only print out ads
but it prints aaads
instead.
What am I doing wrong?
cout << " Please enter a string" << endl;
cin.getline(input, 100); // example input (ahmad wahidy) the output reads a a h a d instead of a h d
for (int i = 0;input[i]!='\0'; i++)
{
for (int j = i+1;input[j]!='\0'; j++)
{
if (input[i] == input[j])
{
cout << input[i] << " ";
}
}
}
cout << endl;
input
. Please make it a complete example. I have a feelinginput
is notstd::string
, which it should be since using raw character buffers is a bad plan. – tadman Oct 20 '17 at 19:16input
can't bestd::string
, since it wouldn't compile if it were.cin
doesn't have an overload ofgetline
method which acceptsstd::string
. – Algirdas Preidžius Oct 20 '17 at 19:21std::set
be better? – Some programmer dude Oct 20 '17 at 19:32