I need an example

This is a discussion on I need an example within the C++ Programming forums, part of the General Programming Boards category; Im kind of new to C++ and can some one give me an example of a password program? I know ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    14

    I need an example

    Im kind of new to C++ and can some one give me an example of a password program?
    I know its probably really simple but yeah i could use an example.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Code:
    #include <iostream>
    
    int main()
    {
    	std::string szPass("tehpassword");
    	std::string szGuess("");
    	while(szGuess != szPass)
    	{
    		std::cin >> szGuess;
    	}
    	std::cout << "You finally guessed the password!" << std::endl;
    	return 0;
    }
    Probably not what you expected.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    There are quite a few examples posted already - try a board search.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    14
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int a;
        
        cout<< "Password please: ";
        cin>> a;
        cin.ignore();
        if(a == 159753 ){
             cout<<"Correct hit enter.\n"}
    else{
         Cout<<"Incorrect\n";
        }
        cin.get();
    }
    I made thi5 5imple one, i know it5 not really much of anything but yeah it doe5nt compile can you tell me what5 wrong with it?

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Read the compiler error messages and figure it out.

    1) There's no semicolon after your second cout statement.
    2) The last cout you do has a capital 'c'. That's wrong, since C++ is case-sensitive.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    14
    How do I get it to repeat from the incorrect password to asking for a password again?

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Put the whole reading and checking of input into a loop with the condition being that the input is not the password. See my example.

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    14
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int a;
        
        cout<< "Password please: ";
        cin>> a;
        cin.ignore();
        if(a == 159753 ){
             cout<<"Correct hit enter.\n";
             }
        while(a != 159753)
    	{
    		std::cin >> a;
    	}
         else{
         cout<<"Incorrect\n";
        }
        cin.get();
    }
    I made it like this.
    It says nothing is wrong but when i run it the program opens and then if I hit any key it just shuts.

    Whats wrong?

  9. #9
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    I recommend that you read C++ Loop Tutorial before venturing any further. Your code should more look like:

    Code:
    while (a != 159753)
    {
        // ask the user to enter the password
    }

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    14
    Alright I re-read the tutorial part and this is what I came up with

    Code:
    #include <iostream>
    
    
    int main()
    {    
         int a;
        
        cout<< "Password please: ";
        cin>> a;
        cin.ignore();
        if(a == 159753 ) {
         cout<<"Your are correct, proceed by hitting enter\n"
         
         cin.get();
         }
    
    do
    { 
        cout<<"Incorrect try again.\n";
    } while ( a != 159753 )
    It still does the thing where whatever I type in when I hit enter it closes no matter if its right or wrong.

    What is wrong here?
    Last edited by PublicAutopsy; 03-26-2007 at 02:53 AM.

  11. #11
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    1. You have created an infinite loop since you never ask the user to enter the password inside the loop, so a will always stay what the user entered in the first place and the condition of the while loop will never be met.

    2. There's a missing ; after the while ()

    3. There's a missing ; after the last cout

    4. the else if part is cut off from the corresponding if by the loop, this doesn't work.

    5. Here's your solution in pseudo-code
    Code:
    password = "secret"
    
    do
    {
        print "Please enter your Password"
        read userPassword
    }
    while (userPassword IS NOT password)
    Be aware that I intentionally removed every C++ syntax, so you have to change my code a fair bit. Also, you don't need the IF condition anymore since the loop condition does that for you already.

  12. #12
    Registered User
    Join Date
    Mar 2007
    Posts
    14
    Code:
    #include <iostream>
    
    
    int main()
    {    
         int a;
        ( a == 159753 )
    
    do
    { 
        cout<< "Password please: ";
        cin>> a;
        cin.ignore();
         
        cout<<"Incorrect try again.\n";
    } while ( a != 159753 );
    
    else if ( a == 159753 ) 
    {
         cout<<"You are correct, press enter.\n";
         
    }
    This is what I have now, same problem persists

  13. #13
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    1. You removed the if, yet you left the condition.

    2. You are already printing the incorrect message before even checking if what he enters is correct.

    3. You still have the "else if" part without corresponding if.

    4. Are you sure you're even trying ? You seem to just remove some random lines, without giving it the slightest thought. If you don't want to learn how to program, have your code written by someone else or find a program that does what you need. You can't code without using your brain.

  14. #14
    Registered User
    Join Date
    Mar 2007
    Posts
    14
    Sorry its 2 am here, I have a headache and I havnt lept isn over 24 hours.
    What im doing seems logical at the time lol guess not though.

    Im not going to bed until I get this.

    Do you think I should just start over with this program?

  15. #15
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Here:
    Code:
    #include <iostream>
    
    
    int main()
    {    
        int a;
        int password = 159753;
        do
        { 
            cout<< "Password please:";
            cin>> a;
            cin.ignore();
        } while ( a != password);
    
        cout<<"You are correct, press enter.\n";
    }
    Good Night

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