Basic stuff but dont work.

This is a discussion on Basic stuff but dont work. within the C++ Programming forums, part of the General Programming Boards category; Been trying to get a password working but it dont seem to work. when u enter the wrong password it ...

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    Basic stuff but dont work.

    Been trying to get a password working but it dont seem to work.

    when u enter the wrong password it will ask you to re-enter it but you can put any number in and it will still work.

    how can i stop this and make it carry on looping untill the write password is entered?

    Code:
    int password()
    
    {
    int password;
    password==1205;
    cout << "Plese enter your 4 digit password that was provided on purchase";
    cin >> password;
    
    int error=3; 
    
    if (password !=1205)
    
        {
         cout<<  "Plese re-enter the password";
         error --;
         cin >> password;
         }
    
    if (password ==1205)
    
        {
            return 0;
            system ("CLS");
        }
    
         
    if (error ==0)
    {
        cout << "WASHING MACHINE HAS BEEN DISABLED PLEASE CONTACT YOUR SUPPLIER";     
        system("pause"); 
    }
    }

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Code:
    int error = 3;
    
    while ( Password != Right && error != 0 )
    {
      error --;
    
      cout<< Enter the password
      cin >> Password
    }
    Also


    Code:
    if (password !=1205)
    
    ...
    
    if (password ==1205)
    should be

    Code:
    if ( password!=whatever )
    {
      do the wrong password stuff
    }
    else
    {
      do the right password stuff
    }
    Also, you only retutn a 0 if the password is correct, which is OK, but you should return something if the number of tries are gone and the pword's still wrong!
    Last edited by twomers; 09-04-2006 at 07:09 AM.

  3. #3
    すまん Hikaru's Avatar
    Join Date
    Aug 2006
    Posts
    46
    Quote Originally Posted by peckitt99
    Code:
    cout << "WASHING MACHINE HAS BEEN DISABLED PLEASE CONTACT YOUR SUPPLIER";
    Where in the world do washing machines require a passcode and go out of order if it's wrong?
    Quote Originally Posted by peckitt99
    how can i stop this and make it carry on looping untill the write password is entered?
    You will need a loop. Only stop the loop when the passcode is right or when the number of errors reaches the limit. cin can fail if a number format isn't read so you will need to test for that too. I wrote something that's probably too heavy duty for what you want, but I got caught up in getting it to work just like I wanted on every error condition. streams are wierd and fun at the same time. I also set the code up so that you can test it with other streams.
    Code:
    #include <cassert>
    #include <ios>
    #include <iostream>
    #include <limits>
    #include <stdexcept>
    #include <string>
    
    namespace Hikaru
    {
        class format_error: public std::runtime_error
        {
        public:
            explicit format_error(const std::string& message)
                : runtime_error(message) {}
    
            virtual ~format_error() {}
        };
    
        bool PassCode(std::istream& is, std::ostream& err = std::cerr)
        {
            const int errorsAllowed = 3;
    
            int errors = 0;
    
            do
            {
                const int code = 1205;
    
                int userCode;
    
                if (!(is >> userCode))
                {
                    if (is == std::cin)
                    {
                        err << "Number format required\n";
                        is.clear();
                    }
                    else
                    {
                        throw format_error("Bad stream format");
                    }
                }
                
                if (is == std::cin)
                {
                    assert(is.rdbuf()->in_avail() > 0 && "\"cin >> number\" always leaves '\n'");
                    is.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                }
                
                if (userCode != code)
                {
                    if (is == std::cin)
                    {
                        err << "Please try again> ";
                    }
    
                    ++errors;
                }
                else
                {
                    break;
                }
            }
            while (errors < errorsAllowed);
    
            assert(errors <= errorsAllowed);
    
            return errors < errorsAllowed;
        }
    }
    I'm sorry, working problems like these is a great way to test new stuff I've learned.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,439
    Quote Originally Posted by Hikaru
    Where in the world do washing machines require a passcode and go out of order if it's wrong?
    When you write a program that says so.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If someone can't write a simple loop I doubt they'd know what a virtual destructor is.

    Here's a tutorial on loops: http://www.cprogramming.com/tutorial/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.

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Another alternative is to use an infinite loop to keep asking the question untill correct input, then break the loop when it is correct.

    Code:
    int i = 0;
    
    for (;;)
    {
        // code
    condition is true = break loop
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  2. Developers Wanted
    By Quasicom in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 08-24-2005, 12:46 AM
  3. 2-d object avoidance. Help please! (basic stuff, I think)
    By charbach007 in forum Game Programming
    Replies: 4
    Last Post: 06-15-2004, 03:49 PM
  4. College stuff
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 06-03-2004, 11:22 AM
  5. Your stuff
    By smog890 in forum C Programming
    Replies: 6
    Last Post: 06-13-2002, 11:50 PM

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