Delete Repeat

I almost got this code to work. I don't need it to output extra array information, only the sentence line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  #include <iostream>

void introduction();
//Explains what the program does

void fill_array(char a[], int size, int& number_used);
//Array a[] is filled with character data from the keyboard

void delete_repeats(char a[], int& number_used);
//Function will remove all repeated characters and move the rest of the characters
//foward to fill in the gap.

void output(char a[], int& number_used);
//Outputs the contents of the array and outs the new size of the array

int main()
{
  using namespace std;
  char array[100];
  int number_used;
  introduction();
  fill_array(array, 100, number_used);
  output(array, number_used);
}

//uses iostream
void introduction()
{
  using namespace std;
  cout << "Enter a text string to test:\n";

}

//uses iostream
void fill_array(char a[], int size, int& number_used)
{
  using namespace std;
  char c;
  int index = 0;
  cout << "Please type in a sentence and then press enter.\n";
  cin.get(c);


  while (c != '\n' && index < size) {
    a[index] = c;
    cin.get(c);
    index++;
  }
  number_used = index;


  for (int i = 0; i < number_used; i++) {
    for (int j = i + 1; j < number_used; j++) {
      if (a[i] == a[j]) {
	cout << a[i] << " duplicates found at "
	     << i << " and " << j <<std::endl;
	number_used=number_used-1;
	for (int k = j; k < number_used; k++)
	  a[k] = a[k + 1];
	a[number_used] = '\0';
	cout << a <<std::endl;
	cout << number_used<<std::endl;
	--i; break;
      }
    }
    cout << a[i]<<std::endl;
    cout << number_used<<std::endl;
  }
  cout << number_used<<std::endl;
}



//uses iostream
void output(char a[], int& number_used)
{
  using namespace std;
  cout << "The new sentence without the repeated letters is:\n";
  for (int i = 0; i < number_used; i++) {
    cout << a[i];
  }
  cout << "\nThe size of the new array is "
       << number_used
       << endl;


  cin.ignore();
  cin.get();
}
If you're outputting information you don't want to, that implies there are some couts that you want to get rid of. Do any of the ones on lines 61, 62, 66, 67, and 69 seem like ones you'd want to get rid of?

-Albatross
Thank you so much Albatross I removed lines 40, 55, 56, 61, 62, 66, 67, 69, 82, 83,and 84. And I got it work right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>

void introduction();
//Explains what the program does

void fill_array(char a[], int size, int& number_used);
//Array a[] is filled with character data from the keyboard

void delete_repeats(char a[], int& number_used);
//Function will remove all repeated characters and move the rest of the characters
//foward to fill in the gap.

void output(char a[], int& number_used);
//Outputs the contents of the array and outs the new size of the array

int main()
{
  using namespace std;
  char array[100];
  int number_used;
  introduction();
  fill_array(array, 100, number_used);
  output(array, number_used);
}

//uses iostream
void introduction()
{
  using namespace std;
  cout << "Enter a text string to test:\n";

}

//uses iostream
void fill_array(char a[], int size, int& number_used)
{
  using namespace std;
  char c;
  int index = 0;
  cout << "Please type in a sentence and then press enter.\n";
  cin.get(c);


  while (c != '\n' && index < size) {
    a[index] = c;
    cin.get(c);
    index++;
  }
  number_used = index;


  for (int i = 0; i < number_used; i++) {
    for (int j = i + 1; j < number_used; j++) {
      if (a[i] == a[j]) {
	cout << a[i] << " duplicates found at "
	     << i << " and " << j <<std::endl;
	number_used=number_used-1;
	for (int k = j; k < number_used; k++)
	  a[k] = a[k + 1];
	a[number_used] = '\0';

	--i; break;
      }
    }

  }

}



//uses iostream
void output(char a[], int& number_used)
{
  using namespace std;
  cout << "The new sentence without the repeated letters is:\n";
  for (int i = 0; i < number_used; i++) {
    cout << a[i];
  }


  cin.ignore();
  cin.get();
}


Now I have to clean it up and put the namespace on the outside.

Thanks again
Last edited on
Topic archived. No new replies allowed.