C++ make program cmd friendly [duplicate] - c++

This question already exists:
C++ create new windows command [closed]
Closed 4 years ago.
I am trying to create a C++ console app and to create new command.
Exemple of what I want to do:
In terminal: (program name) (a value for exemple a string)And then the program make something depending on the value of the string
What is the easiest way to do this using visual studio 2017?

In Visual Studio you can do
Open your project's property window,
Go to "Configuration Properties" -> "Debugging" -> "Command Arguments"
Put your values to "Command Arguments"
To accept this value in your app, your main function should be of signature like
int main(int argc, char* argv[])
For example "Command Arguments" is set to aaa 123 bbb then the arguments' values are:
argc = 4
argv = {"program_name", "aaa", "123", "bbb"}
Hope this is more convenient than manually giving arguments from command line.

Related

How do I can write a program where I can add information just like commands in Ubuntu (ex: program -u "Hello World") [duplicate]

This question already has answers here:
Pass arguments into C program from command line
(6 answers)
How to read/process command line arguments?
(22 answers)
Closed 5 years ago.
I am interested in writing a program in C or Python and add information to my software just like in command line, maybe I'm not explaining so good but what I want looks like this:
Ex: myProgram -u "Hello World"
Then after pressing enter to show my message on screen
In c, we can receive "arguments" from the operating system in the following manner:
int main(int argc,char **argv){
printf("Quantity of arguments: %i\n",argc);
for(int i=0;i<argc;++i){
printf("Argument %i: %s\n",i,argv[i]);
}
}

How can I insert an input in sublime compiler [duplicate]

This question already has answers here:
Sublime Text with console input for c++ programs
(2 answers)
Closed 8 years ago.
I started to use sublime text 2 editor today to write c/c++ programs, but when I run this code
#include <cstdio>
int main(){
int n;
scanf("%d",&n);
printf("%d\n",n);
return 0;
}
I am expecting the compiler to ask for an input.
but instead, it just prints the initial value of (n) which is garbage.
How can i insert an input ?
I suppose what you want to do is input data in the same panel where Sublime Text show results, well friend, you can't.
The only thing you can do is configure Sublime Text for running the compiled program in an external terminal. The link chris provide you is a good start.
On the other hand when you say: expecting the compiler to ask for an input, is a conceptual error. The compiler is not whom is expecting the input. The input is being expected by the compiled program.

How to pass arguments to main.cpp with edit scheme in Xcode 5

I have this simple code as an example:
#include <iostream>
int main(int argc, const char * argv[]){
int a, b, c;
std::cin >> a >> b >> c ;
std::cout << a << " "<< b << " " << c;
}
I would like to pass this code the arguments "1 2 3" that are in a text file named file.txt located in the same file as main.
In Product > Edit Schemes I select the Run Argument side bar option followed by the arguments tab. Under "arguments passed on launch" I enter < file.txt select ok and run my program. The program builds and runs but does not pass the arguments "1 2 3" in file.txt. Is there some other setting I'm missing? Am I putting my file in the wrong folder? Would really like to make this work so I can test code on large sets of data that would be annoying to enter manually.
if I cout << argv[1] I get "<" if I don't quote. If I do quote the argument I get "< text.txt."
Unfortunately, Xcode doesn't directly support command-line file redirections like you're asking for. The arguments are for setting the argv vector in main. When you run a program on the command line, the shell (typically /bin/bash) interprets and sets up redirections and doesn't pass those bits to the program as arguments. XCode doesn't use a shell to launch your program, and doesn't set up the redirection either.
The closest you can get through Xcode, without changing your source code, requires two steps:
In your scheme's Run setup, in the Info tab, set the executable to /bin/sh. You will need to choose “Other…” from the popup menu to get an Open dialog, then type /bin/sh. When you type / in the Open dialog, you will get a popup text box.
In the Arguments tab, add this argument:
-c "$TARGET_BUILD_DIR/$EXECUTABLE_PATH < /path/to/text.txt"
Replace /path/to/text.txt with the path to your input file.
This will run a shell, and ask the shell to run your program with input redirection, but you will find that Xcode ignores your breakpoints, making it hard to debug your program.

Kdevelop execute project with parameters

I am new to Kdevelop and I am trying for 2 hours to run a project based on input parameters in C++ style.
The code is here:
int main(int argc, char** argv)
{
std::string s = args[1]
std::cout<<s<<std::endl;
}
I am trying to add the parameter, but it is crashing and saying
Process Error - Kdevelop
A shell meta character was included in the atguments for file launch ...
Can anyone tell me what is it about? Andhow can I fix it, or where shall I add the execution parameters?
I have placed them in the Launch -> Configuration Launches -> Behaviour -> Arguments see below
Please help
The arguments must be between quotes:
"/your folder and path/Your file"
or
"enter your parameter here"
instead of
just the parameter

Using Command Line Arguments To Input Parameters

So I have a Windows Studio VS File named RectArea that contains the code to a function to find the area of a rectangle. In another Windows Studio VS file named main1, I have my main function like this:
int main(int argc, char *argv[])
{
return 0;
}
How do I use command line arguments to be able to print the area of the rectangle in the main file as soon as i compile it?
I read so much stuff about it online. I just still don't understand what I put for the executable and the stuff after it.
My function for the rectangle's area is in the RectArea file. Do I type in C://RectArea?
It's under a lot more folders though.
Ah, you're confused on how to pass the arguments to the program, not how to interpret them. Those arguments are not command line arguments passed to the Visual Studio build process; they're arguments that can be changed each time you run the program.
An executable written with an entry point like that can't be run properly via double-click like all the others you've written; you have to open cmd.exe and run the program from there, typing the arguments after the program name separated by a space.