Thread: limited password entry attempts (console app) - from "Jumping into C++"

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    18

    limited password entry attempts (console app) - from "Jumping into C++"

    Hi,
    I'm new at C++
    I need help with my code.
    I want to be able to output a message when all attempts have been used up, however my program just ends when attempts reach 0.
    I know its a super easy fix, I just can't find out what it is.
    Thankyou!

    Code:
    int main()
    {
     string correctPassword = "OpenSesame";
     string password;
     
     for (int attempts = 4; attempts > 0; attempts--)
     {
      cout << "Please enter your password: ";
      cin >> password;
      
      if(password == correctPassword)
      {
       cout << "CORRECT PASSWORD ENTERED!\n\n" << "Please wait . . . \n\n\n";
       break;
      }
      
      else if(attempts == 0)
      {
       cout << "WARNING: All attempts failed! System shutting down . . .\n\n";
      }
      else
      {
       cout << "INCORRECT PASSWORD ENTERED!\n\n";
       cout << "Attempts left: " << attempts - 1 << "\n\n";
      }
     }
     return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2016
    Posts
    18

    Oops worked it out :D hahahaha

    Code:
    int main()
    {
     string correctPassword = "OpenSesame";
     string password;
     
     for (int attempts = 4; attempts >= 0; attempts--) // greater than or equal to
     {
      cout << "Please enter your password: ";
      cin >> password;
      
      if(password == correctPassword)
      {
       cout << "CORRECT PASSWORD ENTERED!\n\n" << "Please wait . . . \n\n\n";
       break;
      }
      
      else if(attempts == 0)
      {
       cout << "WARNING: All attempts failed! System shutting down . . .\n\n";
      }
      else
      {
       cout << "INCORRECT PASSWORD ENTERED!\n\n";
       cout << "Attempts left: " << attempts << "\n\n"; // removed the offset
      }
     }
     return 0;
    }

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Good on you.

    I recommend at least a 3 space indent (I use 4) for readability.

    Writing code that works is good, but writing code that readable and maintainable by others is the best (TM).

    gg

  4. #4
    Registered User
    Join Date
    Mar 2016
    Posts
    18
    My code has four spaces aka TAB. . .
    However when I copy it and paste it into the New Thread window, it changes removes spaces, I have no idea why.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I also do not use tabs since they can be interpreted as 1 to 8 (or more) spaces.

    A space is a space

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on "Jumping into C++" Chapter 13 practice problem 1
    By Ben Sturm in forum C++ Programming
    Replies: 9
    Last Post: 04-01-2015, 01:16 PM
  2. dynamic memory allocation problem from "jumping into C++"
    By tomatitobean in forum C++ Programming
    Replies: 8
    Last Post: 02-08-2015, 07:56 AM
  3. Pointer jumping over adress in "uneven" array.
    By Sinery in forum C Programming
    Replies: 3
    Last Post: 01-02-2015, 01:48 PM
  4. Replies: 18
    Last Post: 03-07-2013, 06:55 AM
  5. Replies: 5
    Last Post: 10-12-2012, 06:17 AM