最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

class - I have a calculation problem about c++ output - Stack Overflow

programmeradmin0浏览0评论
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
  • 2 C++ isn't a spreadsheet. float dogum_orani = dogum / nufus; doesn't do anything useful. When the object is created, the value of dogma_orani gets set with whatever uninitialized values dogum and nufus 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, for olum_orani. – Pete Becker Commented Mar 4 at 22:30
  • Don't mix integer and floating point calculations. @PeteBecker even worse, float dogum_orani = dogum / nufus; reads from unitialized variable nufus and the code has undefined behavior. In other words the whole program is broken. – Pepijn Kramer Commented Mar 5 at 6:55
  • 1 This question is similar to: Result of calculation is not changed when one of the variables is updated later. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – pptaszni Commented Mar 5 at 12:22
  • And another dupe for your integer division with floating point result: stackoverflow/a/6727019/4165552 – pptaszni Commented Mar 5 at 12:25
  • Or this: stackoverflow/a/11943536/4165552 – pptaszni Commented Mar 5 at 12:27
Add a comment  | 

1 Answer 1

Reset to default -2

your 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;
}
发布评论

评论列表(0)

  1. 暂无评论