don't get it || operator

This is a discussion on don't get it || operator within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int main () { long int x = 0; start: if (x != 128) ...

  1. #1
    beginner for now
    Join Date
    Oct 2009
    Posts
    35

    Question [SOLVED] don't get it how to make || operator

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    	long int x = 0;
    	start:
    	if (x != 128)
    	{
    		if (x == 9){x++;} //31 space//13 enter//10 enter//9 tab
    		if (x == 10){x++;} //31 space//13 enter//10 enter//9 tab
    		if (x == 13){x++;} //31 space//13 enter//10 enter//9 tab
    		if (x == 32){x++;} //31 space//13 enter//10 enter//9 tab
    		cout << (char)x;
    		cout << x << endl;
    		x++;
    		goto start;
    
    	}
    	return 0;
    }
    this is how I like it but why I can't make code:
    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    	long int x = 0;
    	start:
    	if (x != 128)
    	{
    		if (x == 9||10||13||32){x++;} //31 space//13 enter//10 enter//9 tab
    		cout << (char)x;
    		cout << x << endl;
    		x++;
    		goto start;
    
    	}
    	return 0;
    }
    Code:
    		if (x == 9||10||13||32){x++;} //31 space//13 enter//10 enter//9 tab
    this if is allways possible why?
    Last edited by military genius; 10-03-2010 at 04:27 AM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,545
    You must be explicit, ie:
    if (x == '\t' || x == '\n' || x == '\r' || x == ' ')
    I also suggest that you change x's type to char, remove the cast when printing x, and remove goto in favor of a real loop.
    Finally, use characters instead of magic numbers, such as the example above.
    Last edited by Elysia; 10-03-2010 at 03:49 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    beginner for now
    Join Date
    Oct 2009
    Posts
    35
    ok thanks


    ok new code it's shorter yes you're right
    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    	int x =0;
    	char y=x;
    	for (x=0;x!=128;x++&&y++)
    	{
    		if (x==9){x=x+2;y=x;}
    		if (x==13||x==32){x++&&y++;} //32 space//13 enter//10 enter//9 tab
    		cout << y << x << endl;
    	}
    	return 0;
    }

    can I make it in 1 line as 9 and 10 are together so if 9 + 2 if other numbers +1?

    but another thing i don't know

    Code:
    		if (x==9){x=x+2;y=x;}
    		if (x==13||x==32){x++&&y++;} //32 space//13 enter//10 enter//9 tab
    can i make this code shorter
    Last edited by military genius; 10-03-2010 at 04:25 AM. Reason: further investigation

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think you should check out the reference for <cctype>.

    In particular, whitespace is detected with isspace (and printable characters with isprint).

    Code:
    #include <iostream>
    #include <cctype>
    using namespace std;
    
    int main ()
    {
        int x =0;
        for (x=0;x!=128;x++) {
            if (!isspace(x)) {
                std::cout << char(x) << ' ' << x << '\n';
            }
        }
        return 0;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,301
    That's not correct usage of && there. In this piece of code:
    Code:
    x++&&y++
    y will not increment if x is zero.
    The right way to do it inside the incrementing part of the for loop is to use a comma:
    Code:
    x++, y++
    Or elsewhere, just use a regular semi-colon.

    You can also use x += 2 instead of x=x+2
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. boolean operator ||
    By dunsta in forum C Programming
    Replies: 13
    Last Post: 04-25-2010, 03:32 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Error
    By Kaho in forum C Programming
    Replies: 5
    Last Post: 08-19-2005, 01:56 AM
  4. Replies: 4
    Last Post: 11-23-2003, 06:15 AM
  5. Help plz
    By Unregistered in forum C Programming
    Replies: 41
    Last Post: 05-07-2002, 06:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21