Computing.Net > Forums > Programming > C++, system(), and variables

C++, system(), and variables

Reply to Message Icon

Original Message
Name: RTAdams89
Date: February 10, 2005 at 19:33:16 Pacific
Subject: C++, system(), and variables
OS: xp home
CPU/Ram: na
Comment:

I am using the "sytem("net send") command in a C++ program I am writing. I want the actual command that is sent to the consol to be "net send user message". Where user is a variable and message is a variable. The problem is if I type:

string user = ryan;
string message = hi;
system(net send user message);

it gives and error abotu nto having quotes.
If i type:

string user = ryan;
string message = hi;
system("net send user message");

It tries to send "message" to "user".

And also ("net send" user message) dosn't work.

So what do i nedd to do?


-Ryan Adams
Ryan's
Custom Cables


Report Offensive Message For Removal

Response Number 1
Name: sohaibi
Date: February 10, 2005 at 23:05:43 Pacific
Subject: C++, system(), and variables
Reply: (edit)

Try this!

char* l_lpszUser = "ryan";
char* l_lpszMsg = "hi";
char l_szCommand[50];

ZeroMemory(l_szCommand, sizeof(l_szCommand));
sprintf(l_szCommand, "net send %s %s", l_lpszUser, l_lpszMsg);
system(l_szCommand);

Sohaibi


Report Offensive Follow Up For Removal

Response Number 2
Name: RTAdams89
Date: February 11, 2005 at 08:53:44 Pacific
Subject: C++, system(), and variables
Reply: (edit)

Ok here is my code:

#include <iostream.h>
#include <stdlib.h>

int main()
{

system("net start messenger");

int number;
char* l_lpszUser;
char* l_lpszMsg;
char l_szCommand[50];

cout << "Enter user name: ";
cin >> l_lpszUser;

cout << "Enter message: ";
cin >> l_lpszMsg;

int counter=1;
while ( counter <= 100) {


ZeroMemory(l_szCommand, sizeof(l_szCommand));
sprintf(l_szCommand, "net send %s %s", l_lpszUser, l_lpszMsg);
system(l_szCommand);

counter++;

}

system("net stop messanger");
system("PAUSE");
return 0;
}

It still gives me errors though. Specifically
"
24 h:\ryan_a~1\program.cpp
implicit declaration of function `int ZeroMemory(...)'
25 h:\ryan_a~1\spam.cpp
implicit declaration of function `int sprintf(...)'
"

What is wrong?


-Ryan Adams
Ryan's
Custom Cables


Report Offensive Follow Up For Removal

Response Number 3
Name: Don Arnett
Date: February 11, 2005 at 11:45:08 Pacific
Subject: C++, system(), and variables
Reply: (edit)

Get rid of the ZeroMemory line. It's not necessary in this case and ZeroMemory is not a standard C++ function. It must be specific to Sohaib's compiler or environment.

It's not necessary to zero out the string buffer before you write a string to it.

Once you get rid of the ZeroMemory() call, your code should work.

Be sure to come back and let us know if our suggestions helped!


Report Offensive Follow Up For Removal

Response Number 4
Name: RTAdams89
Date: February 11, 2005 at 19:06:31 Pacific
Subject: C++, system(), and variables
Reply: (edit)

That gets rid of the errors and it compiles, but the program crashes when I try to run it. I think I am doing somethign wrong.

-Ryan Adams
Ryan's
Custom Cables


Report Offensive Follow Up For Removal

Response Number 5
Name: Don Arnett
Date: February 12, 2005 at 08:03:32 Pacific
Subject: C++, system(), and variables
Reply: (edit)

Sorry, I didn't look over the whole program very closely and missed some other things.

Change these two definitions:

char* l_lpszUser;
char* l_lpszMsg;

to something like:

char l_lpszUser[50];
char l_lpszMsg[50];

Make each size big enough to hold what input you expect.

What you have now for both variables is char pointers. The problem is two-fold. First, since you declared them inside the function (main) and did not initialize them, they will have some undetermined value. The value of these two variables will be whatever happens to be in the memory of where they are allocated. So when you use cin to input something for them, one of two things is happening. 1-the value of the variable is an invalid address so the program crashes or 2- the address is OK (very lucky) but writing the input to that address overwrites something important so the program crashes. The third case (which does happen sometimes) is that the address is OK and the memory overwritten doesn't cause any problems, so you don't know that this is wrong.

I assume that you fixed the sprintf error, but if not, you need to include <stdio.h> for that.


Be sure to come back and let us know if our suggestions helped!


Report Offensive Follow Up For Removal


Response Number 6
Name: egkenny
Date: February 12, 2005 at 15:15:15 Pacific
Subject: C++, system(), and variables
Reply: (edit)

#include <iostream>
#include <string>
using namespace std;

int main()
{

string user, message, sendstring;
char buffer[50];

system("net start messenger");

cout << "Enter user name: ";
cin.getline(buffer, 50);
user = buffer;

cout << "Enter message: ";
cin.getline(buffer, 50);
message = buffer;

sendstring = "net send " + user + " " + message;

system(sendstring.c_str());

system("net stop messenger");

system("Pause");

return 0;
}


Report Offensive Follow Up For Removal

Response Number 7
Name: Mechanix2Go
Date: February 19, 2005 at 04:02:41 Pacific
Subject: C++, system(), and variables
Reply: (edit)

Hi egkenny,

I'm reading along here, trying to learn.

I tried to compile the code in #6.

I'm using Turbo c++, so that may be an issue.

The first couple of errors:

Unable to open include file 'iostream'
Unable to open include file 'string'

were eliminated by adding .h

Now we're down to

Error egkenny.cpp 5: Type name expected
Error egkenny.cpp 10: Undefined symbol 'string' in function main()
Error egkenny.cpp 10: Undefined symbol 'user' in function main()

and about a dozen more.

Maybe TC++ 1.0 is not up to this.

TIA

M2


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: C++, system(), and variables

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software