Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Hosted Network C++ program

Tags:
  1. Nov 4, 2016 #1
    Hi, I've been recently started programming, and wanted to make program I can use in everyday life. I use CMD hosted network every day on my other PC, and wanted to make program in witch I can stop/start/restart hosted network.
    This is current progress:

    Code (Text):

    #include <iostream>

    int main ()    {
        using namespace std;
     
        string start;
        string stop;
        string restart = stop + start;
        string answer;
     
        cout << "\n\n        Hosted Network Control        \n\n" << endl;
        cout << "\nStop Hosted Network\n" << endl << "\nStart Hosted Network\n" << endl << "\nRestart Hosted Network\n" << endl;
        cin >> answer;
     
        if (answer == start)
        {
        cout << "\n        Hosted Network Has Been Started.    \n" << endl;
        system("netsh wlan start hostednetwork");
        system("pause>nul");
        }
        if (answer == stop)
        {
        cout << "\n        Hosted Network Has Been Stopped.   \n" << endl;
        system("netsh wlan stop hostednetwork");
        system ("pause>nul");
        }
        if (answer == restart)
        {
        system("netsh wlan stop hostednetwork");
        system("netsh wlan start hostednetwork");
        cout << "\n        Hosted Network Has Been Restarted.    \n" << endl;
        system("pause>nul");
        }
     
     
        system("pause");
        return 0;
    }
    The thing is, when I input stop, start or restart, no text appears on the screen. Can somebody explain it to me and correct my script. Thanks!
     
    Last edited by a moderator: Nov 4, 2016
  2. jcsd
  3. Nov 4, 2016 #2

    Mark44

    Staff: Mentor

    What's the purpose of your declaration for restart?
    Code (C):

        string start;
        string stop;
        string restart = stop + start;
    start and stop are empty strings, which causes restart to be initialized also as an empty string.
    It appears to me that you are confusing the names of variables with their contents. The start, stop, and restart variables do not contain the strings "start", "stop", and "restart".
     
  4. Nov 4, 2016 #3
    Oh, yeah, I was confused about it, I'll try to fix it. Thanks!
     
  5. Nov 4, 2016 #4
    I've made new code, but problem is still the same. When I input start/stop/reset it just says "Press any key to continue..."
     
  6. Nov 4, 2016 #5

    QuantumQuest

    User Avatar
    Gold Member

    What is your new code?
     
  7. Nov 4, 2016 #6
    Why initialize are::strings at all?

    If (answer == "start")

    Also, you are aware that you can create an alias in your .bash_rc file for restarting your network since you're just using system anyway.


    What do you expect > nul to do? Did you mean >/dev/null?
     
  8. Nov 5, 2016 #7
    UPDATE: I just fixed the code, I didn't add "" to start/pause/restart, it works now. Only thing that annoys me is cmd says that hosted network started/ stopped and I want to print only my text. Is there a way to fix it?
     
  9. Nov 5, 2016 #8
    Here's the new program

    //Hosted Network Control
    #include <iostream>
    int main (){
    using namespace std;

    cout << "HOSTED NETWORK CONTROL" << endl;
    cout << "1. Start" << endl << "2. Stop" << endl << "3. Restart" << endl;

    string answer;
    cin >> answer;

    if ((answer == "start") || (answer == "1")) {
    system ("netsh wlan start hostednetwork");
    cout << "Hosted Network Has Been Started" << endl;
    }

    if ((answer == "stop") || (answer == "2")) {
    system ("netsh wlan stop hostednetwork");
    cout << "Hosted Network Has Been Stopped" << endl;
    }

    if ((answer == "restart") || (answer == "3")) {
    system ("netsh wlan stop hostednetwork");
    system ("netsh wlan start hostednetwork");
    cout << "Hosted Network Has Been Restarted" << endl;
    }

    system ("pause>nul");
    return 0;
    }
     
  10. Nov 5, 2016 #9

    Mark44

    Staff: Mentor

    Sure -- don't call the system() function. Just send the strings to the console using the cout stream.

    Code (C):
    cout << "netsh wlan stop hostednetwork" << endl;
     
  11. Nov 6, 2016 #10
    I think the poster is asking how to get what's in the system call to still work, but not print it's stdout.

    In the command line, you can tell something not to print by redirecting its output into a null pipe:

    echo "hello"

    echo "hello" > /dev/null

    Try them both and see what happens. It will still print the std errors for you though, which is nice. If you want to remove those too, you can redirect stderr to stdout, which then both go to /dev/null
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Hosted Network C++ program
  1. C program (Replies: 0)

  2. C programming (Replies: 9)

  3. C++ program (Replies: 2)

  4. C++ programming (Replies: 8)

Loading...