C++ 将int转换为双C++;

C++ 将int转换为双C++;,c++,windows,casting,C++,Windows,Casting,我是编程新手,这可能是一个显而易见的问题,尽管我一辈子都不明白为什么我的程序没有作为替身回归 我想写一个股票程序,包括股票的股票,价格的整美元部分和分数部分。分数部分作为两个int值输入,并包含一个包含3个int值的函数定义。该函数以双精度返回价格 #include <iostream> using namespace std; int price(int, int, int); int main() { int dollars, numerator, denominat

我是编程新手,这可能是一个显而易见的问题,尽管我一辈子都不明白为什么我的程序没有作为替身回归


我想写一个股票程序,包括股票的股票,价格的整美元部分和分数部分。分数部分作为两个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);
}
#包括
使用名称空间std;
整数价格(整数,整数,整数);
int main()
{
整数美元、分子、分母、价格1、股份;
查尔安斯;
做
{
coutnumerator>>分母;
沙发;

cout这是因为您的变量是int类型的。因此您正在失去精度

将int返回类型和变量更改为double

#include <iostream>
using namespace std;
double price(double, double, double);
int main()
{
  double 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;
}
double price(double dollars, double numerator, double denominator)
{
  return dollars + numerator/denominator;
}
#包括
使用名称空间std;
双倍价格(双倍,双倍,双倍);
int main()
{
双美元,分子,分母,价格1,股票;
查尔安斯;
做
{
coutnumerator>>分母;
沙发;

cout这是因为您的变量是int类型的。因此您正在失去精度

将int返回类型和变量更改为double

#include <iostream>
using namespace std;
double price(double, double, double);
int main()
{
  double 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;
}
double price(double dollars, double numerator, double denominator)
{
  return dollars + numerator/denominator;
}
#包括
使用名称空间std;
双倍价格(双倍,双倍,双倍);
int main()
{
双美元,分子,分母,价格1,股票;
查尔安斯;
做
{
coutnumerator>>分母;
沙发;

cout这是因为您正在返回一个
int
。这将修复它

double price (int dollars, int numerator, int denominator)
{ 
    return dollars + (double) numerator / denominator;
}

这是因为您正在返回一个
int
。这将修复它

double price (int dollars, int numerator, int denominator)
{ 
    return dollars + (double) numerator / denominator;
}

“该函数以双精度形式返回价格。”嗯,不;毕竟,您的返回类型是
int
。Gack。没有人再使用分数进行股票价格计算了。它都是十进制的。“该函数以双精度形式返回价格。”嗯,不;毕竟,你有
int
作为返回类型。Gack。再也没有人用分数来计算股票价格了。它都是十进制的。我可以把它们都当作双精度的,但我的赋值说明用户应该输入3个int值,并在函数定义中包含3个int参数。读了这篇文章,我假设我必须将ac_和int转换为一个双精度,尽管它似乎不起作用。我试过Wallyk的建议(双精度)从Num/Denom开始,虽然它没有将我的答案转换为double,但您可以做的是返回double。函数定义似乎没有指定返回类型。因此,请使用double而不是int。尝试静态转换或转换为double,但函数返回的必须是double。您的赋值有一些奇怪的要求:(Thx.反馈帮了大忙!现在明白了!我可以把它们都放在双精度上,但我的作业说明用户应该输入3个int值,并在函数定义中包含3个int参数。读到这篇文章,我假设我必须将stac_和int转换为双精度,尽管它似乎不起作用。我尝试了Wallyk的su推杆运动(双人)从Num/Denom开始,虽然它没有将我的答案转换为double,但您可以做的是返回double。函数定义似乎没有指定返回类型。因此,请使用double而不是int。尝试静态转换或转换为double,但函数返回的必须是double。您的赋值有一些奇怪的要求:(Thx.反馈帮助了很多人!现在就知道了!