我是编程新手,这可能是一个显而易见的问题,尽管我一生都无法弄清楚为什么我的程序没有以双重身份返回。
我想写一个股票程序,接收股票、价格的整美元部分和小数部分。小数部分将作为两个 int 值输入,并包含一个具有 3 个 int 值的函数定义。该函数将价格作为双精度值返回。
#include <iostream>
using namespace std;
int price(int, int, int);
int main()
{
int dollars, numerator, denominator, price1, shares;
char ans;
do
{
cout<<"Enter the stock price and the number of shares.\n";
cout<<"Enter the price and integers: Dollars, numerator, denominator\n";
cin>>dollars>>numerator>>denominator;
cout<<"Enter the number of shares held\n";
cin>>shares;
cout<<shares;
price1 = price(dollars,numerator,denominator);
cout<<" shares of stock with market price of ";
cout<< dollars << " " << numerator<<'/'<<denominator<<endl;
cout<<"have a value of " << shares * price1<<endl;
cout<<"Enter either Y/y to continue";
cin>>ans;
}while (ans == 'Y' || ans == 'y');
system("pause");
return 0;
}
int price(int dollars, int numerator, int denominator)
{
return dollars + numerator/static_cast<double>(denominator);
}