-3
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();
   }

为什么下面的代码没有给出想要的输出?

4

2 回答 2

7

因为你有一个错误。

于 2010-05-01T10:00:03.807 回答
0
#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 应该是浮点数。该程序现在运行良好。(如果我所做的更改是错误的,请原谅我。我没有编码经验。我只是试图摆脱那个错误!)

于 2014-07-30T18:56:02.243 回答