Can I improve this code?

OK, so, I don't have much experience in C++, this is some practice code i typed up and i would like to know if there is anything that could be improved?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 #include <iostream>
#include <string>
using namespace std;
int main()
{
	string username, password, confirmuser, confirmpass;
	cout << "Enter your desired username: ";
    getline(cin, username);
    cout << "Enter your desired password: ";
    getline(cin, password);
    cout << "Account created, login now. \n Username: ";
    getline(cin, confirmuser);
    cout << "\n Password: ";
    getline(cin, confirmpass);
    if (username==confirmuser && password==confirmpass)
    {
    		cout << "Welcome, " << username;
    }
    else
    {
    	cout << "invalid username/password";
    }
    return 0;
}
Last edited on
closed account (48T7M4Gy)
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
#include <iostream>
#include <string>

using namespace std; // OK for student stuff but not good in practice

int main()
{
    string username, password, confirmuser, confirmpass;
	
    cout << "Enter your desired username: ";
    getline(cin, username);
    
    cout << "Enter your desired password: ";
    getline(cin, password);
    
    cout << "Account created, login now. \n Username: ";
    getline(cin, confirmuser);
    
    cout << "\n Password: ";
    getline(cin, confirmpass);
    
    // VALIDATE PASSWORD
    if (username == confirmuser)
    {
    	if (password == confirmpass)
    	{
    		cout << "Welcome, " << username;
    	}
    }
    else
    {
    	cout << "Invalid username/password"; //<--
    }
    
    return 0;
}


Not bad, seems to work OK and is pretty much self-documenting. Possibly put a couple of comments in as shown and maybe a starting comment overview with revison dates would be good for later reminder/maintenance.

Use whitespace to make reading easier. Typo!

:)

PS Logic of username and password should be combined with &&, not separate interior if.
Last edited on
Yeah, after making this i realized i could of just used a boolean and did
1
2
3
4
if (username==confirmuser && password==confirmpass)
    {
    		cout << "Welcome, " << username;
    }

thanks for your input it's appreciated
Last edited on
if there is anything that could be improved?

an obvious suggestion would be to avoid using namespace std …
http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
Is there a limit in how much a code can be improved? :-)
If you asked that question ten times, you would receive ten different hints.
An other thing you could do would be to declare your variables just when you are about to use them. There are many advantages, one of which is that they could be automatically destroyed when they are no more of any use.
For example:
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>

int main()
{
    std::cout << "\nEnter your desired username: ";
    std::string username;
    std::getline(std::cin, username);

    std::cout << "Enter your desired password: ";
    std::string password;
    std::getline(std::cin, password);

    std::cout << "\nAccount created, login now.";
    while(true) {
        std::cout << "\nUsername: ";
        std::string confirmuser;
        std::getline(std::cin, confirmuser);

        std::cout << "Password: ";
        std::string confirmpass;
        std::getline(std::cin, confirmpass);

        if(username==confirmuser && password==confirmpass) {
            std::cout << "Welcome, " << username << '\n';
            break;
        }
        std::cout << "Invalid username/password";
    }

    return 0;
}

closed account (48T7M4Gy)
Is there a limit in how much a code can be improved? :-)


https://en.wikipedia.org/wiki/Program_optimization
https://en.wikipedia.org/wiki/Amdahl%27s_law
https://en.wikipedia.org/wiki/Big_O_notation

and ...
https://en.wikipedia.org/wiki/Unemployment ... so don't fiddle too much.
Topic archived. No new replies allowed.