user2087867
user2087867

Reputation: 105

justification not working properly

    if  (ch1 = 'l' || 'L')
        cout << left;
    else if (ch1 = 'r' || 'R')
        cout << right;
    else
        cout << "error" << endl;

    cout << setw(++aw) << setfill(char(a)) << s1 << endl;

    if  (ch2 = 'l' || 'L')
        cout << left;
    else if (ch2 = 'r' || 'R')
        cout << right;
    else
        cout << "error" << endl;

    cout << setw(++bw) << setfill(char(b)) << s2 << endl;

    if  (ch3 = 'l' || 'L')
        cout << left;
    else if (ch3 = 'r' || 'R')
        cout << right;
    else
        cout << "error" << endl;

    cout << setw(++cw) << setfill(char(c)) << s3 << endl;

return 0;
}

I'm not exactly sure why but all 3 output lines come out left justified. It seems legit to me I'm not positive if there is a logical error somewhere or if I just typed it in wrong

Upvotes: 0

Views: 79

Answers (2)

AndersK
AndersK

Reputation: 36092

When you write e.g.

if (ch1 = 'l' || 'L')

this will always be true because you are effectively saying

assign 'l' to ch1  (whose result is always is != 0)

OR

if 'L' is not equal to zero (which always is !=0)

instead the correct way to write it is using double =

if ( ch1 == 'l' || ch1 == 'L')

or

if ( toupper( ch1 ) == 'L' )

Upvotes: 0

jester
jester

Reputation: 3489

the comparison should be

if (ch1 == 'l' || ch1 == 'L')
  ch1 = left;
else if(ch1 == 'r' || ch1 == 'R')
  ch1 = right;
else
  cout << "error" << endl;

'l' , 'r', 'R', 'L' should be character literals unless you have the variables l,r,L,R with the appropriate characters assigned.

Upvotes: 1

Related Questions