Thread: Newbie with a if loop problem.

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    Newbie with a if loop problem.

    Code:
    #include <stdio.h>
    
    int main()
    {
    int lives = 3, answer;       //integer declaration//
    
       puts("THIS IS JOHN'S GAME. (C) 2010");  //title//
    
    
        //main program//
       while( lives > 0);
        {
    
            printf("What is the square root of 81?");
    
            scanf("%i", &answer);
    
            if(answer == 9, lives++);    //if answer is greater/equal to 9, +1 life//
            {
            printf("YOU ROCK! good.");
            }
    
            if(answer != 9, lives--);      //if answer is less/equal to 9, -1 life//
            {
            printf("YOU SUCK! -_-,");
    
            }
       }
       return(0);
    }
    What's going on? I run the program and it stops at the start of my loop. Just sits after the printf saying it's my game.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    Remove the ';' (semi-colon) after the while

    Code:
     while( lives > 0);
    should be

    Code:
     while( lives > 0)

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    2
    lol thanks.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    if(answer == 9, lives++);
    What's this supposed to do?
    Furthermore, do not put ; after if statements.
    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.

  5. #5
    Registered User R.Hermans's Avatar
    Join Date
    Feb 2010
    Posts
    5
    Quote Originally Posted by Elysia View Post
    Furthermore, do not put ; after if statements.
    Is it not so that no control structure should be closed with a semicolon?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As a general rule, I would say yes. There are exceptions you can use, but you will never get into trouble if not adding a semicolon, true.
    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.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    Quote Originally Posted by R.Hermans View Post
    Is it not so that no control structure should be closed with a semicolon?
    You may want to go over the syntax of the if command again to see why the semi-colons, as they are placed in this example, is not appropriate.

    Cprogramming.com Tutorial: If Statements

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  2. Replies: 8
    Last Post: 12-09-2008, 12:07 PM
  3. weired problem in while loop
    By avisik in forum C Programming
    Replies: 14
    Last Post: 12-01-2008, 01:41 PM
  4. Problem with a loop
    By wiggsfly in forum C Programming
    Replies: 5
    Last Post: 11-30-2008, 12:45 PM
  5. having problem with string statement during a loop!
    By Hp7130p in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2005, 09:40 AM

Tags for this Thread