There seems to be a bit of confusion in the structure of the program.
Starting with main(), current version:
1 2 3 4 5 6 7 8 9
|
int main()
{
int i;
reshte myreshte[n];
for (int i=0;i<50;i++)
myreshte[i].input();
myreshte[i].calc();
myreshte[i].show();
}
|
There's no need to create an array of your class and process each of them. Just create a single instance:
1 2 3 4 5 6 7 8 9
|
int main()
{
reshte myreshte;
myreshte.input();
myreshte.calc();
myreshte.show();
return 0;
}
|
Then there are a few errors, in addition to the compiler error.
You need a place to store the string input by the user, and a place to store the character input by the user.
You will also need a char pointer in order to examine each character. (doesn't have to be a class member, it could be a local variable in function calc).
Anyway, if we use meaningful data names we have this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
const int SIZE = 50;
class reshte
{
char str[SIZE];
char *ptr;
char ch;
int count;
public:
reshte();
void input();
void calc();
void show();
};
|
In the constructor, we need to initialise the counter, and also set the pointer to contain a valid address, in this case, the address of the character string.
1 2 3 4 5
|
reshte::reshte()
{
count = 0;
ptr = str;
}
|
Function input() could look like this. Note the use of the defined constant again:
1 2 3 4 5 6 7
|
void reshte::input()
{
cout << "Enter string: ";
cin.get(str,SIZE);
cout << "Enter character: ";
cin>>ch;
}
|
And the part which does the real work, function calc(). Current version:
1 2 3 4 5 6 7 8 9 10 11
|
void reshte::calc()
{
while (*s)
{
if (*s==x)
{
c++;
s++;
}
}
}
|
Well, there's an ambiguity there, We have two different variables, each named 's' (the cause of the compiler error). Which one is this meant to refer to? There's also a problem that when the
if
condition is false, the values of c and s are not incremented, thus the next time around the same values will be tested again, and an infinite loop results.