C++ is executing cmd command by input possible? [duplicate] - c++

This question already has answers here:
How to convert a std::string to const char* or char*
(11 answers)
Closed 2 years ago.
I want to create sth like:
int main ()
{
string s{};
std::cout << "Enter CMD: \n";
getline(cin,s);
system(s);
}
But since I can use only const char on system, its not working at all, is there any different solution to this? mabye shellexecute?

You can use std::string::c_str().
system(s.c_str());

Related

How can I use std::string for Const Char Parameter [duplicate]

This question already has answers here:
How to convert a std::string to const char* or char*
(11 answers)
Closed 2 years ago.
I am trying to use the std::rename() function to move a .docx file, however, the name of the file may vary. How can I use a std::string within std::rename() so that it does not have to be a hardcoded filepath, like this std::rename(filepath, destination);?
I don't know how you want to populate the strings in question, but here you go:
std::string fromName {"whatever you're going to do"};
std::string toName {"whatever you're going to do"};
std::rename(fromName.c_str(), toName.c_str());

How to convert int to char in c++ style? [duplicate]

This question already has answers here:
Easiest way to convert int to string in C++
(30 answers)
Closed 2 years ago.
Please tell me how to convert int to char in c++ style? The content in str1 is "\001", while the content in str2 is "1"; Can I use static_cast to get the same result as str2?
The code is as follows:
#include <iostream>
using namespace std;
int main()
{
int a = 1;
string str1;
string str2;
str1.push_back(static_cast<char>(a));
str2.push_back('0' + a);
cout << str1 << str2;
return 0;
}
It depends on the exact use-case. For a direct int-to-string conversion, use std::to_string.
When streaming the output, there are usually better options, including using fmt (part of C++20, available as a separate library before that).

Is it possible to declare a variable as a string? [duplicate]

This question already has answers here:
Convert string to variable name or variable type
(7 answers)
Closed 7 years ago.
So basically what I'm trying to find out, take this scenario:
std::string input;
Ask user for a string?: Apples
cin >> input;
std::string Apples = "input";
So basically, I ask the user for a string, and then create a variable with name of that string. Is this possible?
No, you can't do that.
The closest functionality is to use a map.
std::map<std::string, int> aMap;

How to use printf for strings? [duplicate]

This question already has answers here:
printf with std::string?
(9 answers)
Closed 9 years ago.
So this is my code
string name;
cout <<"\n Enter Your name : \n";
cin >> name;
printf("%s" , name);
and for some weird reasons codeblocks crashes at this
why ?
also how could I fix it ?
thanks
You should compile with all warnings (e.g. g++ -Wall). You'll get a useful warning. You want to use c_str like this
printf("%s", name.c_str());
BTW, why use printfand why do you forget a \n at the end of the printf format string? (or use fflush)
Better code:
cout << name << endl;
If you need to pass your std::string to a function that accepts / uses C-style strings (const char *) for input, use .c_str(). It returns a const char *.
This is what you should do when needing to work with existing libraries, system calls, etc. For your own code, it is usually better to find a more C++ way of doing it.
In this case:
std::cout << name << std::endl;

C++: how to add int to current string in C++? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
C++ concatenate string and int
Hi,
In C# I can write like this:
int i = 0;
string text = "out.jpg";
while(true)
{
i++;
Object.write(i+text, stream);
}
But this is not true for C++. the problem is at: i + default.
How could I fix this in C++?
Thanks in advance. Your help is much appreciated!
You could use a stringstream...
std::stringstream ss;
ss << i << text;
Object.write(ss.str(), stream);
default is a keyword in C++. You can't have string default in C++. And I don't see what you are trying to achieve. Please clarify
take a look at stringstreams or boost.format http://www.boost.org/doc/libs/1_38_0/libs/format/doc/format.html
boost::format("%1%%2%") % i % default_;
default is a reserved keyword. Change the variable name to defaultStr or similar, and everything should work fine.