PDA

View Full Version : مبتدی: خطا در یک برنامه



smemamian
شنبه 25 آذر 1391, 00:02 صبح
سلام دوستان

هرکاری کردم برنامه زیر باز کامپایلر ایراد گرفت ! نتونستم بفهمم مشکل از کجاست :

#include <cstdlib>
#include <iostream>

using namespace std;
enum yn{no , yes};
enum color{red,yellow,green,orange};
void out (enum yn x);
char *c[] = {
"red","yellow","green","orange"};
class fruit{
public:
enum yn annual ;
enum yn perennial;
enum yn tree ;
enum yn tropical;
enum color clr ;
char name [40];
};

class apple:public fruit {
enum yn cooking;
enum yn crunchy;
enum yn eating;
public:
void seta(char *n,enum color c, enum yn ck,enum yn crchy,enum yn e);
void show();
};
class orange:public fruit{
enum yn juice;
enum yn sour;
enum yn eating;
public:
void seto(char *n,enum color c,enum yn j,enum yn sr,enum yn e);
void show();
};

void apple::seta(char *n, enum color c,enum yn ck, enum yn crchy,enum yn e)
{
strcpy(name,n);
annual=no ;
perennial = yes;
tree = yes;
tropical = no ;
clr = c;
cooking =ck ;
crunchy = crchy;
eating = e;
}

void orange::seto(char *n,enum color c, enum yn j,enum yn sr,enum yn e)
{
strcpy(name,n);
annual = no ;
perennial = yes;
tree = yes ;
tropical = yes ;
clr = c ;
juice = j ;
sour = sr ;
eating = e ;
}
void apple::show()
{
cout << name << "apple is :" << "\n" ;
cout << "annual:" ; out(annual);
cout << "perennial:" ; out(perennial);
cout << "tree:" ; out(tree);
cout << "tropical:" ; out(tropical);
cout << "color: " << c[clr] << "\n" ;
cout << "good for cooking: " ; out(cooking);
cout << "crunchy: " ;out(crunchy);
cout << "good for eating: "; out(eating);
cout << "\n" ;
}
void orange::show()
{
cout << name << "orange is :" << "\n" ;
cout << "annual:" ; out(annual);
cout << "perennial:" ; out(perennial);
cout << "tree:" ; out(tree);
cout << "tropical:" ; out(tropical);
cout << "color: " << c[clr] << "\n" ;
cout << "good for juice: " ; out(juice);
cout << "sour: " ;out(sour);
cout << "good for eating: "; out(eating);
cout << "\n" ;
}
void out(enum yn x)
{
if(x==no) cout<< "no \n" ;
else cout << "yes \n";

}

int main(int argc, char *argv[])
{
apple a,aa;
orange o,oo;
a.seta("red delicious", red, no , yes, yes);
aa.seta("honathan", red, yes,no, yes);
o.seto("navel", orange, no,no,yes);
oo.seto("valencia", orange, yes,yes,no);
a.show();
aa.show();
o.show();
oo.show();
system("PAUSE");
return EXIT_SUCCESS;
}

H_G_G_I
شنبه 25 آذر 1391, 09:29 صبح
همش بخاطر اینه که همه چیز رو بد جور نام گزاری کردین و ...
باید کدهاتون نظم داشته باشه تا بفهمید چی به چیه و چی نوشتید .
مثلا یه کلاس orange داری و یه eunm که اونم orange داره .
در ضمن لازم نیست هر دفعه بنویسی enum .
من یکمی از کدت رو تر تمیز کردم بقیش به عهده خودت .

enum EYseNo{ YES , NO};
enum EColor{ Red, Yellow, Green, Orange};

class CFruit
{
public:
EYseNo annual ;
EYseNo perennial;
EYseNo tree ;
EYseNo tropical;
EColor clr ;
char name [40];
};

class CApple : public CFruit
{
EYseNo cooking;
EYseNo crunchy;
EYseNo eating;
public:
void seta(char* _n, EColor _c, EYseNo _ck, EYseNo _crchy, EYseNo _e);
void show();
};

class COrange : public CFruit
{
EYseNo juice;
EYseNo sour;
EYseNo eating;
public:
void seto(char* _n, EColor _c, EYseNo _j, EYseNo _sr, EYseNo _e);
void show();
};

omidshaman
شنبه 25 آذر 1391, 10:19 صبح
strcpy(name,n);
هدرشو نزاشتی!

#include<cstring>

smemamian
شنبه 25 آذر 1391, 20:27 عصر
ممنونم و ارورهای عجیبی دیده میشه !


1 IntelliSense: expected a ';' c:\users\smemamian\documents\visual studio 2010\projects\hg\hg\fd.cpp 99 12 hg
2 IntelliSense: identifier "o" is undefined c:\users\smemamian\documents\visual studio 2010\projects\hg\hg\fd.cpp 102 5 hg
3 IntelliSense: identifier "oo" is undefined c:\users\smemamian\documents\visual studio 2010\projects\hg\hg\fd.cpp 103 5 hg