Hỏi cách rút gọn chương trình

Bác nào chỉ em rút gọn cái chương trình này với, nghe nói dùng hàm có thể làm gọn cái đoạn code này đúng ko các bác

// #include <iostream>
using namespace std;
void pass();
void main()
{
	void pass();
	{
		int pass;
		int passcor;
		cout << "Please enter password to unlock the program" << endl<<"Password=";
		cin >> pass;
		passcor = 123;
		if (pass == passcor)
		{
			cout << "Your code is: " << "Let me hit!" << endl;
			system("pause");
		}
		else
		{
			cout << "Wrong Password (2 next)" <<endl;
			cout << "Please enter password to unlock the program" << endl << "Password=";
			cin >> pass;
			passcor = 123;
			if (pass == passcor)
			{
				cout << "Your code is: " << "Let me hit!" << endl;
				system("pause");
			}
			else
			{
				cout << "Wrong Password (1 next)" << endl;
				cout << "Please enter password to unlock the program" << endl << "Password=";
				cin >> pass;
				passcor = 123;
				if (pass == passcor)
				{
					cout << "Your code is: " << "Let me hit!" << endl;
					system("pause");
				}
				else
				{
					cout << "Wrong Password (0 next)" << endl;
					cout << "Please enter password to unlock the program" << endl << "Password=";
					cin >> pass;
					passcor = 123;
					if (pass == passcor)
					{
						cout << "Your code is: " << "Let me hit!" << endl;
						system("pause");
					}
					else
					{
						cout << "No more mercy with you! End program" << endl;
						system("pause");
					}
				}
			}
		}
	}
}

Gợi ý là dùng vòng lặp O3O

3 Likes

Dùng vong lặp while or do … while cũng được !

2 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?