Stuck on a quick console app

Posts 1–4 of 4 · Page 1 of 1
Stuck on a quick console app
Hey guys its me again. Im here to pick your brain! I wanted to make a really small console app that just requests for a hostname of a computer and uses a cmd promp command to open up a unc connection.

Im writing this from my phone so this will be rough. Due to web security at my job. When I get home I will refine it. My phone is going to capitalize alot so void some of the syntax.

static string pcname;

int main ()
{

cout << "UNC PATHER \n";
cout << "Hostname to path? \n>";
cin >> pcname;

system (("start " + "\\" + pcname + "").c_str ());
system ("exit");
return EXIT_SUCCESS
}

1. In function 'int main ()':
2. I got an error output showing "invalid operands of types 'const char [7]' and 'const char[2]' to binary 'operator+'.
3. [Build error] [main.o] error 1.

Can anyone help out with this?
C++ is case-sensitive. All your upper-cased letters need to be lower-case.

Int main -> int main
Cout -> cout
Cin -> cin

And so on. Things should only be uppercase if they require it to be.
Yes I know, I was typing from my phone and it auto corrected my coding and screwed up syntax. lol I fixed it up and made it proper.
Your first system call is not creating a string. C++ does not auto-type to strings/objects. You have to tell it to use a string:
Code:
system( std::string( "start \\\\" + pcname ).c_str() );
You also need to double escape the backslashes as C++ uses \ as the start of an escape character. Such as \n would be new line.
So you have to use \\ to display and use a single slash. Thus you need \\\\ instead.

Your system("exit") call is also not needed.
Also you should try to avoid using system() altogether given that it is a huge resource hog for simple tasks.
Posts 1–4 of 4 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us