how to navigate to a user input site on OSX? - c++

I'm pretty new to C++ and I'm trying to write a simple function to take user input and navigates to the site, adding the https://www. at the beginning of the string, but I cant quite figure out how to call the link within the quotes (or otherwise) within the system function. I come from python so I'm used to being able to use an f string for something like this, and I'm not sure if there's a C++ equivalent, here's my code:
#include <fstream>
#include <iostream>
using namespace std;
void openChrome(string site){
string link = "https://www." + site;
system("open -a 'Google Chrome' //link//");
cout << link;
}
int main()
{
openChrome("apple.com");
}
the cout correctly outputs the full site link, I tried moving the link var outside of the quotes, and that throws an error, so is there something obvious that I'm missing?
EDIT: The issue I'm having isn't with the string concatenation, rather if I try to call the link variable there, it just outputs as the literal since it's in quotes and interpreted directly by the terminal, and throws an error that says 'link' isn't a valid link

I made a few changes to your program:
#include <fstream>
#include <iostream>
using namespace std;
void openChrome(string site) {
string link = "https://www." + site;
string script = "open -a \"Google Chrome\" " + link;
const char *command = script.c_str();
system(command);
cout << link;
}
int main()
{
openChrome("apple.com");
}

Related

How can I create a folder in C++ that is named using a string or char?

I'm trying to create a folder with a custom name for each user that will logged in but it doesn't work. Can you please help me? I'm a beginner and it's quite difficult.
#include <iostream>
#include <direct.h>
using namespace std;
int main() {
string user = "alex";
_mkdir("D:\\Programe\\VS\\ATM\\Fisiere\\" + user);
return 0;
}
I was trying to make the folder in the same way I make the files, but it doesn't work.
_mkdir is an older function which takes a C string as it's parameter. So you have to convert the std::string that you have into a C string. You can do that with the c_str method. Like this
_mkdir(("D:\\Programe\\VS\\ATM\\Fisiere\\" + user).c_str());
This code creates a std::string by appending the path with the user string and then calls c_str on that string and then passes the result of that to _mkdir.

How to make C++ program a Terminal program (UNIX)

I wrote this simple C++ program to compare two strings for a match. Although basic, it's very useful to me as I often need to verify details multiple times a day.
I want to initiate the program with a command name e.g. check4match (the program name) so I can run the program in the terminal.
#include <iostream>
#include <string>
using namespace std;
void match(string, string);
int main() {
string addrOne, addrTwo;
cout<<"Insert str one: "; cin>>addrOne;
cout<<"Insert str two: "; cin>>addrTwo;
match(addrOne, addrTwo);
return 0;
}
void match(string addrOne, string addrTwo){
if(addrOne == addrTwo)
cout<<"SAFE: strings match";
else
cout<<"WARNING: N0 match found";
}
Ok, as always relatively simple in the end. So chmod a+x didn't work to make the cpp program executable. Simple make filename (without the .cpp extension).
Then I moved the newly made .exe file to /usr/local/bin. I had to drag & drop the file, as moving via terminal command wasn't allowed, even with sudo.

how do i fix this no matching function for call to 'stoi(int&)'|

i keep getting this error. i know this is a c++ 11 function but it still isnt working with code blocks c++ compiler. am i using this function correctly of is it a problem with the codeblocks compiler. i tried changing the compiler. using the "have g++ follow the c++11 iso standard" i still keep getting this error. or getting the "stoi() does not exist in the current scope" error
#include <iostream>
#include <string>
using namespace std;
int main()
{
int test = 34;
cout << stoi(test);
}
stoi means "String To Int". It will read an int from a std::string (or std::wstring). See also the reference.
You were probably looking for the reverse std::to_string (reference). But you don't need either, there is no need to convert to string before printing:
#include <iostream>
int main()
{
int test = 34;
std::cout << test;
}
stoi means string to int. So it takes a string as an input.
This should work:
string test = "34"; cout << stoi(test);

NetBeans C++ will BUILD but not RUN (exit value 127) when I use a "string" command in my code

Hey i'm pretty new to coding and I can't seem to get a very basic string program to work. Here is my code:
# #include <string>
# #include <iostream>
using namespace std;
int main() {
string name;
cin >> name;
string message("hi");
cout << name << message;
return 0;
}
This is a very generic example but whenever I run it in NetBeans 8.1 it will build but not run and give me this:
Process is started in an external terminal ...
RUN FAILED (exit value 127, total time: 352ms)
Any other file I run will work as long as it does not contain a string command. I figure it must be something with the settings in NetBeans. I've tried using std:: etc etc but it doesn't fix the problem. Any tips/advice would be much appreciated!
You should change the include statements to have only one # symbol preceding them.
#include <string>
#include <iostream>
This would hopefully fix your error; the rest of the code seems fine.

C++ program gives a run-time error when strings are used

#include <iostream>
#include <string.h>
using namespace std;
int main ()
{
string st = "Hello world";
return 0;
}
and
#include <string>
int main ()
{
std::string st = "Hello world";
return 0;
}
I tried compiling this code using minGW compiler on netbeans. It brings up the following error after the successful build.
RUN FAILED (exit value -1,073,741,511, total time: 93ms)
But it works clean when strings are not used. I would like to know what I am doing wrong here. Thanks in advance.
Use c++ strings and don't use using namespace std:
#include <string> //c++ string header
int main ()
{
std::string st = "Hello world";
return 0;
}
#include <string.h> is the old C-style string header and most likely isn't what you want to use here. See this question for more details: Difference between <string> and <string.h>?
Note: If you really wanted the old C-style strings then you really should be using #include <cstring> because this will put those functions into the std namespace and won't cause any namespace pollution that can lead to other undesirable outcomes.
Likely what happened was that you used the old style string header and didn't properly initialize those strings. The old C-style strings don't have a constructor and operator= defined like the std::string class.
Edit: After looking at the Netbeans forum this is a problem with Netbeans and not a c++ issue. Try changing the output to an external terminal in Netbeans. Or run the program directly from the command line. If these approaches don't fix the problem or are undesirable then make a post over on the Netbeans forum. Also have a look at this question: Program won't run in NetBeans, but runs on the command line!
Uss #include <string> instead of string.h