运行此命令时,我得到一个疯狂的数字作为小数点。
然后,我将使用完全相同的分子和分母再次运行它,这将给我带来其他的不同。我不知道十进制值在哪里得到它正在使用的数字和分母。我希望它使用被迫在程序开始时输入的那个。

我不知道我在做什么错。任何帮助是极大的赞赏!

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

void printLogo() {
    cout << "logo here" << endl;
}

int AskFraction(int &numerator, int &denominator) {
    cout << "Enter numerator: ";
    cin >> numerator;
    cout << "Enter denominator: ";
    cin >> denominator;

    while (denominator == 0 || denominator < 0) {
        cout << "Denominator must be a number above zero." << endl;
        cout << "Enter denominator: ";
        cin >> denominator;
    }

    return numerator, denominator;
 }

float DecimalValue(float &decimalValue, int numerator, int denominator) {
if (denominator != 0 && denominator > 0) {
    decimalValue = numerator / denominator;
}

return decimalValue;
}

void PrintFraction(int numerator, int denominator) {
cout << numerator << "/" << denominator << endl;
}

int main(int numerator, int denominator, float decimalValue) {

printLogo();

AskFraction(numerator, denominator);

DecimalValue(decimalValue, numerator, denominator);
cout << "The decimal value is: " << decimalValue << endl;

PrintFraction(numerator, denominator);

system("pause");
return 0;
}

最佳答案

您的代码混合了各种概念。首先,通过引用传递变量,然后尝试返回值。使用引用传递或返回结果的值。

float DecimalValue(float &decimalValue, int numerator, int denominator) {
if (denominator != 0 && denominator > 0) {
    decimalValue = numerator / denominator;
}

return decimalValue;
}


这应该是:

void DecimalValue(float &decimalValue, int numerator, int denominator) {
if (denominator != 0 && denominator > 0) {
    decimalValue = numerator / denominator;
}
}


要么

float DecimalValue(int numerator, int denominator) {
float decimalValue; //You should initialize this to a value. Decide this based on what you want to happen when the condition of the next if is false.
if (denominator != 0 && denominator > 0) {
    decimalValue = numerator / denominator;
}

return decimalValue;
}


另外,您尝试返回两个值。如果需要返回多个结果,则需要使用引用传递。像:

int AskFraction(int &numerator, int &denominator) {
    cout << "Enter numerator: ";
    cin >> numerator;
    cout << "Enter denominator: ";
    cin >> denominator;

    while (denominator == 0 || denominator < 0) {
        cout << "Denominator must be a number above zero." << endl;
        cout << "Enter denominator: ";
        cin >> denominator;
    }

    return numerator, denominator;
 }


应该是:

void AskFraction(int &numerator, int &denominator) {
    cout << "Enter numerator: ";
    cin >> numerator;
    cout << "Enter denominator: ";
    cin >> denominator;

    while (denominator == 0 || denominator < 0) {
        cout << "Denominator must be a number above zero." << endl;
        cout << "Enter denominator: ";
        cin >> denominator;
    }
 }


更改我上面指出的内容并更正了主要参数之后的最终代码是:

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

void printLogo() {
    cout << "logo here" << endl;
}

void AskFraction(int &numerator, int &denominator) {
    cout << "Enter numerator: ";
    cin >> numerator;
    cout << "Enter denominator: ";
    cin >> denominator;

    while (denominator == 0 || denominator < 0) {
        cout << "Denominator must be a number above zero." << endl;
        cout << "Enter denominator: ";
        cin >> denominator;
    }
 }

void DecimalValue(float &decimalValue, int numerator, int denominator) {
if (denominator != 0 && denominator > 0) {
    decimalValue = (float)numerator / denominator;
}

}

void PrintFraction(int numerator, int denominator) {
cout << numerator << "/" << denominator << endl;
}

int main() {
int numerator, int denominator, float decimalValue
printLogo();

AskFraction(numerator, denominator);

DecimalValue(decimalValue, numerator, denominator);
cout << "The decimal value is: " << decimalValue << endl;

PrintFraction(numerator, denominator);

system("pause");
return 0;
}

关于c++ - C++打印给定分子和分母的小数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49459541/

10-11 22:50