I wrote the code trying to serching a word in a string, for I was practicing pointer in C++.
first time when I tried this code is fine, but I wanted it to become decoupling and to practice passing pointer into function.
I then wrote some global function trying to perform the same things, but the concole kept saying things like the title.
I put two versions below, first is the version that cannot be perform.
please give me some advice or corrections about this porblem or programming style.
I post a question for the first time, if the highlightint doesn’t work please pardon me.
#include <iostream>
#include <cstring>
using namespace std;
int serching(char*a , const char* b, const int n); // repeating serching desginated word in the ariticle.
int target(char*& a, int& b); // Let users enter a word.
int main()
{
int timesA = 1; //recording the numbers of total words in article.
int timesD = 0; //recording the numbers of designated words.
const int maxlength = 10000;
char a[maxlength];
cout << "Please enter the paragraph less than " << maxlength << " characters. " << endl;
cin.getline(a, maxlength);
size_t l = strlen(a);
cout << "Total numbers of characters: " << l << endl;
char* aptr = new char [l];
for(int i = 0; i < l; i++)
{
aptr[i] = a[i];
}
int n;
char* desg;
int num = target(desg, n);
serching(aptr, desg, num);
delete[] desg;
desg = 0;
cout << "There are " << timesD << " designated words found in this paeagraph. " << endl;
delete[] aptr;
aptr = 0;
return 0;
}
int serching(char*a , const char* b, const int n)
{
int num = 0;
char* i = strstr(a, b);
while(i != 0)
{
num++;
i = strstr((i + n), b);
}
delete[] i;
i = 0;
return num;
}
int target(char*& a, int& b)
{
a = new char[b];
cout << "Please enter a word: " << endl;
cin.getline(a, 20);
b = strlen(a);
delete[] a;
a = 0;
return b;
}
Here is the original version which can be execute without problem.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int timesA = 1; //recording the numbers of total words in article.
int timesD = 0; //recording the numbers of designated words.
const int maxlength = 10000;
char a[maxlength];
cout << "Please enter the paragraph less than " << maxlength << " characters. " << endl;
cin.getline(a, maxlength);
size_t l = strlen(a);
cout << "Total numbers of characters: " << l << endl;
char* aptr = new char [l]; //pass into a dynamic array, for I'm practcing this.
for(int i = 0; i < l; i++)
{
aptr[i] = a[i];
}
int n;
char* desg = new char[n];
cout << "Please enter a word: " << endl;
cin.getline(desg, 20);
n = strlen(desg);
char* i = strstr(aptr, desg); // repeating serching the word in the string.
while(i != 0)
{
timesD++;
i = strstr((i + n), desg);
}
delete[] i;
i = 0;
delete[] desg;
desg = 0;
cout << "There are " << timesD << " designated words found in this paeagraph. " << endl;
delete[] aptr;
aptr = 0;
return 0;
}
I thought the problem might be leakinig memory or some value is too big, I have seen some posts that have same console error context as me, but still cannot figure it out.
Bevis000 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.