Stupid question.. sorry..

This is a discussion on Stupid question.. sorry.. within the C Programming forums, part of the General Programming Boards category; what's wrong with this statement? I keep getting parse errors. Note: The variable q1 refers to a 'char' variable, declared ...

  1. #1
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59

    Stupid question.. sorry..

    what's wrong with this statement? I keep getting parse errors.

    Note: The variable q1 refers to a 'char' variable, declared as follows:

    Code:
     char q1 = 'y';
    Code:
    if  (q1=='y') or (q1=='Y')
       
    printf("Correct!");

    i've also tried adding braces..

    Code:
    if  (q1=='y') or (q1=='Y')
    {
       
    printf("Correct!");
    }
    and i've even tried without the parenthesis..

    Code:
    if  q1=='y' or q1=='Y'
       
    printf("Correct!");

    I just had this working!! I messed around with it a little, re-wrote it, and now I'm getting parse errors. Is it something i'm not seeing?

    Anyone know of a few code examples that use conditionals with operators? (and, or, not)
    Last edited by kwikness; 08-18-2006 at 11:33 PM.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    The keyword or is a readable alternative to the logical OR operator defined in <iso646.h>
    You should only consider using it if you can't type this:

    if ( q1=='y' || q1=='Y' )

    or even

    #include <ctype.h>

    if ( tolower(q1) == 'y' )

  3. #3
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59
    Thanks citizen, the || operator worked fine.

    I could have sworn I compiled it using "or", and all I included was the <stdio.h>,
    maybe i'm too many beers deep..

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    Compilers should warn you about implicit declarations of things you're using, when you hadn't included the proper header file. Sometimes the compiler is smart and the implicit declaration is correct, but it's a good thing to strive for warning-free compilation.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kwikness
    I could have sworn I compiled it using "or", and all I included was the <stdio.h>,
    You could have been compiling as C++. Quoting 'C: A Reference Manual, 5th Edition', page 333:
    Amendment 1 to C89 added the header file iso646.h, which contains definitions of mac-
    ros that can be used in place of certain operator tokens. Those tokens could be inconvenient
    to write in a restricted source character set (such as ISO 646). In C++, these identifiers are keywords.
    The macros in question are:

    and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor, and xor_eq

    So it is possible that had you compiled with C++, as many do here, it let you use your keyword or without needing any additional headers.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > if (q1=='y') or (q1=='Y')
    You need () round the whole thing as well

    if ( (q1=='y') || (q1=='Y') )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Java and C newbie Xero's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    21
    Quote Originally Posted by Salem
    > if (q1=='y') or (q1=='Y')
    You need () round the whole thing as well

    if ( (q1=='y') || (q1=='Y') )
    Totally agreed to him.

  8. #8
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59
    Cool. Thanks for your help guys.

  9. #9
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59
    Ok, here's another one if anyone is willing to help. The source compiles fine, but when I execute the program, I get a whole bunch of errors.. I believe the bug has to be somewhere in the "lucky number" part of the program.

    Code:
    #include <stdio.h>
    main()
    {
    int randomNum;
    srand(time());
    
    randomNum = (rand() %4) + 1;
    
    printf("\n\n\nFortune cookie say..\n");
    
    switch (randomNum)
    {
    
    case 1:
    printf("\nHim with little shovel, not do big dig.\n");
    break;
    
    case 2:
    printf("\nBoiled rice better then fried rice.\n");
    break;
    
    case 3:
    printf("\nIf ghost scare you in dream, wake up.\n");
    break;
    
    case 4:
    printf("\nConfucious is philosopher, not chef.\n");
    break;
    
     }
    printf("\n\nYour lucky number is...\n");
    printf("%d ", (rand() % 49) +1);
    
    }

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > srand(time());
    You need to include stdlib.h and time.h
    Then you'll see time() needs a parameter.

    > main
    int main
    at the start, and finish with
    return 0;

    > printf("%d ", (rand() % 49) +1);
    rand() gives you a new result each time, try
    printf("%d ", randomNum);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Question Probably
    By Kyrin in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 01:51 AM
  2. Replies: 7
    Last Post: 11-04-2005, 12:17 AM
  3. Stupid Question
    By digdug4life in forum C++ Programming
    Replies: 22
    Last Post: 05-17-2005, 12:43 PM
  4. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 08:22 PM
  5. Stupid question: What does Debugger do?
    By napkin111 in forum C++ Programming
    Replies: 6
    Last Post: 05-02-2002, 11:00 PM

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