C/C++ How to Create a Hosted Network Program in C++?

AI Thread Summary
The discussion revolves around creating a program to control a hosted network using C++. The initial code provided by the user contained issues where the program did not display any output when commands like "start", "stop", or "restart" were entered. The problem was identified as the use of uninitialized string variables for comparison. After corrections, the user updated the code to include direct string comparisons, which resolved the output issue.However, the user expressed frustration that the command line still displayed default messages from the `netsh` commands, despite wanting only their custom messages to appear. Suggestions were made to suppress the output of the `system()` calls by redirecting the command line output to null, allowing the program to execute the commands without displaying the default messages. The conversation highlighted the importance of proper variable initialization and output management in programming.
ExplosiveMercury
Messages
8
Reaction score
0
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:
#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:
Technology news on Phys.org
ExplosiveMercury said:
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:

C:
#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!

What's the purpose of your declaration for restart?
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".
 
  • Like
Likes ExplosiveMercury
Oh, yeah, I was confused about it, I'll try to fix it. Thanks!
 
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..."
 
ExplosiveMercury said:
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..."

What is your new code?
 
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?
 
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?
 
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;
}
 
ExplosiveMercury said:
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?
Sure -- don't call the system() function. Just send the strings to the console using the cout stream.

C:
cout << "netsh wlan stop hostednetwork" << endl;
 
  • #10
Mark44 said:
Sure -- don't call the system() function. Just send the strings to the console using the cout stream.

C:
cout << "netsh wlan stop hostednetwork" << endl;
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
 

Similar threads

Back
Top