首页 > 解决方案 > 我如何在 C++ 中获取用户输入来完成我的 system() 函数

问题描述

我想获取用户输入并使用它来完成系统()中的以下功能


#include <iostream>
using namespace std;

int main ()
{

    int bssid;
    int chanel;
    cout<<"Enter the targets Bssid = "<<endl;
    cin>>bssid;
    cout<<"Enter the channel"<<endl;
    cin>>chanel;

    system("airodump-ng --bssid" + bssid  " -c " + channel + " wlan0mon");

    return 0;
}

标签: c++gcc

解决方案


std::string结合std::to_string()为您提供了一个简单的机会来格式化字符串。

std::string Input = "hackwifi " + std::to_string(bssid) + " --emacsoversendmail " + std::to_string(channel);

然后,获得一个空终止的 c 字符串调用std::string::c_str()

system(Input.c_str());

注意:此方法在 C++11 及以上版本中受支持


推荐阅读