Thread: complicates loops

  1. #1
    ___
    Join Date
    Jun 2003
    Posts
    806

    complicates loops

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char letter=0;
    
    	cout << endl
    		 << "Enter a letter: ";
    	cin >> letter;
    
    	if(((letter >= 'A') && (letter <= 'Z')  ||
    	   ((letter >= 'a') && (letter <= 'z')))
    
    	   cout << endl
    			<< "You entered a letter." << endl;
    
    	else
    		cout << endl
    			 << "You didn't enter a letter idiot..." << endl;
    
    	return 0;
    
    }


    And the error

    Code:
    c:\cfolders\logicops\cpp1.cpp(16) : error C2146: syntax error : missing ')' before identifier 'cout'
    So I'm missing a ) before cout buw where does it go? Its 100% exactly like the book shows it.
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Your 'if' statement is missing a closing paren.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    ___
    Join Date
    Jun 2003
    Posts
    806
    Your wonderful.
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  4. #4
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    I actually believe that you wanted it here:

    Code:
    if ( ( (letter >= 'A')  && (letter <= 'Z') )  ||
    	   ( (letter >= 'a') && (letter <= 'z') ) )
    Away.

  5. #5
    ___
    Join Date
    Jun 2003
    Posts
    806
    Thats where I put it since it didn't look right. And since I'm on loops I have one more question for the night.


    Code:
     << ((ncakes > 1) ? "s." : ".")
    Thats part of a cout statment. Its at the very end. ncakes is a variable. Just getting confirmation on what this does. So it checks to see if the variable ncakes is more than one. IF it is add the s. Then add the period no matter what?
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Yep.

    The syntax of ?: is this:

    expr ? exec-if-expr-true : exec-if-expr-false

    Therefore, if ncakes is greater than one, it attaches a 's.' otherwise, a '.'.

    Also, a bit about terminology. A 'loop' is a construct which repeats (while, for, do-while, etc.). An 'if' statement is more properly called a conditional statement.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    ___
    Join Date
    Jun 2003
    Posts
    806
    bah. Its all in the same chapter..
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  8. #8
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    That is not a loop! A loop:
    Code:
    While(condition)
    {
    ...
    }
    
    do
    {
    ...
    }
    while(condition)
    
    for(initialization;condition;exec)
    {
    ...
    }
    By the way, you can call ?: a ternary operator
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  9. #9
    ___
    Join Date
    Jun 2003
    Posts
    806
    Sorry. I keep calling that a loop because it's in the loop chapter and I have loops on my mind.
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  10. #10
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    A few things:
    Instead of doing if (blah >= 'a' && blah <= 'z' etc..) you should use the isalpha function included in <cctype>. With that in mind, your conditional would look like: if (isalpha(blah))

    When you have complex expressions with many parenthesis, go through the entire statement and keep a tally: When you encounter a '(' add 1. When you encounter a ')', subtract 1. If you end up with a negitive number, you're missing a '('. If you end up with a positive number, you're missing a ')'. Otherwise, it's all good

  11. #11
    ___
    Join Date
    Jun 2003
    Posts
    806
    I haven't learned that yet so this expression explains my thoughts.
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by Eibro
    When you have complex expressions with many parenthesis, go through the entire statement and keep a tally: When you encounter a '(' add 1. When you encounter a ')', subtract 1. If you end up with a negitive number, you're missing a '('. If you end up with a positive number, you're missing a ')'. Otherwise, it's all good
    Or use emacs and let it show you what parens match up.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  13. #13
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Whoa if you highlight a " " it looks like it is sad
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  14. #14
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    You know,
    Code:
    if (((letter >= 'A') && (letter <= 'Z'))  ||
        ((letter >= 'a') && (letter <= 'z')))
    can be written as
    Code:
    if (letter >= 'A' && letter <= 'Z'  ||
        letter >= 'a' && letter <= 'z')
    IMO, the second way looks better.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Or even better (read: portable), use isalpha()
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  2. For/Next Loops...adding 10 numbers...
    By IanelarAzure in forum C++ Programming
    Replies: 5
    Last Post: 09-12-2002, 12:02 PM
  3. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  4. for loops in C
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-15-2001, 05:09 PM
  5. exiting loops with ease?
    By KingRuss in forum Game Programming
    Replies: 3
    Last Post: 09-24-2001, 08:46 PM