The number of repeated characters in a string

Hi
It's a program for ''The number of repeated characters in a string''
I wrote this program but It doesn't compile .Where Is problem?
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
#include <iostream>
using namespace std;
const int n=50;
 class reshte
 {

	 char s[50];
	 char *s;
	 char x;
	 int c;
 public:
	 void input();
	 void calc();
	 reshte();
	 void show();
 };
 void reshte::input()
 {
	 cin.get(s,50);
	 cin>>x;
 }
 reshte::reshte()
 {
	 c=0;
 }
 void reshte::calc()
 {
	 while (*s)
	 {
		 if (*s==x)
		 {
			 c++;
			 s++;
		 }
	 }
 }
 void reshte:: show()
 {
	 cout<<c;
 }
 int main()
 {
	 int i;
	 reshte myreshte[n];
	 for (int i=0;i<50;i++)
	 myreshte[i].input();
	 myreshte[i].calc();
	 myreshte[i].show();
 }

Tanx
Doesn't your compiler issue any messages? It should tell you where/what the problem is.
It gives two errors:

1
2
3
Error	2	error C2105: '++' needs l-value	

Error	1	error C2372: 'reshte::s' : redefinition; different types of indirection	
Lines 7 and 8 try to declare two variables with the same name.
*s(pointer) and s Are same name .Where should I change ?I didn't notice you mean.
Did Nobody see my question???
You need to either remove one of them or change one of their names. I think you might only need one of them anyway.
I remove line 8 but It gives this error:
prog.cpp: In member function ‘void reshte::calc()’:
prog.cpp:32: error: lvalue required as increment operand
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.
Last edited on
Topic archived. No new replies allowed.