C ++打印给定分子和分母的小数

时间:2018-03-23 23:21:57

标签: c++ function decimal fractions

当我运行这个时,我得到一个疯狂的小数字。 然后,我将使用完全相同的分子和分母再次运行它,它给了我一些不同的东西。我不知道decimalValue在哪里得到它正在使用的数字和分母。我想让它使用用户被迫在程序开头输入的那个。

我不知道我做错了什么。非常感谢任何帮助!

#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;
}

1 个答案:

答案 0 :(得分:1)

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

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;
}