Thread: Simple Question

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    11

    Simple Question

    Hello I seem to be getting "syntax error before else" please help me.
    Also will this code work?

    Code:
    int main()
    {
        int x;
    
        x = getchar();
    
        while (x = 1) (printf ("yes"));
        else if (x = 2) (printf ("no"));
        else (printf ("you must choose either yes or no");
    
    
    
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you starting with an else? You need an if to else. Also, you are using ( ) where you should be using braces.


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

  3. #3
    Registered User
    Join Date
    Sep 2009
    Location
    Europe / Latvia
    Posts
    5
    Hey there, I tried to explain the code in this way, I'm not sure if you're using C, seems like you're using C++ maybe, anyway.

    Code:
    #include <stdio.h>
    int main()
    {
        int x; 
     
        x = getchar();
     
        if(x==1) // if tests to see whether two things are equal, two equal signs are used, think "is equal to" not "equals" :)
    {
        printf("Yes.");
    }
        else if (x==2)
    {
          printf("No.");
    }
        else 
    {
        printf("You must choose either yes or no."); 
    }
    
    return;
    }
    Last edited by austra; 10-03-2009 at 07:25 AM.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    Okay thanks a lot, I'm really new to c so I keep confusing everything, you really cleared things up for me austra.

    okay so this is the current code, the probem is that no matter what is typed in the answer "you must choose either yes or no" comes up.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int x;
    
        x = getchar();
    
        if (x == 1) (printf ("yes"));
        else if (x == 2) (printf ("no"));
        else (printf ("you must choose either yes or no"));
    
        return;
    
    
    
    
    }
    Last edited by Syle; 10-03-2009 at 05:11 AM.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Location
    Europe / Latvia
    Posts
    5
    No problem, Syle, I'm new at C too, so all the other things, the serious ones are like blank pages to me Okay, sorry, that's my fault, because I didn't look hard enough, well, I'm gonna look into it right now and figure out the answer or at least try to figure it out, because we have to deal with multiple characters.

    For now here's the little program, you can surely change it so it will work with your examples, here you simply type 'Y' or 'y' and it will say "Okay, it's gonna explode and little aliens will come after us." when you type anything other except 'N' or 'n' it will give you a message "The End".

    Code:
    #include <stdio.h>
    int main() {
      char a;
      printf("Would you like your computer to explode?");
       a=getchar();
       if( (a=='Y') || (a=='y') ) //translates simply as does a variable is equal to big Y or little y, if so display the following.
    {
       printf("Okay, it's gonna explode and little aliens will come after us.");
    }
     else if( (a=='N') || (a=='n') ) 
    {
       printf("Okay, it will not explode.");
     }
     else
    {
      printf("The End!");
     }
     return;
    }
    By the way, what exactly you thought of putting there that while?... What compiler are you using?

    You use ' ' (example x=='t') when comparing single characters letter, number or a symbol.
    Last edited by austra; 10-03-2009 at 07:26 AM.

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Here is your code, with a few changes. The changes are almost entirely to do with style.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int x;
    
        x = getchar();
    
        if (x == 1) {
            printf("yes\n");  /* I added some newline characters.. */
        } else if (x == 2) {
            printf("no\n");
        } else {
            printf("You must choose either yes or no\n");
        }
    
        return 0;
    }
    First of all, you had you had no need to put brackets around your printf statements. You had this:
    Code:
    if (x == 1)(printf("yes"));
    I changed it to this:
    Code:
     if (x == 1) {
            printf("yes\n");
        }
    Now what I have showed you is known as K&R style, which is the style used in the book, The C Programming Language by Brian Kernighan, and Dennis Ritchie. There are other styles you could use, but one thing is important - pick a style and stick to it. It makes your code easier to read and comprehend. Theoretically you could invent your own style - but at this stage of the game, with the experience you have, I would just use someone else's style, and concentrate on learning how to program. As for the style you have used, believe me, it is hard to read.

    A minor issue:

    Code:
    return;
    You really should return an integer there. Something like:

    Code:
    return 0;
    Finally, the problem with your code not printing what you want is that you are comparing a '1' to a 1. '1' actually evaluates as 49. You can verify this by doing

    Code:
    printf("x = %d\n, x);
    somewhere in your code after the call to getchar. Then run the program and enter a 1, and see what you get. Have a look at this to see what I am talking about.
    Last edited by kermit; 10-03-2009 at 05:56 AM.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    Quote Originally Posted by Syle View Post
    Hello I seem to be getting "syntax error before else" please help me.
    Also will this code work?

    Code:
    int main()
    {
        int x;
    
        x = getchar();
    
        while (x = 1) (printf ("yes"));
        else if (x = 2) (printf ("no"));
        else (printf ("you must choose either yes or no");
    }
    First error - You made integral "x", but try to get it as character, so - it never by TRUE. Why? Read about variable types in C.
    Next error, WHILE syntax is absolutly wrong, this code can't be right. The same is with IF and ELSE syntax, it's too wrong.
    When You include some header file ( "#include <stdlib.h>" )? And where Your program returning system some value on exit ( return 0; )?
    I put this code working version, but i'm not C porgramer, so - it's maybe with some error's, but WHILE and IF/ELSE syntax is right.

    Code:
    #include <stdlib.h>
    
    main() {
    
    int x;
    x=0;
    
    while((x<1) || (x>2)) { // WHILE starts
    printf ("you must choose either 1(yes) or 2(no)\t");
    scanf("%d",&x);
    } // WHILE ends
    if (x == 1) {
      printf ("yes");
    }
    else {
    printf ("no"); // We don't need other IF, because now "x" may be only "1" or "2", "1" already checked with IF.
    }
    return 0;
    }

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by pluto View Post
    First error - You made integral "x", but try to get it as character, so - it never by TRUE. Why? Read about variable types in C.
    Actually, getchar returns an integer. All values of a char will fit in an int
    Quote Originally Posted by pluto View Post
    Next error, WHILE syntax is absolutly wrong, this code can't be right.
    Actually, there's technically nothing wrong with the while loop. However, x = 1 means that it will loop indefinitely unless you provide a mechanism to break from the loop.


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

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    Quote Originally Posted by Syle View Post
    Code:
    int main()
        while (x = 1) (printf ("yes"));
    Quote Originally Posted by quzah View Post
    ]Actually, there's technically nothing wrong with the while loop. However, x = 1 means that it will loop indefinitely unless you provide a mechanism to break from the loop.
    About WHILE
    1) To be one While - need one more ( after while and one ) on line end's.
    2) On While syntax no semicolon after argument brackets.
    3) No body brackets "{}" for WHILE. (where it starts, where stops?).
    4) In While parameters write functions ( printf(); ), what need be in While body.
    So, if compilator does not syntax error about this line, it's some strange. And if Style want to read for C, We need to say him about that errors.
    Last edited by pluto; 10-03-2009 at 08:01 AM.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by pluto View Post
    About WHILE
    1) Here is 3 opening and 2 closing brackets
    2) No body brackets for WHILE. (where it starts, where stops?).
    If there are no braces, then the next statement only ties to the previous:
    Code:
    if( x )
        printf( "this goes with the if" );
    printf( "this does not" );
    The same thing goes for the while loop.
    Quote Originally Posted by pluto View Post
    3) In While parameters write functions (printf(), what need be in While body.
    So, if compilator does not syntax error about this line, it's some strange. And if Style want to read for C, We need to say him about that errors.
    Leaving off the braces is not an error. Wrapping your function calls in parenthesis isn't an error either, so long as your semicolon is in the right spot (end of the line).
    Code:
    (printf("fine"));
    (printf("not fine);)

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

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    In C While too may be in short variant?
    But some strage with While (not first time), if try:
    Code:
    while((x<1) || (x>2))
    replace with:
    Code:
    while((x != 1) || (x != 2))
    It never stop's, not on a==1, not on a==2. But if try only with one of these parametrs (without OR) - it work's. Why?

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    One of those will always evaluate false.
    Code:
    while( (x is not one) OR (x is not two )
    If it is one, then it is not 2, and vice versa. Try AND instead.


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

  13. #13
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    No, in that case AND cant be true, because We geting "x" value and going to While parameters where it need stop IF x==1 OR x==2, but it never be x==1 AND x==2, here is only one value possible. So - we need OR.
    In C++ it work's normaly, but C i get already not first problem that type, with while.
    That is the same code, but ">" replaced with "!=" in While. While is neverending.

    Code:
    #include <stdlib.h>
    
    main() {
    
    int x;
    x=0;
    
    while((x!=1) || (x!=2)) { // WHILE starts
    printf ("you must choose either 1(yes) or 2(no)\t");
    scanf("%d",&x);
    } // WHILE ends
    if (x == 1) {
      printf ("yes");
    }
    else {
    printf ("no"); // We don't need other IF, because now "x" may be only "1" or "2", "1" already checked with IF.
    }
    return 0;
    }
    Last edited by pluto; 10-03-2009 at 10:31 AM.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No... not if you are saying that this isn't working for you:
    Code:
    while((x != 1) || (x != 2))
    If x is 1, then x != 2, so it will keep looping. If you want it to stop on either 1 or 2, then you need AND.
    Code:
    while( x != 1 && x != 2 )
        ...stops on 1 or 2...

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM