2010-05-01 73 views
-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(); 
    } 

爲什麼以下代碼沒有得到所需要的輸出?類和對象在C++

+4

什麼是你的問題?你爲什麼使用turbo C++? – 2010-05-01 09:41:52

+0

渦輪C++是編譯器 – anurag18294 2010-05-01 09:46:57

+1

@ anurag18294:如果你希望人們把你當回事,說在一個有遠見的語氣,也許「渦輪C++的編譯器!人人都會顫抖! (我已經顫抖) – nc3b 2010-05-01 09:50:34

回答

7

因爲你的錯誤。

+0

這意味着我的代碼是完全正確的 – anurag18294 2010-05-01 10:07:30

+1

歡迎陌生人,這也是非鏡面的宇宙。 – shoosh 2010-05-01 10:42:37

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應該是float。該程序現在工作正常。 (請原諒我,如果我所做的更改是錯誤的。我不跟編碼經驗。我只是試圖擺脫錯誤的!)