如何在 cout 语句后打印 void 函数?

How to print void function after cout statement?

我试图在我的值发送到函数后打印函数的输出。 cout 语句需要一个字符串,但我不确定如何从我的 reduce_fraction 函数中 return 一个字符串,同时保持数学正确。在我的 add_fraction 函数中,您会看到我只是想打印添加的分数,然后打印它正下方的减少的分数。编译器 return 没有错误,但输出仅显示 "Improper Fraction" 答案。

#include <iostream>
#include <string>

using namespace std;


 void reduce_fraction (int top, int bottom)
 {
    for (int i = top * bottom; i > 1; i--) {  
            if ((top % i == 0) && (bottom % i == 0)) {  
         bottom /= i;  
            top /= i;  
    }  

     }
}

void add_fraction (int numerator, int numerator2, int denominator, int              
denominator2)
{
int top;
int bottom;
top = numerator2 * denominator + denominator2 * numerator;
bottom = denominator2 * denominator;

cout << "Improper Fraction -> ";
cout << top << "/" << bottom << endl;
cout << "Simplified Fraction -> ";
reduce_fraction(top, bottom);
}


int main()
{

int numerator;
int denominator;
int numerator2;
int denominator2;
char operation;

cout << "Input the numerator: ";
cin >> numerator;

cout << "Input the denominator: ";
cin >> denominator;

cout << "Input the numerator2: ";
cin >> numerator2;

cout << "Input the denominator: ";
cin >> denominator2;

cout << "Input the operation: ";
cin >> operation;

if (operation == '+'){
    add_fraction(numerator, numerator2, denominator, denominator2);
}

return 0;   
}

使用引用来反映topbottom中的变化 并在调用 reduce_fraction

后在 add_fraction 函数中打印它们
void reduce_fraction ( int & top, int & bottom)
{                         ~~~        ~~~
 //...
}

然后,

reduce_fraction(top, bottom);
cout << top << "/" << bottom << endl;