Boolean value not being updated in print loop

Im writing a code for a class and it involves using classes and a boolean value to display a (-) if the fraction is negative. The input function is correct but I cannot get the value of the boolean to update in the print function so it prints everything out with a (-) sign. Can anyone help me modify my code so when the user enters Y it prints out no sign but when they enter N it prints out a (-)?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  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)
    {
        cout << "-" << numerator << " / " << denom;
    }
    else
    {
        cout << "+" << numerator << " / " << denom;
    }
}
void fraction::inputFrac()
{    
    char tempchar1;
    
    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=1;
    }
    else if (tempchar1=='N')
    {
        positive=0;
    }
}
Are you sure you're entering an upper case 'Y'? Remember C++ is case sensitive 'Y' is not equal to 'y'.

Also you really don't need the else if() clause in your input function, a simple else would work.
Or you could cover all premise for your user input in all cases:

1
2
    if ( (tempchar1 == 'Y') || (tempchar1 == 'y') ) { ... }
    else ..


More so, using 1 or 0 for bool value though it works, but it sound too much C/Perl like. Using true or false could spice up the soup!

Lastly, I think duplicating this post, is not really helping. At least one can follow your train of thought and solutions provided by others, If all the issues you have encountered solving this project is concentrated on a single thread of a several pages, others too can get help in a sequential other.
Just follow up your next post on this particular issue in your post, not opening up a new post for every issue for a particular question. Just saying...
Sorry for the duplicating of posts,

but still whenever I enter any numbers in the f1 and f2 and assign it a Y or N value, it will still add a (-) sign and im still unsure how to get this away
Topic archived. No new replies allowed.