C++ 中的类和对象

发布于 2024-08-30 23:25:19 字数 731 浏览 10 评论 0原文

class anurag
{
private:
int rollno;
char name[50];
int marks;
float percen;
void percentage(int num)   
{
 percen=(num/500)*100;

}
public:
void getdata(void)
{
cout<<"\n\nEnter the name of the student:";
gets(name);
cout<<"\n\nEnter the roll no: and the marks:";
cin>>rollno>>marks;
percentage(marks);
}
void display(void)
{
cout<<"\n\nThe name of the student is:";
cout.write(name,50);
cout<<"\n\nThe roll no: of the student is:";
cout<<rollno;
cout<<"\n\n The marks obtained is:"<<marks;
cout<<"\n\nThe percentage is:"<<percen;
}};
   void main()
   {
clrscr();
anurag F;
F.getdata();
F.display();
getch();
   }

为什么以下代码没有给出所需的输出?

class anurag
{
private:
int rollno;
char name[50];
int marks;
float percen;
void percentage(int num)   
{
 percen=(num/500)*100;

}
public:
void getdata(void)
{
cout<<"\n\nEnter the name of the student:";
gets(name);
cout<<"\n\nEnter the roll no: and the marks:";
cin>>rollno>>marks;
percentage(marks);
}
void display(void)
{
cout<<"\n\nThe name of the student is:";
cout.write(name,50);
cout<<"\n\nThe roll no: of the student is:";
cout<<rollno;
cout<<"\n\n The marks obtained is:"<<marks;
cout<<"\n\nThe percentage is:"<<percen;
}};
   void main()
   {
clrscr();
anurag F;
F.getdata();
F.display();
getch();
   }

why the following code is not giving the desired output?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄断弥空 2024-09-06 23:25:19

因为你有一个错误。

Because you have a bug.

愁以何悠 2024-09-06 23:25:19
#include<iostream>
#include<conio.h>
using namespace std;
class anurag
{
private:
int rollno;
string name;
int marks;
float percen;

public:
void percentage(float num)
{
  percen=(num/500)*100;

}
public:
void getdata(void)
{
cout<<"\n\nEnter the name of the student:";
cin>>name;
cout<<"\n\nEnter the roll no: and the marks:";
cin>>rollno>>marks;
percentage(marks);
}

void display(void)
{
cout<<"\n\nThe name of the student is:";
cout<<name;
cout<<"\n\nThe roll no: of the student is:";
cout<<rollno;
cout<<"\n\nThe marks obtained is:"<<marks;
cout<<"\n\nThe percentage is:"<<percen<<"%";
}};
   int main()
   {
//clrscr();
anurag F;
F.getdata();
F.display();
getch();
return 0;
   }

我做了一些改变。 int num 应该是浮点数。该程序现在运行良好。
(如果我所做的更改是错误的,请原谅我。我没有编码经验。我只是想摆脱这个错误!)

#include<iostream>
#include<conio.h>
using namespace std;
class anurag
{
private:
int rollno;
string name;
int marks;
float percen;

public:
void percentage(float num)
{
  percen=(num/500)*100;

}
public:
void getdata(void)
{
cout<<"\n\nEnter the name of the student:";
cin>>name;
cout<<"\n\nEnter the roll no: and the marks:";
cin>>rollno>>marks;
percentage(marks);
}

void display(void)
{
cout<<"\n\nThe name of the student is:";
cout<<name;
cout<<"\n\nThe roll no: of the student is:";
cout<<rollno;
cout<<"\n\nThe marks obtained is:"<<marks;
cout<<"\n\nThe percentage is:"<<percen<<"%";
}};
   int main()
   {
//clrscr();
anurag F;
F.getdata();
F.display();
getch();
return 0;
   }

I made some changes. The int num should be float. The program works fine now.
(Pardon me if the changes I've made are wrong. I'm not experienced with coding. I just tried to get rid of that error!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文