Thread: Help with a simple loop...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    14

    Help with a simple loop...

    Code:
    #include <stdio.h>
    main()
    {
          int option ;
          int pin ;
          int pindef ;
          int exit ;
          pin = 1234 ;
          
          
          
         while ( exit //PROBLEM HERE!!!
         {
          printf( "Welcome to the main menu,please chose from the following options...:\n\n" ) ;//menu greting
          printf( "1.      Enter PIN and verify correct.\n\n" ) ;//option 1
          printf( "2.      Change PIN.\n\n" ) ;//option 2
          printf( "3.      Display the number of times the PIN was entered:\n (i) Successfully \n(ii) Incorrectly\n\n" ) ;//option 3
          printf( "4.      Set maximum number of attempts to enter correct PIN.\n\n" ) ;//option 4
          printf( "5.      Exit Program.\n\n" ) ;//option 5
          
          
        
          
          
          printf( "Please chose an option between 1 - 5:\n") ;
          scanf( "%d",&option) ;//option input
          
          switch (option)       //begining of switch statement
          {
                
                
                
                case 1:
                     printf( "Please enter you pin\n") ; // begining of option 1
                     scanf("%d",&pin) ; //scan PIN
          
                     if ( pin == 1234 ) // loop if pin is entered correctly
                     printf( "This is the correct PIN\n" ) ;
                     printf( "Press 3 to exit\n" ) ;
                     scanf( "%d",&exit) ;
            
            if ( pin !=1234 )
            
            while ( pin != 1234)//loop if pin is entered incorrectly
            {
                 printf( "This is not the correct PIN\n" ) ;
                 printf( "Please re - enter your pin\n" ) ;
                 scanf( "%d",&pin ) ; //re scan pin
                 
                 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
                 
                 printf( "This is not the correct PIN\n" ) ;
                 printf( "You must re - start Program\n" ) ;
                 scanf( "%d",&pin ) ; //re scan pin
                 printf( "Press 3 to exit\n" ) ;
                     scanf( "%d",&exit) ;
                 
                 
                 break ;
            }// end while
                break;  //end of option 1
                
                
                
                
                
                case 2:
                     printf( "You have chosen option 2" ) ;
                  break ;
                     
                case 3:
                     printf( "You have chosen option 3" ) ;
                     break ;
                     
                case 4:
                     printf( "You have chosen option 4" ) ;
                     break ;
                     
                case 5:
                     printf( "You have chosen option 5" ) ;
                     break ;
                
                 }//end switch
          
          }//end while
          
          
          
          
          
    }//end main
    basically i need the program to loop back to the main menu after i have interacted with an option,i did it earlier on in college but cant do it now any help would be appreciated!

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    For one your while brackets are not closed off:
    Code:
    while ( exit
    Should be
    Code:
    while (exit)
    Also it should be:
    Code:
    while(exit != 3)
    if I'm reading your code correctly.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    ah i know its not closed off....i left it open because i didnt know what to do next...i did what you said,but its not looping!

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Your if needs brackets:
    Code:
    if ( pin !=1234 )
    {
     
            while ( pin != 1234)//loop if pin is entered incorrectly
            {
                 printf( "This is not the correct PIN\n" ) ;
                 printf( "Please re - enter your pin\n" ) ;
                 scanf( "%d",&pin ) ; //re scan pin
     
                 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
     
                 printf( "This is not the correct PIN\n" ) ;
                 printf( "You must re - start Program\n" ) ;
                 scanf( "%d",&pin ) ; //re scan pin
                 printf( "Press 3 to exit\n" ) ;
                     scanf( "%d",&exit) ;
     
     
                 break ;
            }// end while
    }//End if
    Also, initialize exit:
    Code:
    int exit = 0;
    Or better yet, enum a bool type and use that.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    kinda works now....its just not looping option2..

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    What do you mean? What is not working? Selecting option 2? DId you forget the closing bracket of the if statement?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Choose another name rather than exit, since there is also a function of the same name.
    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.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
            while ( pin != 1234)//loop if pin is entered incorrectly
            {
                 printf( "This is not the correct PIN\n" ) ;
                 printf( "Please re - enter your pin\n" ) ;
                 scanf( "%d",&pin ) ; //re scan pin
                 
                 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
                 
                 printf( "This is not the correct PIN\n" ) ;
                 printf( "You must re - start Program\n" ) ;
                 scanf( "%d",&pin ) ; //re scan pin
                 printf( "Press 3 to exit\n" ) ;
                     scanf( "%d",&exit) ;
                 
                 
                 break ;
            }// end while
    That won't do what you want it to do. I read somewhere that some programmers think that as soon as a while loop's condiiton is false, the loop will end. I guess you have this impression. Well, a while loop only exits when execution of the program gets to the to of the loop and the condition is false. http://www.cprogramming.com/tutorial/c/lesson3.html
    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.

  9. #9
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    aka add:
    Code:
    printf( "This is not the correct PIN\n" ) ;
    printf( "Please re - enter your pin\n" ) ;
    scanf( "%d",&pin ) ; //re scan pin
    if (pin != 1234)
    {
             continue;
    }
    
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I would do it like this myself:
    Code:
    int x;
    
    for(x = 0; x < 3 && pin != 1234; x ++) {
        printf( "This is not the correct PIN\n" ) ;
        printf( "Please re - enter your pin\n" ) ;
        scanf( "%d",&pin ) ; //re scan pin
    }
    Or something like that.

    BTW, you don't need to set pin at the beginning of the program . . . but if you do, I wouldn't set it to the correct value. Maybe -1 or INT_MIN or something.
    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.

  11. #11
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    It's a matter of preference. Generally I would use your approach, I just wanted to keep the code as close to his original as possible (adding what amounts to 2 lines).
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I don't see how a continue would keep the code in its original condition . . . a break 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.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by manutd
    Your if needs brackets:
    Code:
    if ( pin !=1234 )
    {
     
            while ( pin != 1234)//loop if pin is entered incorrectly
            {
    Purely optional, though it is good practice. The if will bind to the nearest appropriate statement, as will for, do, while. Thus:
    Code:
    if( x == y )
        while( x < z )
            do
                for( ; z-- ; )
                    printf( "%d", y );
            while( z = y-- );
    While incredibly ugly, it's perfectly legal.


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

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    ok you have to remember i NEW to all of this lol...if someone could write a really small code giving the user a choice of 2 options,and when the user finishes wit an option it loops back to the menu,this would be great help.oh and using a while loop

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It was explained reasonably well in your other thread. The basic structure looks like this:
    Code:
    while(!quit) {
        scanf("%i", &menu);
    
        switch(menu) {
        case N:
            /* ... */
            break;
        default:
            /* ... */
            break;
        }
    }
    Anyway, you don't seem to be having too much trouble with that part . . . it's the pin part that needs working on.

    [edit]
    While incredibly ugly, it's perfectly legal.
    http://cboard.cprogramming.com/showthread.php?t=85193 [/edit]
    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.

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, 05:39 AM
  2. Trying to figure out a simple loop....
    By chadsxe in forum C++ Programming
    Replies: 9
    Last Post: 01-05-2006, 01:31 PM
  3. simple collision detection problems
    By Mr_Jack in forum Game Programming
    Replies: 0
    Last Post: 03-31-2004, 04:59 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM