Two outputs instead of one

Pages: 12
seeplus, THANK YOU SO MUCH!!! I’m in a Facebook group and tried to get help but instead they turned ability for me to post and comment, but this website is pretty cool.
Thank you so much for help guys, especially you seeplus! ;)
Happy coding!
You might want to change your design a little - using some more functions can make your main() cleaner. Also putting login into the processing loop gives as a possible:

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <string>
#include <iomanip>

using std::cout;
using std::cin;

void space() {
	const static std::string lines(76, '\n');

	std::cout << lines;
}

bool login(const std::string& user, const std::string& pswrd)
{
	std::string checkuser, checkpswrd;

	cout << "Login:\n";
	cout << "Write username: ";
	cin >> checkuser;

	cout << "\nWrite password: ";
	cin >> checkpswrd;
	cout << '\n';

	if (checkuser == user && checkpswrd == pswrd) {
		cout << "Login Complete!\n";
		return true;
	}

	cout << "Incorrect password or username\n\n";
	return false;
}

void setcred(std::string& user, std::string& pswrd)
{
	for (bool while_user {true}; while_user; ) {
		cout << "Write new username: ";
		cin >> user;

		std::string yesno;
		cout << "Save username? Write yes or no: ";
		cin >> yesno;

		if ("yes" == yesno or "y" == yesno) {
			while_user = false;
			cout << "Saved!\n";
		}
	}

	for (bool while_pswrd {true}; while_pswrd; ) {
		cout << "Write new password: ";
		cin >> pswrd;

		std::string yesno2;
		cout << "Save password? Write yes or no: ";
		cin >> yesno2;

		if ("yes" == yesno2 or "y" == yesno2) {
			while_pswrd = false;
			cout << "Saved!\n";
		}
	}
}

int main()
{
	std::string user, pswrd;

	setcred(user, pswrd);
	space();

	cout << "NikPolo Command Terminal \n v.0.1.3\n";
	std::string checkuser, checkpswrd;

	for (bool whileterminal {true}, whilelogin {true}; whileterminal; ) {
		if (whilelogin)
			whilelogin = !login(user, pswrd);
		else {
			std::string commands;

			cout << "\npolo - " << user << " >> ";
			std::getline(cin >> std::ws, commands);

			if (commands == "exit") {
				cout << "\nConfirming exit...\n";
				//system("pause");
				whileterminal = false;
			}
		}
	}
}

You could also have login as a command to be entered - like exit is a command. Perhaps something like:

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <string>
#include <iomanip>

using std::cout;
using std::cin;

void space() {
	const static std::string lines(76, '\n');

	std::cout << lines;
}

bool login(const std::string& user, const std::string& pswrd)
{
	std::string checkuser, checkpswrd;

	cout << "Login:\n";
	cout << "Write username: ";
	cin >> checkuser;

	cout << "\nWrite password: ";
	cin >> checkpswrd;
	cout << '\n';

	if (checkuser == user && checkpswrd == pswrd) {
		cout << "Login Complete!\n";
		return true;
	}

	cout << "Incorrect password or username\n\n";
	return false;
}

void setcred(std::string& user, std::string& pswrd)
{
	for (bool while_user {true}; while_user; ) {
		cout << "Write new username: ";
		cin >> user;

		std::string yesno;
		cout << "Save username? Write yes or no: ";
		cin >> yesno;

		if ("yes" == yesno or "y" == yesno) {
			while_user = false;
			cout << "Saved!\n";
		}
	}

	for (bool while_pswrd {true}; while_pswrd; ) {
		cout << "Write new password: ";
		cin >> pswrd;

		std::string yesno2;
		cout << "Save password? Write yes or no: ";
		cin >> yesno2;

		if ("yes" == yesno2 or "y" == yesno2) {
			while_pswrd = false;
			cout << "Saved!\n";
		}
	}
}

int main()
{
	std::string user, pswrd;

	setcred(user, pswrd);
	space();

	cout << "NikPolo Command Terminal \n v.0.1.3\n";
	std::string checkuser, checkpswrd;

	for (bool whileterminal {true}, whilelogin {true}; whileterminal; ) {
		std::string commands;

		cout << "\npolo - " << (whilelogin ? "" : user) << " >> ";
		std::getline(cin >> std::ws, commands);

		if (whilelogin && commands == "login")
			whilelogin = !login(user, pswrd);
		else if (!whilelogin && commands == "exit") {
			cout << "\nConfirming exit...\n";
			//system("pause");
			whileterminal = false;
		} else
			cout << "Invalid command\n";
	}
}

Also, instead of using >> to obtain username/password, consider using getline(). Then you won't need a complicated getline() for the command. This complication is only due to the mixing of >> with getline(). Doing this would also allow spaces in the username and password, which using >> doesn't.
Topic archived. No new replies allowed.
Pages: 12