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

  • Context: C/C++ 
  • Thread starter Thread starter ExplosiveMercury
  • Start date Start date
  • Tags Tags
    C++ Network Program
Click For Summary

Discussion Overview

The discussion revolves around creating a C++ program to control a hosted network using command line instructions. Participants are sharing their code snippets, troubleshooting issues, and refining their approach to ensure the program works as intended.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant shares an initial code snippet but encounters issues with output not appearing when commands are entered.
  • Another participant points out that the variables for start, stop, and restart are not initialized with the expected string values, leading to confusion.
  • Subsequent posts reveal attempts to fix the code, with one participant noting that they forgot to include quotation marks around command strings, which resolved their issue.
  • There are suggestions about using command line aliases and redirecting output to suppress unwanted messages from the command line.
  • Participants discuss the implications of using the system() function and how to manage output from command line commands effectively.

Areas of Agreement / Disagreement

Participants generally agree on the need to properly initialize variables and manage output, but there are differing opinions on the best methods to achieve the desired functionality without unwanted command line messages.

Contextual Notes

Some participants express uncertainty about the behavior of the system() function and how to redirect output effectively. There are also unresolved questions about the best practices for managing command line interactions in C++.

Who May Find This Useful

This discussion may be useful for beginner programmers interested in C++ and those looking to control system commands through programming, particularly in network management contexts.

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;
 
    count << "\n\n        Hosted Network Control        \n\n" << endl;
    count << "\nStop Hosted Network\n" << endl << "\nStart Hosted Network\n" << endl << "\nRestart Hosted Network\n" << endl;
    cin >> answer;
 
    if (answer == start)
    {
    count << "\n        Hosted Network Has Been Started.    \n" << endl;
    system("netsh wlan start hostednetwork");
    system("pause>nul");
    }
    if (answer == stop)
    {
    count << "\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");
    count << "\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   Reactions: 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;

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

string answer;
cin >> answer;

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

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

if ((answer == "restart") || (answer == "3")) {
system ("netsh wlan stop hostednetwork");
system ("netsh wlan start hostednetwork");
count << "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 count stream.

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

C:
count << "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

  • · Replies 5 ·
Replies
5
Views
3K
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 14 ·
Replies
14
Views
35K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 29 ·
Replies
29
Views
10K