PDA

View Full Version : مشکل در اجرای یک کلاس



A.iman
دوشنبه 28 اسفند 1391, 13:09 عصر
سلام دوستان.
من یه کلاس نوشتم که اسم و شماره و نمره 4 درسو میگیره و در یه کلاس ذخیره میکنه...
ولی یه error ای میده که اصلا برام قابل فهم نیس!
student.h

// student header file
#include <string>

class student
{
private:
double cours1,cours2,cours3,cours4,avg;
int stno;
std::string name[20];
public :
student();
void AVG();
void setname(std::string);
void setstno(int);
void print();
void setcours(double,double,double,double);
}

student .cpp :


#include<iostream>
#include<string>
#include"student.h"
#include<iomanip>


student::student()
{cours1=cours2=cours3=cours4=avg=0;
}
void student::setcours(double a,double b, double c, double d)
{cours1=a;
cours2=b;
cours3=c;
cours4=d;
}
void student::setname(std::string n)
{
name[20] = n;
}
void student::setstno(int e)
{
stno =e;

}
void student::AVG()
{
avg= (cours1+cours2+cours3+cours4)/4 ;
}
void student::print()
{
std::cout<<name<<std::setw(4)<<stno<<std::endl;
std::cout<<"cours1 :"<<cours1<<" cours2 :"<<cours2<<" cours3"<<cours3<<" cours4"<<cours4<<std::endl;
}

st1.cpp:

#include<iostream>
#include<string>
#include"student.h";
#include<iomanip>

int main()
{

int i,m,q[3];
std::string n[20];
student st;
std::cout<<"pleas enter the name of student ?"<<std::endl;
std::cin>>n[20];
st.setname( n[20]);
std::cout<<std::endl<<"pleas enter the number of student ?"<<std::endl;
std::cin>>m;
st.setstno( m);
for(i=0;i<4;i++)
{
std::cout<<"pleas enter the cours "<<i<<":"<<std::endl;
std::cin>>(q[i]);
}
st.setcours(q[0],q[1],q[2],q[3]);
st.AVG();
st.print();
}

Ananas
دوشنبه 28 اسفند 1391, 13:45 عصر
سلام.
در قسمت :
void student::setname(std::string n)
{
name[20] = n;
}

این:
name[20] = n;
یعنی چی؟

smemamian
دوشنبه 28 اسفند 1391, 13:46 عصر
برای من پیغامی نداد ! ولی چند مشکل اساسی داره !

به برنامه زیر دقت کنید :

#include <iostream>
using namespace std;
int main()
{
char name[20];
cin.getline(name,20,'\n');
cout << name << endl ;
return 0;
}

این مشکل رو برطرف کنید تا بعدی...

UfnCod3r
دوشنبه 28 اسفند 1391, 14:03 عصر
اه اقا اخه چرا ارور رو نمی زاری یه ارور گذاشتن اینقدر سخته یا ما جواب بدیم سخت تره .:عصبانی++:
این طوری خوتونم زودتر به نتیجه میرسید ما هم کمتر حرص می خوریم .:عصبانی++:
باز خوبه کد رو گذاشتی اخه بعضی ها نه کد می زارن نه ارور رو بعد می گن مشکل چیه .:قهقهه: