Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C++ system(variable); ??


ghost's Avatar
0 0

Is there a way that I can pass a variable into the system() function?

I understand the syntax of the system command is: system(""); , but when I try to put a variable into the function, it gives me some really wierd and extensive error that I cannot remember off the top of my head.

Any help in this manner would be much appreciated ^_^

–Skunk


ghost's Avatar
0 0

It depend what variable you wanna pass in the system command. If i remember well the only variable you can pass is string (char), otherwise you'll have to convert it to string then pass it.


ghost's Avatar
0 0
#include <cstdlib>
#include <string>

int main()
{
     std::string myCmd = "PAUSE";
     
     system(myCmd.c_str());
     
     return 0;
}

Edit: HBH code tags suck.


ghost's Avatar
0 0

thanks guys, I'll try some stuff with this and hopefully get it right…by the looks of that example it doesn't look hard.

–Skunk


ghost's Avatar
0 0

Okay, the variable I want to pass is in a struct. The variable is called "SendInfo.String", but when I try:

std::string myCmd = SendInfo.String;
system(myCmd.c_str());

I get:

Error   : function call 'basic_string(apstring)' does not match
'std::basic_string<char, std::char_traits<char>, std::allocator<char>>::basic_string(const std::allocator<char> &)'
'std::basic_string<char, std::char_traits<char>, std::allocator<char>>::basic_string(const std::basic_string<char, std::char_traits<char>, std::allocator<char>> &, unsigned int, unsigned int)'
'std::basic_string<char, std::char_traits<char>, std::allocator<char>>::basic_string(const std::basic_string<char, std::char_traits<char>, std::allocator<char>> &, unsigned int, unsigned int, const std::allocator<char> &)'
'std::basic_string<char, std::char_traits<char>, std::allocator<char>>::basic_string(const char *, unsigned int, const std::allocator<char> &)'
'std::basic_string<char, std::char_traits<char>, std::allocator<char>>::basic_string(const char *, const std::allocator<char> &)'
'std::basic_string<char, std::char_traits<char>, std::allocator<char>>::basic_string(unsigned int, char, const std::allocator<char> &)'
'std::basic_string<char, std::char_traits<char>, std::allocator<char>>::basic_string(T1_0, T1_0, const std::allocator<char> &)'
'std::basic_string<char, std::char_traits<char>, std::allocator<char>>::basic_string()'
MAIN.CPP line 294     std::string myCmd = SendInfo.String;

Do you think you understand this error? Can I not set myCmd = to SendInfo.String because it's a variable or something wierd like that?


ghost's Avatar
0 0

Can you please post that structure and the headers you are including.

~T


ghost's Avatar
0 0
#include "apstring.h" // Necessary for the string data type in my compiler (apstring)
#include <iomanip.h> // Necessary for formatting(setw, etc.)
#include <WinSIOUX.h> // Necessary for clearing the screen

struct NetSendType
{
	char NetView;
	apstring Method;
	apstring String; // This is the only relevant variable in this case
};

NetSendType SendInfo; // Declares "SendInfo" as type "NetSendType"


ghost's Avatar
0 0
#include <iostream>
#include <cstdlib>
#include "apstring.h"

struct NetSendType
{
char NetView;
apstring Method;
apstring String; // This is the only relevant variable in this case
};

int main()
{
NetSendType SendInfo;

apstring myCmd = SendInfo.String;

system(apstring.c_str());

return 0;
}

ghost's Avatar
0 0

holy shit it almost worked! All I got was a " '(' needed" error; my compiler went retarded and thinks I need a '(' instead of a '.' in "apstring.c_str", but that should be a pretty easy fix, thanks alot!

:) –Skunkfoot