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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++, system(), and variables

Reply to Message Icon

Name: RTAdams89
Date: February 10, 2005 at 19:33:16 Pacific
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



Sponsored Link
Ads by Google

Response Number 1
Name: sohaibi
Date: February 10, 2005 at 23:05:43 Pacific
Reply:

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


0

Response Number 2
Name: RTAdams89
Date: February 11, 2005 at 08:53:44 Pacific
Reply:

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


0

Response Number 3
Name: Don Arnett
Date: February 11, 2005 at 11:45:08 Pacific
Reply:

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!


0

Response Number 4
Name: RTAdams89
Date: February 11, 2005 at 19:06:31 Pacific
Reply:

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


0

Response Number 5
Name: Don Arnett
Date: February 12, 2005 at 08:03:32 Pacific
Reply:

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!


0

Related Posts

See More



Response Number 6
Name: egkenny
Date: February 12, 2005 at 15:15:15 Pacific
Reply:

#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;
}


0

Response Number 7
Name: Mechanix2Go
Date: February 19, 2005 at 04:02:41 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++, system(), and variables

C++ system() and variables? www.computing.net/answers/programming/c-system-and-variables/11796.html

c++ system() and variables www.computing.net/answers/programming/c-system-and-variables/14545.html

c++ - system() question www.computing.net/answers/programming/c-system-question/16524.html