problem in float function of percntage

hi guys,, below is the code of a program using classes and functions ,, my float function doesn't show answer in fractions ,, show something else,, anybody please sort it out please..

#include <iostream>

using namespace std;
class khan{
public:
void print(){
cout<<"welcome to the online Addition System!!!" <<"\n" << endl;
}
int add(int x, int y, int z){
a = x+y+z;
return a;
}
float percentage(){
float total = 300;
float per = 0.0;
per = a*100.0/total;
return per;

}
private:
int a;



};

int main()
{
khan kk;
kk.print();
int a,b,c;
cout<<"Enter Value for Maths" <<endl;
cin>> a;
cout<<"Enter Value for Science" <<endl;
cin>>b;
cout<<"Enter Value for English" <<endl;
cin>> c;
khan k;
k.add(a,b,c);
cout << "The sum of your marks is = "<< k.add(a,b,c) << endl;
khan kkk;
kkk.percentage();
cout<<"the percentage is = " << kkk.percentage();
return 0;
}
per =float( a)*100.0/total;
Last edited on
the member a of kkk is uninitialized.

use kk only and remove the other k's
Double post with double accounts?
http://www.cplusplus.com/forum/beginner/103230/
Hi coder7777 and evey1 if you peoples kindly sort it out please,,,,
i did this way now but it gives me percentage equal to 895576 every time, dont know why...


#include <iostream>

using namespace std;
class khan{

public:

void print(){
cout<<"welcome to the online Addition System!!!" <<"\n" << endl;
}
int add(int x, int y, int z){
a = x+y+z;
return a;
}
float percentage(){
float total = 300.0;
float per = 0.0;
per =a*100.0/total;
return per;
}
private:
int a;
};

int main()
{
khan kk;
kk.print();
int a,b,c;
cout<<"Enter Value for Maths" <<endl;
cin>> a;
cout<<"Enter Value for Science" <<endl;
cin>>b;
cout<<"Enter Value for English" <<endl;
cin>> c;
khan k;
k.add(a,b,c);
cout << "The sum of your marks is = "<< k.add(a,b,c) << endl;
khan kkk;
kkk.percentage();
cout<<"the percentage is = " << kkk.percentage();
return 0;
}
because kk and kkk are completely different objects.

kkk knows absolutely nothing about the calculation a 'a' done in kk.

So when you do:
kkk.percentage()
it (kkk) using a value for 'a' that you haven't set!
Last edited on
Thank you guys.. Its been so nice of you,,, its solved now,,

#include <iostream>

using namespace std;
class khan{

public:

void print(){
cout<<"welcome to the online Addition System!!!" <<"\n" << endl;
}
float add(float x, float y, float z){
a = x+y+z;
return a;
}
float percentage(){
float total = 300;
float per = 0.0;
per =a*100.0/total;
return per;
}
private:
float a;
};

int main()
{
khan kk;
kk.print();
float a,b,c;
cout<<"Enter Value for Maths" <<endl;
cin>> a;
cout<<"Enter Value for Science" <<endl;
cin>>b;
cout<<"Enter Value for English" <<endl;
cin>> c;
kk.add(a,b,c);
cout << "The sum of your marks is = "<< kk.add(a,b,c) << endl;
kk.percentage();
cout<<"the percentage is = " << kk.percentage();
return 0;
}
Topic archived. No new replies allowed.