【发布时间】:2015-11-30 17:31:14
【问题描述】:
所以我有一个我正在编写的类的程序,除了打印功能之外,它都已完成。输入函数是正确的,布尔值将在输入函数中更新,但是当我尝试转移到打印函数时,它总是打印(-),如果用户输入“Y”,那么假设绕过(-)和只需打印出分数。如果有人无论如何要让它工作,我觉得我已经尝试了一切。
编辑:当我只打印出 main 中的两个分数 f1 和 f2 时,打印功能起作用,但是当我相信将正数传递给打印功能时问题就来了。
class fraction
{
private:
int numerator;
int denom;
bool positive;
public:
void inputFrac();
void printFrac();
fraction fracMult(fraction& b);
fraction fracDiv(fraction& b);
fraction fracAdd(fraction& b);
fraction fracSub(fraction& b);
};
void fraction::printFrac()
{
if (positive=true)
{
cout << "-" << numerator << " / " << denom;
}
else
{
cout << "+" << numerator << " / " << denom;
}
}
void fraction::inputFrac()
{
char tempchar1;
fraction tempchar;
cout<<"Please input the numerator ";
cin>>numerator;
cout<< "Please input the denominator ";
cin>>denom;
cout<<"Is the fraction positive? (Y or N) ";
cin>>tempchar1;
if((tempchar1=='Y'))
{
positive=true;
}
else
{
positive=false;
}
}
【问题讨论】:
标签: c++ function class printing boolean