我是編程新手,這可能是一個明顯的問題,但我不能爲我的生活弄清楚爲什麼我的程序沒有返回作爲一個雙。類型鑄造int到雙C++
我想寫一個股票計劃,需要股票的股票,價格的整個美元部分和分數部分。小數部分將作爲兩個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);
}
「該函數返回雙倍價格。」嗯,不,畢竟,你有'int'作爲返回類型。 – GManNickG
Gack。沒有人再用股票來做股票價格。這都是小數。 –