我试图在一个带do-while循环的情况下做一个while (true)循环,但是当我将while (true)放在这个例子中时,菜单不会返回到控制台,我需要关闭调试器并再次运行它--有人可以帮助我,帮助我熟悉c++。
这是我的代码:
do
{
std::cout << "[0] Quit\n"; // This is Option 0 of the Menu
std::cout << "[1] Infinite Health\n"; // This is Option 1 of the Menu
std::cout << "[2] Infinite Ammo\n"; // This is Option 2 of the Menu
std::cout << "[3] Infinite Rounds\n"; // This is Option 3 of the Menu
std::cin >> choice;
switch (choice) // This is to detect the Choice the User selected
{
case 0:
std::cout << "Why did you even open me to not use me :(\n";
return 0;
case 1:
std::cout << "You Have Activated Infinite Health!\n";
while (true)
{
int health = 1000;
WriteProcessMemory(phandle, (LPVOID*)(healthPtrAddr), &health, 4, 0);
}
break;
case 2:
std::cout << "You Have Activated Infinite Ammo On Primary Weapon!\n";
while (true)
{
int ammo = 500;
WriteProcessMemory(phandle, (LPVOID*)(ammoPtrAddr), &ammo, 4, 0);
}
break;
case 3:
std::cout << "You Have Activated Infinite Rounds On Primary Weapon!";
while (true)
{
int rounds = 200;
WriteProcessMemory(phandle, (LPVOID*)(roundsPtrAddr), &rounds, 4, 0);
}
break;
}
}
while (choice !=0);发布于 2021-12-22 18:51:43
是的,它不会返回,因为它阻塞了程序。
为了解决您的问题,可以将循环放入另一个线程中。
如果使用以下方法包括线程库:
#include <thread>然后,您必须定义应该运行的函数:
void keepHealth() {
while (true)
{
int health = 1000;
WriteProcessMemory(phandle, (LPVOID*)(healthPtrAddr), &health, 4, 0);
}
}现在,您可以在另一个线程中执行此函数:
std::thread task1(keepHealth);如果您想传递像句柄这样的参数,就必须在函数头中写入它们:
void keepHealth(void* pHandle, void* healthPtrAddress) {
while (true)
{
int health = 1000;
WriteProcessMemory(phandle, (LPVOID*)(healthPtrAddr), &health, 4, 0);
}
}然后像这样递过来:
std::thread task1(keepHealth, pHandle, healthPtrAddress);https://stackoverflow.com/questions/70453709
复制相似问题