更新布尔值时,类中的打印函数不更新
Print function in a class not updating when boolean is updated
所以我有一个程序,用于我正在编写的一个类,除了print函数之外,其他都完成了。输入函数是正确的,布尔值将在输入函数中更新,但当我尝试转移到打印函数时,它总是打印(-),如果用户输入"Y",则假设它绕过(-)并只打印分数。如果有人想让它发挥作用,我觉得我什么都试过了。
编辑:当我只打印出主要的两个分数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;
}
}
您在if
语句中进行赋值。更改:
if (positive=true)
至:
if (positive==true)
在printFrac函数中,您有if (positive=true)
。这是不正确的,因为你正在做什么。您应该使用if (positive) {/*stuff*/}
。
相关文章:
- 如何循环打印顶点结构
- 从C++本机插件更新Vector3数组
- 为什么在popback()操作之后,它仍然打印完整的矢量
- QGraphicsPolygonItem在拖动时未更新QPolygonF坐标
- 如何在c++中打印目录
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 在线编译器中的分段C++没有打印消息
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 如何将结构插入到集合中并打印集合的成员
- 在循环C++中指定字符串之后,不会打印该字符串
- 以螺旋方式打印矩阵的程序.(工作不好)
- 从控制台中删除最后打印的元素
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何仅使用对象名称打印特定于对象的成员
- 更新的矢量元素不打印
- 为什么字符串没有更新并且最终没有打印任何内容
- 为什么在 var 未更新的情况下使用,仅在启动 var 后打印
- 更新布尔值时,类中的打印函数不更新
- 更新在控制台 C++ 上打印的值