Thread: While loop with multiple characters

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    18

    While loop with multiple characters

    First off - this is a homework problem, and I do not expect you guys to do the coding for me

    However, I do need some general hints as to how I should format this problem.

    Here is the problem assigned to me
    "To gain entrance to a secret area you often need a secret password consisting a sequence of alphanumeric characters that must be entered in a specified order. Create a program that reads in characters until the user enters the correct two character sequence (cs) to open the door. "

    Note: we have not covered strings or arrays, so we can take those options off the board right not.

    I figured the best way to accomplish this task was to use a while loop, but I am encountering some troubles trying to figure out exactly how to accomplish it.

    I have created a program that correctly uses a while loop with a character but I cannot get it to work for 2 characters.
    Code:
    #include <stdio.h>
    
    int main()
    {
    
        puts("Please enter password now");
        
        while(getchar() != 'c')
        
        printf("Incorrect password try again");
        
        system("pause");
    }
    Its a small start, but this is something I don't have much experience with.

    Thanks in advance,

    AB

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Well, you pretty much have the idea. You have to keep reading until the user enters c and then s, right? OK, so in a loop keep reading until the user enters c. Once you get a c, the next char must be s. Check for it. If it's s, then you have the password, otherwise you don't and you loop again.

  3. #3
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Hello arctic_blizzard!

    To gain entrance to a secret area you often need a secret password consisting a sequence of alphanumeric characters that must be entered in a specified order. Create a program that reads in characters until the user enters the correct two character sequence (cs) to open the door.
    Did you see the main problem in the bold text?

    You can get two character sequence by doing this:
    Code:
    char c1, c2;
    c1 = getchar();
    c2 = getchar();
    So, check the characters inputted by user are correct or not:
    Code:
    if(c1 == 'a' && c2 == 'b')
    {
       printf("Correct password.");
    }
    else
    {
       printf("Incorrect password.");
    }
    And you can make your program request to the user repeatly:
    Code:
    for(;;)
    {
       //request to the user
    }
    At last, if we combine them, should be look like this:
    Code:
    for(;;)
    {
       char c1, c2;
    
       printf("Please enter the password: ");
    
       c1 = getchar();
       c2 = getchar();
    
       if(c1 == 'a' && c2 == 'b')
       {
          printf("Correct password.");
          break; //Exit loop
       }
       else
       {
          printf("Inorrect password.");
       }
    }

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    18
    Granted - this program works beautifully, but I am pretty sure the professor was looking for us to create a program using loops.

    I have tried to build on this code, using loops instead of the if else statement, but am still encountering problems. For one - the program issues success even if only one of the variables is met, and my printf statement shows no matter what the outcome it - is there a way to display different statements depending on success/failure using the while statement?

    Code:
    #include <stdio.h>
    
    int main()
    
    {
       char pass1, pass2;
    
       printf("\nPlease enter your password now:");
    
       while(pass1!='c' && pass2!='s')
       {
          pass1=getchar();
          pass2=getchar();
          printf("statement");
    }
        system("pause");
    }
    Thank you for your posts,

    AB

  5. #5
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Quite simple...
    Code:
    int first = 1;
    
    while(pass1 != 'c' && pass2 != 's')
    {
       printf("&#37;s", (first ? "" : "Incorrect password."));
       first = 0;
       pass1=getchar();
       pass2=getchar();
    }
    printf("Correct password.");

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The loop condition is backwards. You need to think harder about it.
    Try to follow the logic in your program and see if you can figure out what it should be.
    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
    Jul 2008
    Posts
    18
    Ok, I am bound and determined to get this one right.

    I tried a new tactic - using a do while loop. I can get the program to successfully run when I input 1 correct letter, but is there a way to get it to only accept the correct sequence of letters?
    Code:
    #include <stdio.h>
    
    int main()
    {
        char Pass;
        do
          {
                      printf("Enter password:");
                      scanf("%s", &Pass);
          } 
          while(Pass != 'a');
    system("pause");
    return 0;
    }
    the program works fine, and asks for the password again if 'g' is not entered
    but, how can I make this work if the password is two letters (say 'ab')

    I have tried
    while(Pass != 'a' && Pass != 'b');
    but the program never successfully completes, it just keeps looping
    I have also tried
    while(Pass != 'ab');
    but I get the same result.

    What am I missing?

    -AB

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    2
    The Pass variable can only store 1 character at a time.
    Why don't you create a second variable, read it also and then compare on while using ||?
    Regards,

    on post #4, when you use while(pass1!='c' && pass2!='s')
    you are expecting one! correct pass to be entered.

    From logic point of view you could see that
    not(A and B) = not(A) OR not(B)
    Last edited by ricardo0y; 09-14-2008 at 08:54 PM. Reason: to correct operator and compare to post 4

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    scanf("%s", &Pass);
    This is wrong. Pass is a char, but %s is for reading a string (i.e. an array of chars). getchar() was the right approach.

    What about using a control flag? i.e. using another variable that is set to 1 if the getchar() returned 'c', otherwise set it back to 0. If the control flag is set to 1 then check if the next char is 's'...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    Registered User
    Join Date
    Jul 2008
    Posts
    18
    OK, I think I finally have it!

    Thanks to everyone for the suggestions, I used many of them.

    Here is my working code, take a look at it, and see if there is anything wrong with it.

    Code:
    #include <stdio.h>
    
    int main()
    {
         int Pass = 1;
         char Pass1;
         char Pass2;
    
         while (Pass)
         {
               printf("Please enter your password: ");
               Pass1=getchar();
               Pass2=getchar();
               if (Pass1!='g' || Pass2!='f');
               else 
      
               Pass = 0;
               }
    system("pause");
    return 0;
    }
    Cheers,
    ~AB

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> if (Pass1!='g' || Pass2!='f');

    Do you see the semicolen at the end of that line? Remember: a semicolen by itself *is a statement*.

    And if you really want challange, design a finite state machine that does that (without using arrays).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    'g' & 'f'? I thought you were supposed to be looking for 'c' & 's'?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  13. #13
    Registered User
    Join Date
    Jul 2008
    Posts
    18
    it would work for any characters that you define in the equation.

    I was just using random letters

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
         while (Pass)
         {
               printf("Please enter your password: ");
               Pass1=getchar();
               Pass2=getchar();
               if (Pass1 == 'g' && Pass2 == 'f'); // Change your IF!
                    Pass = 0; // Indent!
         } // Put it on the correct indentation level!
    Just asking, but what IDE / editor do you use?
    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.

  15. #15
    Registered User
    Join Date
    Jul 2008
    Posts
    18
    I am using Dev C++ v 4.9.9.2 using its default compiler (GCC I believe).

    Is there a better program I should be using?

    I have Microsoft Visual C++ 2008, would that be a better program to use?
    Can you still write C code in VC++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  2. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  3. Unexplained "unhandled exception"
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 04-19-2007, 11:19 AM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM