How do I make a C++ program where it identifies the first repeating character?

0 votes
asked May 26, 2019 by anonymous
edited May 26, 2019

This is my work so far:

#include <iostream> //Including parts of code

#include <string>

#include <algorithm>

#include <string>

std::string input;        //Declaring variables

int A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z;

int capLetters[26] = {A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z};

void counter_print(std::string input, char character,int value){ //Defining a function

  if(input.find(character) != std::string::npos){ //Identifies the letters

    value++;

  }

  std::cout<<capLetters[0]<<'\n';

  std::sort(capLetters, capLetters+26,std::greater<int>());

  

  if(capLetters[0]==value){

    std::cout<<character;

  }

}

int main(){

  std::cout << "\x1B[2J\x1B[H";

  std::cout<<"\n*****START PROGRAM******\nEnter a sequence of Capital letters. The program will identify which ones repeat.\n\n";

  std::cin>>input;

  counter_print(input,'A',A);

  counter_print(input,'B',B);

  counter_print(input,'C',C);

  counter_print(input,'D',D);

  counter_print(input,'E',E);

  counter_print(input,'F',F);

  counter_print(input,'G',G);

  counter_print(input,'H',H);

  counter_print(input,'I',I);

  counter_print(input,'J',J);

  counter_print(input,'K',K);

  counter_print(input,'L',L);

  counter_print(input,'M',M);

  counter_print(input,'N',N);

  counter_print(input,'O',O);

  counter_print(input,'P',P);

  counter_print(input,'Q',Q);

  counter_print(input,'R',R);

  counter_print(input,'S',S);

  counter_print(input,'T',T);

  counter_print(input,'U',U);

  counter_print(input,'V',V);

  counter_print(input,'W',W);

  counter_print(input,'X',X);

  counter_print(input,'Y',Y);

  counter_print(input,'Z',Z);

  std::cout<<"\n\n*****END PROGRAM*****\n";

  return 0;

}

However, it doesn't give output. How do I edit it so it will work correctly?

2 Answers

+2 votes
answered May 28, 2019 by Pierre (320 points)

For the love of God, never copy/paste the same line twenty-six times! Use a loop:

int letterCount[26];

for(char c = 'A'; c <= 'Z'; c++)

         counter_print(input, c, letterCount[c - 'A']);

+1 vote
answered Apr 27 by gfrve dfss (340 points)
to make it easier, put the sentence
USING NAMESPACE STD and after no more need to add std ::
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...