PDA

View Full Version : تعداد تکرار یک کاراکتر در رشته



alirah84
چهارشنبه 24 آبان 1391, 13:56 عصر
سلام
من این برنامه رو در رابطه با تعداد تکرار یک کاراکتر در رشته رو برای 50 شیء نوشتم ولی نمیدونم اشکال از کجاشه که یک ارور میده .دوستان اگه میشه یه کمکی کنند .
این اروری که میده:
Line 8 (http://codepad.org/thqfmUM2#line-8): error: declaration of 'char* reshte::s' compilation terminated due to -Wfatal-errors.
در خط 8 مگه نباید پوینتر S رو معرفی کرد؟ .یکی بهم گفت باید یا خط 7 رو پاک کنی یا خط 8.

این برنامه :

#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();
}
ممنون

omidshaman
پنج شنبه 25 آبان 1391, 11:28 صبح
نیازی به اون حلقه ی For اخر و پوینتر s و خط 22 تا 25 نیست

#include <iostream>
using namespace std;
const int n=50;
class reshte
{
char s[n];
int c;
char x;
public:
void input();
void calc();
void show();
};
void reshte::input()
{
cin.get(s,n);
cin>>x;
}
void reshte::calc()
{
int i=0;
c=0;
while (s[i])
{
if (s[i++]==x)
C++‎;
}
}
void reshte:: show()
{
cout<<c;
}
int main()
{
reshte r;
r.input();
r.calc();
r.show();
}

alirah84
پنج شنبه 25 آبان 1391, 20:59 عصر
اخر این برنامه رو باید با پوینتر بنویسم.
اگه بخوام با پوینتر بنویسم چطوری میشه؟

omidshaman
جمعه 26 آبان 1391, 13:39 عصر
فرقی نداره خوب مثلا میشه این جوری

#include <iostream>
using namespace std;
const int n=50;
class reshte
{
char *s;
int c;
char x;
public:
void input();
void calc();
void show();
};
void reshte::input()
{
s=new char[n];
cin.get(s,n);
cin>>x;
}
void reshte::calc()
{
c=0;
while (*s)
{
if (*s++==x)
c++;
}
}
void reshte:: show()
{
cout<<c;
}
int main()
{
reshte *r;
r->input();
r->calc();
r->show();
}