class YerlesimBirimi {
public:
string yerlesim;
int nufus;
int dogum;
int olum;
float dogum_orani = dogum / nufus;
float olum_orani = olum / nufus;
void isimAta() {
cout << "Yerlesim yerini giriniz:";
cin >> yerlesim;
}
void nufusAta() {
cout << "Nufusu giriniz:";
cin >> nufus;
}
void dogumAta() {
cout << "Dogum sayisini giriniz";
cin >> dogum;
}
void olumAta() {
cout << "Olum sayisini giriniz:";
cin >> olum;
}
float dogumorani() {
return dogum_orani;
}
float olumorani() {
return olum_orani;
}
};
int main()
{
YerlesimBirimi x;
x.isimAta();
x.nufusAta();
cout << "Dogum sayisini giriniz: ";
cin>>x.dogum;
cout << "dogum***************" << x.dogum<<endl;
cout << "Olum sayisini giriniz: ";
cin >> x.olum;
cout << x.yerlesim << " --- " << " Dogum orani: " <<x.dogumorani()<<endl;
cout << x.yerlesim << " --- " << " Olum orani: " << x.olumorani();
return 0;
}
If I add dogumorani()
and olumorani()
as void
and cout
the dogum_orani
and olum_orani
, I get the outputs as 0, but if I do float
and return datas
I get 1.
I want to get the dogum_orani
and olum_orani
as a float
number, but every time I run this code I get these float
datas as 1.
That is my output:
It must be like this:
class YerlesimBirimi {
public:
string yerlesim;
int nufus;
int dogum;
int olum;
float dogum_orani = dogum / nufus;
float olum_orani = olum / nufus;
void isimAta() {
cout << "Yerlesim yerini giriniz:";
cin >> yerlesim;
}
void nufusAta() {
cout << "Nufusu giriniz:";
cin >> nufus;
}
void dogumAta() {
cout << "Dogum sayisini giriniz";
cin >> dogum;
}
void olumAta() {
cout << "Olum sayisini giriniz:";
cin >> olum;
}
float dogumorani() {
return dogum_orani;
}
float olumorani() {
return olum_orani;
}
};
int main()
{
YerlesimBirimi x;
x.isimAta();
x.nufusAta();
cout << "Dogum sayisini giriniz: ";
cin>>x.dogum;
cout << "dogum***************" << x.dogum<<endl;
cout << "Olum sayisini giriniz: ";
cin >> x.olum;
cout << x.yerlesim << " --- " << " Dogum orani: " <<x.dogumorani()<<endl;
cout << x.yerlesim << " --- " << " Olum orani: " << x.olumorani();
return 0;
}
If I add dogumorani()
and olumorani()
as void
and cout
the dogum_orani
and olum_orani
, I get the outputs as 0, but if I do float
and return datas
I get 1.
I want to get the dogum_orani
and olum_orani
as a float
number, but every time I run this code I get these float
datas as 1.
That is my output:
It must be like this:
Share Improve this question edited Mar 5 at 20:04 Remy Lebeau 601k36 gold badges508 silver badges851 bronze badges asked Mar 4 at 22:23 İsmail Eren Uruçİsmail Eren Uruç 293 bronze badges 5 |1 Answer
Reset to default -2your issue is how to initialize dogum_orani and olum_orani in your class . dogum and nufus are both integers , dogum / nufus performs integer division , which means it truncates the decimal part and the result will be 0 for vaules where dogum < nufus also they are initialized in order of their declaration here is how to fix the code :
#include <iostream>
using namespace std;
class YerlesimBirimi {
public:
string yerlesim;
int nufus;
int dogum;
int olum;
void isimAta() {
cout << "Yerlesim yerini giriniz: ";
cin >> yerlesim;
}
void nufusAta() {
cout << "Nufusu giriniz: ";
cin >> nufus;
}
void dogumAta() {
cout << "Dogum sayisini giriniz: ";
cin >> dogum;
}
void olumAta() {
cout << "Olum sayisini giriniz: ";
cin >> olum;
}
float dogumorani() {
if (nufus == 0) return 0; // Prevent division by zero
return (float)dogum / nufus;
}
float olumorani() {
if (nufus == 0) return 0; // Prevent division by zero
return (float)olum / nufus;
}
};
int main()
{
YerlesimBirimi x;
x.isimAta();
x.nufusAta();
x.dogumAta();
x.olumAta();
cout << x.yerlesim << " --- " << " Dogum orani: " << x.dogumorani() << endl;
cout << x.yerlesim << " --- " << " Olum orani: " << x.olumorani() << endl;
return 0;
}
float dogum_orani = dogum / nufus;
doesn't do anything useful. When the object is created, the value ofdogma_orani
gets set with whatever uninitialized valuesdogum
andnufus
have (formally, that's undefined behavior, but in practice it will just use whatever values those two members have). After that its value doesn't change, because nothing in the program changes it. Same thing, of course, forolum_orani
. – Pete Becker Commented Mar 4 at 22:30float dogum_orani = dogum / nufus;
reads from unitialized variablenufus
and the code has undefined behavior. In other words the whole program is broken. – Pepijn Kramer Commented Mar 5 at 6:55