c ++中的变量不会相乘

时间:2018-02-12 01:06:58

标签: c++

我在做c ++作业。 在这个项目中,我需要知道出售股票所得的金额 我通过sell_price乘以股票得到了这一点。但是由于某种原因,当我运行代码时,我得到的结果只是我输入的数字作为售价。为什么卖价不会乘以股票数量? 提前谢谢。

// ConsoleApplication9.cpp:定义控制台应用程序的入口点。     //

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
    double stocks ;
    double purchase_price ;
    double selling_price ;

    cout << "Enter number of stocks sold:";
    cin >> stocks ;

    cout << "Enter purchase price:";
    cin >> purchase_price ;

    cout << "Enter selling price:";
    cin >> selling_price ;

    double invested = purchase_price * stocks + (purchase_price * 0.015);
    double total_service_charges = ((purchase_price + selling_price)* (.015 * stocks));

    cout << "Total money invested =" << invested << endl;
    cout << "total service charges=" << total_service_charges << endl;
    cout << "Amount received from selling stocks=" << (selling_price*stocks);

        if (purchase_price < selling_price)
        {cout << "Amount gained=" << selling_price - purchase_price;}
        else if (purchase_price > selling_price)
        {
            cout << "Amount lost=" << purchase_price - selling_price;
        }

        else if (purchase_price = selling_price)
        {
            cout << "Amount gained=" << purchase_price - selling_price<< endl;
        }


    return 0;
}

1 个答案:

答案 0 :(得分:0)

else if (purchase_price = selling_price)

您使用了=(赋值)而不是==(相等比较。)您想要的是:

else if (purchase_price == selling_price)

注意编译器警告。在这种情况下,编译器应该警告您在条件表达式中进行了赋值。