If statement hanging...

This is a discussion on If statement hanging... within the C Programming forums, part of the General Programming Boards category; I am trying to check for valid date.. if it is not valid then I ask them to reenter the ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    40

    If statement hanging...

    I am trying to check for valid date.. if it is not valid then I ask them to reenter the date. Otherwise it should ask for the deposit. But when I enter a bad date and then reenter the date it hangs? Why?




    Code:
    case 1:
                  printf("Please enter a valid date from 1 to 31:  ");
                  scanf("%d", &date);
                        if ((date < 1) || (date > 31)){
                           printf("Please enter a valid date from 1 to 31.  ");
                           scanf("%d", &date);
                      }else
                           printf("How much do you want to deposit?   ");
                           scanf("%f", &dpst);
                       if ( crdt == 0 ) {                 
                            blnce = strt_blnce + dpst;    
                      } else if (crdt > 0)
                       blnce = blnce2 + dpst;            
                  printf("Your current balance is $%1.2f. \n\n", blnce);
                  crdt++;                                
                  printf("Please indicate your option:    ");
                  scanf("%d", &opt);
                  break;

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Your missing a } to close the else. The scanf for the amount is executing...so its "hanging" waiting for input

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    there is no opening or closing brace to the else statement

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Code:
    case 1:
        printf("Please enter a valid date from 1 to 31:  ");
        scanf("%d", &date);
        if ((date < 1) || (date > 31))
        {
            printf("Please enter a valid date from 1 to 31.  ");
            scanf("%d", &date);
        //Missing braces in red
        } else {
            printf("How much do you want to deposit?   ");
            scanf("%f", &dpst);
        }
        if ( crdt == 0 ) {                 
            blnce = strt_blnce + dpst;    
        } 
        else if (crdt > 0)
            blnce = blnce2 + dpst;            
        printf("Your current balance is $%1.2f. \n\n", blnce);
        crdt++;                                
        printf("Please indicate your option:    ");
        scanf("%d", &opt);
        break;
    I suppose to need a closing brace you need an open one too

  5. #5
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    See, this is why people don't like K&R.

    *ducks*

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    K&R

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    '...We follow the style use in the book The C Programming Language...Pick one style, preferably ours, use it consistently, and don't waste time arguing.'

    The Practice of Programming by Brian W. Kernighan & Rob Pike
    ::evil laugh::

    ~/

  8. #8
    ---
    Join Date
    May 2004
    Posts
    1,379
    omg i cant believe they put that in a book

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    I'm glad I missed that book.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You people apparently have no sense of humor.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 01:33 PM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 03:49 PM

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