Help with a simple loop...

This is a discussion on Help with a simple loop... within the C Programming forums, part of the General Programming Boards category; My mistake! I thought it checked for the pin in the while loop. However, break will also do nothing, going ...

  1. #16
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    My mistake! I thought it checked for the pin in the while loop. However, break will also do nothing, going straight to the break out of the switch statement. Thus you need to get the pin in the while loop.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    break might work, I was thinking something along the lines of this:
    Code:
            for(;;)
            {
                 printf( "This is not the correct PIN\n" ) ;
                 printf( "Please re - enter your pin\n" ) ;
                 scanf( "%d",&pin ) ; //re scan pin
                 if(pin == 1234) break;
    
                 printf( "This is not the correct PIN\n" ) ;
                 printf( "Please re - enter your pin.You have one attempt left\n" ) ;
                 scanf( "%d",&pin ) ; //re scan pin
                 if(pin == 1234) break;
                 
                 printf( "This is not the correct PIN\n" ) ;
                 printf( "You must re - start Program\n" ) ;
                 scanf( "%d",&pin ) ; //re scan pin
                 if(pin == 1234) break;
    
                 printf( "Press 3 to exit\n" ) ;
                     scanf( "%d",&exit) ;
                 
                 
                 break ;
            }// end for
    But still, a modified version would be better.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #18
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Maybe a more flexible

    Code:
    int count, max = 5;
    for(count = 1; count <= max; ++count)
    {
        printf("Enter PIN [attempt %d of %d]: ", count, max);
        scanf("%d", &pin);
        if(pin == 1234)
        {
            break;
        }
        printf("ERROR: PIN incorrect\n");
    }
    
    if(pin != 1234)
    {
        printf("You have used up all attempts!\n");
        printf("The PIN has been locked\n"); /* whatever */
    }
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  4. #19
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    thanks!!

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 04:39 AM
  2. Trying to figure out a simple loop....
    By chadsxe in forum C++ Programming
    Replies: 9
    Last Post: 01-05-2006, 12:31 PM
  3. simple collision detection problems
    By Mr_Jack in forum Game Programming
    Replies: 0
    Last Post: 03-31-2004, 03:59 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM

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