Thread: Strange behaviour of Ternary

  1. #1
    Registered User
    Join Date
    Dec 2007

    Strange behaviour of Ternary

    It puts me out '0's before every line. There is nothing in the code to write zeros.
    If i take out this line everything goes well. No zeros.

    Now i think it could be due to the fact that if the first value is true =0
    second value is true =1.

    But why would it put it out? If i use the same statement in another program it wont put out zeros. I dont understand.

    #include <cstdio>
    #include <windows.h>
    #include <iostream>
    using namespace std;
    HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
    WORD wOldColorAttrs;
    COORD NewSBSize = GetLargestConsoleWindowSize(h);
    SMALL_RECT DisplayArea = {0, 0, 0, 0};
    int x=0;
    int main()
        SetConsoleScreenBufferSize(h, NewSBSize);  // FULLSCREEN
        DisplayArea.Right = NewSBSize.X - 1;
        DisplayArea.Bottom = NewSBSize.Y - 1;
        GetConsoleScreenBufferInfo(h, &csbiInfo);
        wOldColorAttrs = csbiInfo.wAttributes;
        SetConsoleTextAttribute ( h, 8 );
        cout<< "        A         B        C        D        E        F        G       H "<<endl;
        for(int y=0; y<42; y++)
            for(int x=0; x<42; x++)
               if(y==1)                          // FIRST LINE OF CHECKERBOARD
                    if(x==0)   cout<< "    " ;
                    else if(x==1)   putchar(218);
                    else if(x==41)  cout<<(char)191 << "\n" <<"    " ;
                    else if(x%5==1) putchar(194);
                    else            cout<<(char)196<<(char)196;
               else if(y%5==1 && 37>y)          // HORIZONTAL  LINES
                    if(x==0)        cout<< "";
                    else if(x==1)   putchar(195);
                    else if(x==41)  cout<<(char)180<< "\n" <<"    " ;
                    else if(x%5==1) putchar(197);
                    else            cout<<(char)196<<(char)196;    //horizontal lines
               else if(y==41)                  // LAST LINE OF CHECKERBOARD
                    if(x==0)        cout<< "";
                    else if(x==1)   putchar(192); 
                    else if(x==41)  cout<<(char)217<< "\n" << "   " ;
                    else if(x%5==1) putchar(193);
                    else            cout<<(char)196<<(char)196;
               else if(y!=0)                  // VERTICAL LINES
                    if(x==41)       cout<<(char)179 << "\n"<< (y%5==1) ? cout<<"  2" : cout<<"  3";  //  <-- THIS LINE!!!
                    else if(x%5==1) cout<<(char)179 <<"        ";
        cout<< endl;
        return 0;
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Well . . .
    cout<<(char)179 << "\n"<< (y%5==1) ? cout<<"  2" : cout<<"  3";
    Examine it.
    This does not do what you think:
     ? cout<<"  2" : cout<<"  3";
    It's printing the return value of
    , I believe.

    Correct me if I'm wrong . . . simply remove the "cout<<" and all should be fine.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Fix + tidyup suggestion:
    cout << static_cast<char>(179)
         << ((y%5==1) ? "\n  2" : "\n  3");
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Dec 2007
    Brilliant! Thank you Nightowl and iMalc!
    Compiler MSVC++ 2013 with Code::Blocks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange behaviour.......
    By surdy in forum C Programming
    Replies: 2
    Last Post: 05-01-2004, 11:50 AM
  2. Strange behaviour of ODBC cursor
    By knutso in forum Windows Programming
    Replies: 6
    Last Post: 05-07-2003, 07:39 AM
  3. GetClientRect strange behaviour
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 10-02-2002, 02:13 PM
  4. Strange behaviour
    By PrivatePanic in forum Windows Programming
    Replies: 11
    Last Post: 07-23-2002, 12:54 AM
  5. strange behaviour by rhide
    By ustuzou in forum C++ Programming
    Replies: 0
    Last Post: 03-17-2002, 11:31 AM