Arrays and Functions

I am writing a program to read a sentence into an array and then delete all repeated letters and spaces and then print out the new sentence. after a repeated letter is deleted the remaining letters are moved up to fill in the gap. The output also includes the size of the new array. The problem is my output only shows four letters, sometimes the first 4 letters of the sentence and sometimes 'tttt' and new array size as 4, no matter what I type in. Thank you in advance for any assistance provided.


//code

#include <iostream>
#include <cstdlib>

using namespace std;

void fill_array(char a[], int size, int& number_used);
//Array a[] is filled with 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()
{
char array[100];
int number_used;

cout << "This program will will ask the user to type in a sentence\n"
<< "and then will delete all repeated characters of the sentence.\n"
<< "The program will then output the new sentence with all repeated\n"
<< "letters deleted\n";

fill_array(array, 100, number_used);
delete_repeats(array, number_used);
output(array, number_used);

system("PAUSE");
return 0;
}



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

//uses iostream
void delete_repeats(char a[], int& number_used)
{
for (int i = 0; i < number_used; i++)
{
for (int j = i + 1; j < number_used; j++)
{
if (a[i] == a[j])
{
for (int k = j; k < number_used; k++)
a[k] = a[k + 1];
}
}
}
number_used = sizeof a;
}

//uses iostream
void output(char a[], int& number_used)
{
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;
}




//output
Please type in a sentence and then press enter.
hello
The new sentence without the repeated letters is:
xhel
The size of the new array is 4
Press any key to continue . . .

Hello!

There are two problems with your code above. First, inside fill_array(), in the while loop, you increment index before using it, causing a[0] to be never assigned a character.
Second, within delete_repeats(), you assign sizeof a to number_used; Within a function, an array parameter is always seen as a pointer to its first element (remember, you cannot pass c-style arrays to functions by value) and therefore sizeof a is always equal to the size of a pointer on your architecture, which is 32 bits in this case.
To correct the former problem, just swap the first 2 lines of the while loop; to correct the latter, remove that assignment and decrement number_used each time you move the string up.

Bye
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
#include <iostream>
#include <cstdlib>

using namespace std;

void fill_array(char a[], int size, int& number_used);
//Array a[] is filled with 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()
{
char array[100];
int number_used;

cout << "This program will will ask the user to type in a sentence\n"
     << "and then will delete all repeated characters of the sentence.\n"
     << "The program will then output the new sentence with all repeated\n"
     << "letters deleted\n";

fill_array(array, 100, number_used);
delete_repeats(array, number_used);
output(array, number_used);

    system("PAUSE");
    return 0;
}



//uses iostream
void fill_array(char a[], int size, int& number_used)
{
     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;
           index++;
           cin.get(c);
     }
           number_used = index;
}

//uses iostream
void delete_repeats(char a[], int& number_used)
{
     for (int i = 0; i < number_used; i++)
     {
         for (int j = i + 1; j < number_used; j++)
         {
                  if (a[i] == a[j])
                  {
                   for (int k = j; k < number_used; k++)
                   a[k] = a[k + 1];
                  }
         }
     }
     number_used = sizeof a;
}

//uses iostream
void output(char a[], int& number_used)
{
     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;
}



i swapped the first two lines of the loop.

but i don't understand how to fix "number_used" part...
Last edited on
hey nvm i think i got it. hahaha
thank you soo much<3333
Topic archived. No new replies allowed.