Repeating characters problem. :(

I'm having a problem with a program. The program should make an char array with max 80 chars, then the user should input something (for example, user inputs: abacdce) and then the program should make another char array that contains every char from first char array that is not repeating itself. So if user inputted : abacdce then the program should give out : bde. Because 'b' and 'd' and 'e' are not repeating itselfs in the first char array.

This is my code. It just gives an error. What seems to be a problem ?



#include <iostream>

using namespace std;

int main()
{
char a[80];
char b[80];
int n, m, anzahl = 0;

cout << "Array? ";
cin >> a;

for(int i = 0; a[i] != '\0'; i++)
anzahl++;

for(int i = 0; a[i] != '\0'; i++, n++)
{
for(int j = anzahl-1; j >= i; j--)
{
if(a[i] == a[j])
{
m++;
b[n] = m;
}
}
}

b[n] = '\0';

for(int i = 0; b[i] != '\0'; i++)
{
if(b[i] == 0)
{
cout << b[i];
}
}
}
You code is far more complex than it has to be. All you need to do is;

1: Declare an array of 26 int's
2: Declare a string

3 Iterate through each character, testing if it's alpha, if so converting to uppercase and then subtracting 41 from it. That leaves you with an index and you can increment your array by one for each occurrence.

After all is done each position will be either;
0 if that character wasn't used
1 if it was only used once
>1 in all other cases.

I'm going to give you a big boost here, as you did put in a reasonable attempt.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

 using namespace std;

 int main() {
	 int Used [26] = {0};
	 string Entry;
	 
	 getline ( cin, Entry, '\n' );
	 
	 if ( !Entry.empty () ) {
		 for ( auto ch : Entry ) {
                 // Your code here
		 }
	 }
	
       // Your code to do whatever type of display you want.	
	return 0;
 }

I've got a completed version of the program and this is the output from your example.

	2 A
	1 B [B] 

	2 C
	1 D [D] 

	1 E [E] 

	0 F
	0 G
	0 H
	0 I
	0 J
	0 K
	0 L
	0 M
	0 N
	0 O
	0 P
	0 Q
	0 R
	0 S
	0 T
	0 U
	0 V
	0 W
	0 X
	0 Y
	0 Z
Thanks a lot, that was really helpful !! :)
Topic archived. No new replies allowed.