need help please.

This is a discussion on need help please. within the C Programming forums, part of the General Programming Boards category; i am not asking for you to do my homework, i am just asking for some pointers. i would ask ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    99

    need help please.

    i am not asking for you to do my homework, i am just asking for some pointers. i would ask my professor but i am taking the intro class online and he hasnt responded to my email yet and i want to figure this out. i am trying to write a code for a password 'cs' using a while loop. any advice would be great. thanks.


    Code:
    #include <stdio.h>
    
    
    main ()
    
    
    {
    char ch1;
    char ch2;
    int pass;
    
    
    printf("Please Enter Your Password>\n");
    pass = 0;
    scanf("%c %c", &ch1, &ch2);
    while(!pass);
    {
    if (ch1 == 'c' && ch2 == 's');
    else
    printf("Access Denied.\n");
    scanf("%c %c", &ch1, &ch2);
      }
     printf("The door opens. Congratulations!\n");
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,264
    Code:
    if (ch1 == 'c' && ch2 == 's');
    That semi-colon at the end of the "if()" statement is not doing anything useful.
    On a related note, your variable "pass" is not updated anywhere, and that is what would allow you to exit the "access denied" loop.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Moreover to what matticus said,i would replace this
    Code:
    while(!pass);
    with this
    Code:
    while(!pass)

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Quote Originally Posted by Matticus View Post
    your variable "pass" is not updated anywhere, and that is what would allow you to exit the "access denied" loop.
    i dont know what that means.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    Is "c s" the same as "cs"? According to your scanf format, it is.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    ok. i changed it a bunch, but still need help. be patient with me. thanks.

    Code:
    #include <stdio.h>
    
    
    main ()
    
    
    {
    char ch1;
    char ch2;
    int pass;
    
    
    printf("Please Enter Your Password>\n");
    pass = 0;
    scanf("%c%c", &ch1, &ch2);
    
    
    while(pass){
    if(ch1 == 'c' && ch2 == 's'){
    	printf("Access Granted.\n");
    	break;
    	}
    	else
    	printf("Access Denied.\n");
    	scanf("%c%c", &ch1, &ch2);
      }
      }

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by rosemary View Post
    i dont know what that means.
    You have this loop(i erased the semicolons that were not supposed to be here).
    Code:
    while(!pass)
    {
              if (ch1 == 'c' && ch2 == 's')
              {
                             /*You might want to do something here?*/
                             /*Whatever you do here is going to be
                            executed if ch1 is character c and ch2 is
                            character s*/
               }
                else
               {
                      printf("Access Denied.\n");
                      scanf("%c %c", &ch1, &ch2);
                }
      }
    So,take a look at the following
    • The if structure is like this
      Code:
          if(someCondition)
          {
                   /*do something if the someCondition
                     is true.Here is the body of the if*/
           }
    • If you write it without the braces,the only the next line is going to be considered to be the body of the if.For instance
      Code:
        int x;
        if(x<5)
           printf("x is smaller than five\n");/*body of if*/
        printf("x is just a number\n");
      if x is smaler than five,then the 1st printf is going to be executed and the second one too (!),beacuse the body of if is only the 1st printf.The second printf is going to be executed no matter what.It is a totally irrelevant line of code with the if structure above.I suggest the beginners not to write if,for,while and other structures without braces.
    • The while loop should be something like this
      Code:
      while(someCondition)
      {
           /*body of loop*/
      }
      the body of the loop is going to be executed only if the someCondition is true(so may not executed at all(the someCondition is always false ) or forever(someCondition is always true).In the last case(forever),we have the phenomenon of infinite loop,which in theory is going to last forever and ever.So in case we enter the loop,this means that someCondition is true,then in order to exit from it ,something in the body of the loop must happen to get us out of the loop.Usually this is some code that makes the someCondition false.
      [
    • So in your loop,you have the someCondition to be represented by !pass ,but in the body of the loop,you do not modify it at all,so once you enter the loop ,you are going to be trapped there forever.
    • As a result,you have to modify the pass into your loop.How?
      Remember, in what you read above, some comments i wrote in an if-body that were like this /*You might want to do something here?*/?Well this body of if is going to be executed if the user has given the correct password(at least this is your intention).So if you enter this body of the if,then you know that the user has given the right password,so you should assign pass the value 1 (logical true).That way you are going to exit the loop when the password is ok!


    Also take a look at whiteflag said,it is a very clever input

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by rosemary View Post
    ok. i changed it a bunch, but still need help. be patient with me. thanks.
    Please read what i just posted and if you have question and/or problems post back.We post the same time ,so my post has taken into no consideration your latest post

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    2,264
    Quote Originally Posted by rosemary View Post
    ok. i changed it a bunch, but still need help. be patient with me. thanks.
    Let's tidy up the code a bit so it's easier to read.

    Code:
     
    main ()  // should be "int main(void)"
    {
        char ch1;
        char ch2;
        int pass;
    
        printf("Please Enter Your Password>\n");
        pass = 0;  // can be initialized above at the declaration
        scanf("%c%c", &ch1, &ch2);
    
        while(pass)  // "pass" is zero, so this won't execute - a "do-while" loop would be better here
        {
            if(ch1 == 'c' && ch2 == 's')
            {
                printf("Access Granted.\n");
                break;  // this works, but so would setting "pass" equal to 1
            }
            else
                printf("Access Denied.\n");
            scanf("%c%c", &ch1, &ch2);  // You want this as part of the "else" statement.  If so,
                                        // enclose these lines in brackets like you did in the "if()"
        }
    
        // don't forget to "return 0;"!
    }

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    k. im back. ive changed it a bunch again. trying to use the do-while loop now.

    Code:
    #include <stdio.h>
    
    
    int main(void)
    
    
    {
    char ch1;
    char ch2;
    int pass = 0;
    
    
    printf("Please Enter Your Password>\n");
    scanf("%c%c", &ch1, &ch2);
    
    
    	do
    	{
    	printf("Access Granted.\n");
    	}
    	while(ch1 != 'c' || ch2 != 's');
    	{	
    		printf("Access Denied.\n");
    		scanf("%c%c", &ch1, &ch2);
        }
      
      }

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    i need the pass somewhere, dont i?

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    2,264
    You don't quite have the "do-while" syntax correct. (Read up about it here.)

    Code:
    do
    {
        // code you want to loop
    } while(testCondition);
    You can use the same code as you have in post #6; just move the "while()" part after the block and put a "do" where the "while()" was originally.

    Then you can change "pass" where I indicated in the comments of post #9.

  13. #13
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Code:
    #include <stdio.h>
     
    int main (void)
    {
        char ch1;
        char ch2;
        int pass = 0;
     
         printf("Please Enter Your Password>\n");
        scanf("%c%c", &ch1, &ch2);
     
         do{    
            printf("Access Granted.\n");
            pass = 1;    
        }while(ch1 == 'c' && ch2 == 's');
        
            
          return 0;
        }



    so, this accepts every password. where do i put the "denied"? thanks.

  14. #14
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    changed it again. (sorry, this is how i learn best). i think this will work.

    Code:
    #include <stdio.h>
     
    int main (void)
    {
    	char ch1;
    	char ch2;
    	int pass = 0;
     
    	do
    	{	
    		printf("Please Enter Your Password>\n");
    		scanf("%c%c", &ch1, &ch2);
     		if(ch1 != 'c' || ch2 != 's')
     		{	
    			printf("Access Denied.\n");
    			scanf("%c%c", &ch1, &ch2);
    		}
    		pass = 0;	
    	}
    	while(ch1 != 'c' || ch2 != 's');
    	printf("Access Granted.\n");
    	
    	
    		
    	  return 0;
    	}

  15. #15
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,451
    No offense, but I feel like you're just trying random configurations of keywords, variable names and punctuation. This is typical of newer programmers who are having a hard time figuring out the logic of their program. You need to think about the flow of the program, but do not write any code yet. Put away the keyboard and get out the paper and pencil. Work through how you want this program to work with that paper and pencil. Draw a flow chart if that helps. Write down the steps in basic English. Pretend you are going to give those instructions to a 10 year old to follow. When you can do that, you know you understand the problem and how you want to solve it. For example:

    1. Ask the user for a password consisting of 2 letters/characters
    2. Get the password from the user
    3. If they have the right password let them in
    4. Otherwise, go back to step 1


    Now you have a clear description in English, transform it to pseudo code. Pseudo code should look like code in the sense that there may be a do-while loop or an if-else statement, but you should not be concerned with syntax and little details, i.e. don't worry about putting a semicolon at the end of every statement, etc. Once you have your pseudo code, run through the pseudo code by hand and make sure your algorithm works. Last thing, begin writing your program. Translate each line of pseudo code into real code (it may be more than one line of real code).

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

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