First you can make your program a lot smaller(in size) and better(general) using std::map
Version 1: Does not display the output in insertion order
#include <iostream>
#include <map>
int main() {
std::string inputString;
std::cout<<"Enter a string: ";
std::getline(std::cin,inputString);
//this map maps the char to their respective count
std::map<char, int> charCount;
for(char &c: inputString)
{
charCount[c]++;
}
for(std::pair<char, int> pairElement: charCount)
{
std::cout << pairElement.first <<"-" << pairElement.second<<std::endl;
}
return 0;
}
The output of the above Version 1 is as follows:
Enter a string: Prog
P-1
g-1
o-1
r-1
Note in the above order the order of the characters is alphabetical. If you want the output in order then do this:
Version 2: Displays the output in insertion order as you want
#include <iostream>
#include <map>
int main() {
std::string inputString;
std::cout<<"Enter a string: ";
std::getline(std::cin,inputString);
//this map maps the char to their respective count
std::map<char, int> charCount;
for(char &c: inputString)
{
charCount[c]++;
}
//just go through the inputString instead of map
for(char &c: inputString)
{
std::cout << c <<"-" << charCount.at(c)<<std::endl;
}
return 0;
}
The output of this 2nd version is as follows:
Enter a string: Prog
P-1
r-1
o-1
g-1
Also the output of the above version 2 when the input has re-occuring characters like "AnoopRana" will be repeated as shown below:
Enter a string: AnoopRana
A-1
n-2
o-2
o-2
p-1
R-1
a-2
n-2
a-2
To display each character only once you can see this program which is as follows:
Version 3: For displaying each character exactly once and in insertion order
#include <iostream>
#include <map>
int main() {
std::string inputString;
std::cout<<"Enter a string: ";
std::getline(std::cin,inputString);
//this map maps the char to their respective count
std::map<char, int> charCount;
for(char &c: inputString)
{
charCount[c]++;
}
std::size_t i = 0;
//just go through the inputString instead of map
for(char &c: inputString)
{
std::size_t index = inputString.find(c);
if(index != inputString.npos && (index == i)){
std::cout << c <<"-" << charCount.at(c)<<std::endl;
}
++i;
}
return 0;
}
Note
Both of these version count small and capital letters separately which is what you want.