I don't understand why my code isn't working. The commented out code would remove the wrong characters and not remove any spaces, but the current delete_repeats function is giving me an error of: `line 49: expected initializer before numeric constant.
Can anyone help me?`
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
void fill_array(char a[], int size, int& number_used);
void delete_repeats(char a[], int& number_used);
void sentences_output(char a[], int& number_used);
int main()
{
char sentences[100];
int used=0;
fill_array(sentences, 100, used);
delete_repeats(sentences, used);
sentences_output(sentences, used);
return 0;
}
void fill_array(char a[], int size, int& number_used)
{
char c;
int index = 0;
cout<<"Enter your sentence or sentences and press enter. \n"
<< "The max number of characters is 100.\n";
cin.get(c);
while((c != '\n')&&(index < size))
{
a[index]=c;
cin.get(c);
index++;
}
number_used = index;
cout<<"Your original array size was "<<number_used<<endl;
return;
}
void delete_repeats(char a[], int& number_used)
48{
int counter = 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 - 1; k++)
{
a[k]= a[k + 1];
}
// } wrong place; j-- and size-- only if a duplicate
j--;
number_used--;
} // moved brace here
cout << "size = " << number_used;
cout << endl;
}
}
return;
}
/*
void delete_repeats(char a[], int& number_used)
{
int location = 0;
int target = 0;
int change;
for(location = 0; location < number_used; location++)
{
for(target = 0; target<number_used; target++)
{
change = target;
if(a[location] == a[target])
{
a[target] = a[change+(target - location)];
}
}
}
return;
}
*/
void sentences_output(char a[], int& number_used)
{
cout<<"The new sentence is \n";
for(int i = 0; i < number_used; i++)
{
cout<<a[i]<<endl;
}
cout<<"The size of the new array is \n";
cout<<number_used<<endl;
return;
}
The commented out delete_repeats was the second one I came up with and the first delete_repeats was from an example I found, but neither is working.
Thanks everyone! Here is my corrected code:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
void fill_array(char a[], int size, int& number_used);
void delete_repeats(char a[], int& number_used);
void sentences_output(char a[], int& number_used);
int main()
{
char sentences[100];
int used=0;
fill_array(sentences, 100, used);
delete_repeats(sentences, used);
sentences_output(sentences, used);
return 0;
}
void fill_array(char a[], int size, int& number_used)
{
char c;
int index = 0;
cout<<"Enter your sentence or sentences and press enter. \n"
<< "The max number of characters is 100.\n";
cin.get(c);
while((c != '\n')&&(index < size))
{
a[index]=c;
cin.get(c);
index++;
}
number_used = index;
cout<<"Your original array size was "<<number_used<<endl;
return;
}
void delete_repeats(char a[], int& number_used)
{
int counter = 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 - 1; k++)
{
a[k]= a[k + 1];
}
j--;
number_used--;
}
}
}
return;
}
void sentences_output(char a[], int& number_used)
{
cout<<"The new sentence is \n";
for(int i = 0; i < number_used; i++)
{
cout<<a[i]<<" ";
}
cout<<"\n";
cout<<"The size of the new array is \n";
cout<<number_used<<endl;
return;
}
I removed the commented out delete_repeats function and edited the new one as well as changed the output function.