Thread: Multiple NetSends

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    20

    Multiple NetSends

    Hi everyone. I would like a little help with this code I'm working on. It's purpose is to ask the user for a computer's IP, a message to be sent, and number of messages. Then it will net send that computer the supplied amount of messages.
    The problem I'm having is with this:
    Code:
    void sendmsg (string a, string b)
    {
    system("net send a b");
    }
    How would I make the console understand that a and b are variables. Here is all the code.
    Code:
    #include <iostream>
    #include <stdio.h>
    #include <windows.h>
    #include <string.h>
    using namespace std;
    
    void sendmsg (string a, string b)
    {
    system("net send a b");
    }
    
    int main(void)
    {
    string ipaddr;
    string msg;
    int nummsg;
    char cnfrm [1];
    int n = 0;
    
    cout << "Enter Computer Name or IP:\n ";
    cin >> ipaddr;
    cout << "Enter Message:\n ";
    cin >> msg;
    cout << "Enter Number of Messages to be Sent:\n ";
    cin >> nummsg;
    cout << "Press y to send the message or n to quit:\n ";
    cin >> cnfrm;
    
    if (cnfrm == "y") {
        while (n < nummsg) {
            sendmsg (ipaddr, msg);
            }
        }
    else 
        {
        exit(0);
        }
        
    return 0;
    }
    There are probablly a ton of mistakes, but I am definitely a beginner in C++.
    Thank you.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You'd use sprintf() to print to the string, and then call system() with it. Or you could use stringstreams, but those I'm not familiar with.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Here is a quick example of using stringstreams
    Code:
    #include <iostream>
    #include <sstream>
    
    int main()
    {
    	std::stringstream stream;
    	std::string aString = "This is a string!";
    	int number=10;
    	stream << "Here is a string: " << aString << " And here is a number: " << number;
    	std::cout << stream.str() << std::endl;
    }
    To reset the content of the string you would just do this (at least this is how I do it):
    Code:
    stream.str("");
    stream.clear();
    And finally, to get a char array from the stream you do this:
    Code:
    stream.str().c_str();

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    If you were writing the string to the screen, functions like printf would allow you to just write something like %d in your string, telling it there was a variable there, and you would then list all the variables in order after your string. System however, does not format your string for you. You need to make a string, and there are functions that do that for you (as mentioned above), and then pass that string to system().

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    20
    Thank you for all the help. I'm trying to do this with string streams thanks to ya'lls advise and I've almost got it. This is a pretty basic question, but how would I make theur be spaces in between each word in this code:
    Code:
    std::stringstream stream;
    std::string ipaddr = "xxx.xxx.xxx.xxx";
    std::string message = "hello";
    stream << "net send" << ipaddr << message;
    That outputs:
    net sendxxx.xxx.xxx.xxxhello

    The desired output is:
    net send xxx.xxx.xxx.xxx hello

    Basically, I want to add spaces.

    My other question is how could I use std::cin (or whatever else I should use) to store input as a string.
    For instance, what would I have to do to assign the variable ipaddr whatever the user inputs.
    Code:
    std::string ipaddr;

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    try:
    Code:
    stream<<"net send "<<ipaddr<<' '<<message;
    (notice the space after "send")

    And for input you should be able to do:
    Code:
    std::cin>>ipaddr;
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    20
    Thanks. Seems to be working. Now I just have to piece stuff together. Thanks everyone.
    EDIT: Damn. One more thing.
    Code:
    stream << "net send " << ipaddr << ' '<< msg;
    std::system("stream");
    How do I get system to understand that stream is a stream and that it should not actually try to run the DOS command "stream" which doesnt exist. I get an error if I try without the quotes or with ' 's. Any ideas?
    Last edited by zach0616; 08-18-2004 at 09:17 PM.

  8. #8
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Quote Originally Posted by Shakti
    Here is a quick example of using stringstreams
    And finally, to get a char array from the stream you do this:
    Code:
    stream.str().c_str();


    Edit: Uhh weird...If i edit it I have to have at least 4 characters (besides the quote)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  9. #9
    Registered User
    Join Date
    Aug 2004
    Posts
    20
    Maybe I'm just dense or something.
    Here's part of the code.
    Code:
    stream << "net send " << ipaddr << ' '<< msg;
    stream.str().c_str();
    
    system ("PAUSE");
    while (n < nummsg) {
            std::system(stream);
            n++;
    }
    When I do it like that, I get the error "invalid conversion from `void*' to `const char*' ". When I put quotes around stream it compiles, but like I said in the other post, doesn't work correctly. Any ideas what I'm doing wrong? Sorry to bug everyone with questions.

  10. #10
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    stream.str().c_str() will return a 'const char*' that you can pass to std::system.
    Code:
            std::system(stream.str().c_str());
    - or -
    Code:
            const char* mystr = stream.str().c_str();
            std::system(mystr);

  11. #11
    Registered User
    Join Date
    Aug 2004
    Posts
    20
    Ok thanks. Everything works fine except the string msg will only hold the first word of your message. Can strings not hold space?

  12. #12
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You need to use getline() in order to capture spaces.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. why Multiple define error ...
    By nilathinesh in forum C Programming
    Replies: 2
    Last Post: 10-19-2006, 06:31 AM
  3. Phantom redefinition
    By CodeMonkey in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2005, 05:42 PM
  4. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM