Exercises for Beginners 3

closed account (1v5E3TCk)
1. Implement a simple "password" system.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>
#include <string>

using namespace std;

int main () 
{
    int password=1234 ; //anything you want as the password
    int password2 ;
    
    cout<<"Input the password."<<endl;
    cin>>password2;
    
    if (password==password2)
    {
        //anything you want                    
    }
    
}


2.Expand the password checking program and make it take multiple
usernames, each with their own password, and ensure that the right username is used for the right password. Provide the ability to prompt user's again if the first login attempt failed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>

using namespace std;

int main () 
{   
    string userdef; // a string for storing input
    string username1;
    username1= "arda"; //anything you want as first username
    
    string username2;
    username2= "ahmet";  //anything you want as second username
    
    cout<<"NOTE:The program is case secsitive."<<endl;
    cout<<"Input the username."<<endl;
    cin>>userdef;
    
//if input is username1    
    
    if (userdef==username1) //if input is username1  
    {
    
    int password=1234 ;
    int password2 ;
    
    cout<<"Input the password."<<endl;
    cin>>password2;
    
    if (password==password2)  //compare the password with first user's password
    {
         system("pause");                      
    }
    }
    
    else if (userdef==username2)  //if input is username1 
    {
    
    int password=9876 ;  //compare the password with second user's password
    int password2 ;
    
    cout<<"Input password."<<endl;
    cin>>password2;
    
    if (password==password2)
    {
      system("pause");                       
    }
    }
    else {cout<<"Wrong user name."<<endl;
    system("pause");}    
    }


3.Write a password prompt that gives a user only a certain number of password entry attempts—so that the user cannot easily write a password cracker.

3.1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>

using namespace std;

int main () 
{
    
    int a=2;
    int password=1234 ;
    int password2 ;
    
    //Integer b counts how many times did you tried
    //And integer a counts how many tries left to show
    for (int b=0; b<3; ++b){

    cout<<"input the password."<<endl;
    cin>>password2;

    if (password==password2){break;}
    if (a==0){break;}

    cout<<"Wrond input. "<<a<<" tries left.\n";
    --a;
    }

    if (password==password2)
    {       
      system("pause");
    }    
    }


3.2(coded by cire note: needs c++11)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <thread>
#include <chrono>

void initiateSelfDestructSequence() ;

int main () 
{
    const std::string actualPassword = "r2Smoke" ;
    const unsigned maxAttempts = 3 ;

    std::string pw ;
    unsigned attempts = 0 ;

    while ( pw != actualPassword && attempts < maxAttempts )
    {
        std::cout << "\npassword> " ;
        std::getline(std::cin, pw) ;
        ++attempts ;
    }

    if ( pw == actualPassword )
        std::cout << "Access granted. Disaster averted.\n" ;
    else
    {
        std::cout << "Access denied.\n" ;
        initiateSelfDestructSequence() ;
    }
}

void initiateSelfDestructSequence()
{
    std::cout << "Computer will now self destruct.  You have 5 seconds to save yourself!\n" ;

    for ( unsigned i=5; i > 0 ; --i )
    {
        std::cout << i << std::flush ;
        std::this_thread::sleep_for(std::chrono::milliseconds(250)) ;

        for ( unsigned j=0; j<3; ++j )
        {
            std::cout << '.' << std::flush ;
            std::this_thread::sleep_for(std::chrono::milliseconds(250)) ;
        }
        
        std::cout << "\b\b\b\b    \b\b\b\b" ;
    }

    std::cout << "BOOM!" << std::endl ;
}
Last edited on
If you're using break inside a for loop, there's a decent chance it can be rewritten so that the actual condition the loop depends on to execute is in the loop condition. Sometimes a break may make more sense. Here, I would say it is out of place.

For instance, the following is equivalent to your number 3 code snippet (with the self destruct sequence added just for fun - it requires C++11.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <thread>
#include <chrono>

void initiateSelfDestructSequence() ;

int main () 
{
    const std::string actualPassword = "r2Smoke" ;
    const unsigned maxAttempts = 3 ;

    std::string pw ;
    unsigned attempts = 0 ;

    while ( pw != actualPassword && attempts < maxAttempts )
    {
        std::cout << "\npassword> " ;
        std::getline(std::cin, pw) ;
        ++attempts ;
    }

    if ( pw == actualPassword )
        std::cout << "Access granted. Disaster averted.\n" ;
    else
    {
        std::cout << "Access denied.\n" ;
        initiateSelfDestructSequence() ;
    }
}

void initiateSelfDestructSequence()
{
    std::cout << "Computer will now self destruct.  You have 5 seconds to save yourself!\n" ;

    for ( unsigned i=5; i > 0 ; --i )
    {
        std::cout << i << std::flush ;
        std::this_thread::sleep_for(std::chrono::milliseconds(250)) ;

        for ( unsigned j=0; j<3; ++j )
        {
            std::cout << '.' << std::flush ;
            std::this_thread::sleep_for(std::chrono::milliseconds(250)) ;
        }
        
        std::cout << "\b\b\b\b    \b\b\b\b" ;
    }

    std::cout << "BOOM!" << std::endl ;
}
Last edited on
closed account (1v5E3TCk)
it is better than my code thx i will add it
Topic archived. No new replies allowed.