我不明白为什么我的代码无效。注释掉的代码将删除错误的字符而不删除任何空格,但是当前的delete_repeats函数给出了一个错误:`第49行:数字常量之前的预期初始化程序。
任何人都可以帮助我吗?`
#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;
}
注释掉的delete_repeats是我提出的第二个,第一个delete_repeats来自我找到的一个例子,但两个都没有用。
谢谢大家!这是我更正后的代码:
#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;
}
我删除了注释掉的delete_repeats函数并编辑了新函数并更改了输出函数。
答案 0 :(得分:0)
您是否在实际代码中包含了行号!?这会引起你引用的错误。