How to put custom input for debugging in Visual Studio Code - c++

In Visual studio code , i dont want to give long input to my program everytime. Is there a way like in hackerearth to copy paste my input and the program will run on them.
Take this as example.What should i write in args of launch.json. I am new to vscode.Here is my program

You have to change the main() as below:
int main(int argc, char* argv[])
{
for(int i = 1; i <argc; ++i)
std::cout<<argv[i];
return 0;
}
This is simple program.
You have to right-click the project, choose Properties, go to the Debugging section -- there is a box for "Command Arguments".
Specify the as "34 45 44".
You can run this again and again by mentioning it once.

Related

Simple output loop fails in Visual Studio Code terminal when compiled with cl

Consider the simple program, in file a.cpp:
#include <iostream>
int main(int argc, char* argv[])
{
for (int i = 0; i < 10000; i++)
{
std::cout << i << std::endl;
}
}
It works fine when I run it in the Visual Studio Code (Powershell) terminal after compiling it on Windows with Cygwin g++, like so:
g++ a.cpp -o a.exe
However, I get strange and inconsistent behavior when I run it after compiling it with cl like so (in a separate program, the Developer Command Prompt for VS 2017):
cl a.cpp
When run in the Visual Studio Code (Powershell) terminal after compiling it this way, it stops outputting numbers well before 9999. Furthermore, it stops outputting at a different point each run. Running the program from a .bat script to check the %errorlevel% indicates that the error code is 0, so it doesn't seem to be crashing during the loop.
This, on the other hand, works totally fine from the VSCode terminal when compiled with cl, outputting 9999 every time:
#include <iostream>
int main(int argc, char* argv[])
{
int j = -1;
for (int i = 0; i < 10000; i++)
{
j++;
}
std::cout << j << std::endl;
}
So it seems like code compiled with cl will just stop outputting data written to std::cout after a little while when run from the Visual Studio Code terminal. But why would that happen?
Update
It works perfectly after compiling with cl when running from Developer Command Prompt for VS 2017, or from Powershell, or from Command Prompt. It only has the strange behavior when (a) compiled with cl instead of g++ and (b) run from a Powershell terminal within VSCode.

Eclipse C++ Runs on Windows Command Prompt

I wrote a simple C++ code. When I click "Run" on Eclipse, first a Windows command prompt appears with my program. After executing it, Eclipse console starts to work. Is there a way to deactivate command prompt and just use Eclipse console?
#include <iostream>
int main(){
int x = 0,y = 0;
std::cout <<"type2 numbers and I will make a magic trick."<< std::endl;
std::cin >> x >> y;
std::cout<<"Ta daa "<<x<<" and "<<y<<" equals to "<<x+y<<std::endl;
return 0;
}
When I erase std:cin part, command prompt appears and dissappears less than a second so I guess everytime I run my code, this command prompt will appear.

Run .exe file of C code using Eclipse

I've make a very simple basic code in C language using Eclipse IDE.
Eclipse details:
Eclipse IDE for C/C++ Developers
Version: Juno Service Release 2
Build id: 20130225-0426
Here is the code:
#include <stdio.h>
int main( int argc, char ** argv ) {
printf("Hello, World!\n");
return 0;
}
Runs successfully in Eclipse, it generates .exe & .o file in Project/Debug directory. I am trying to run that .exe file but it is not working.
Eclipse acts as if it ran the program very quickly and then terminates it. Window appears, but nothing happens when I run the .exe. It just looks like a flash of a dialogue box.
What should be the problem? I don't want to change the IDE and I've already tried these two things:
Eclipse CDT won't run compiled exe files
eclipse won't run my exe file
run the program from the command prompt window. If you simple double click the exe file .. it just runs the program and shuts out in an instant. You won't have any time to see it.
Run it through a cmd window by navigating to the directory and doing ./yourexefile
Or a terrible way to do this with double click is to do this :
#include <stdio.h>
int main( int argc, char ** argv ) {
int n;
printf("Hello, World!\n");
scanf("%d",&n); // this will force the execution window to stay open until you put in some input
return 0;
}
You could also do:
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char ** argv ) {
printf("Hello, World!\n");
system("pause");
return 0;
}
Another way ( more aesthetic ):
#include <stdio.h>
int main( int argc, char ** argv ) {
printf("Hello, World!\n");
printf(" Press enter to exit\n");
getchar();
return 0;
}
Windows is opening a command prompt for your program, running your print statement, and then closing the window (because your function is over).
If you want to see the result of your program, run the command prompt, navigate to the directory of your .exe file, and run it from there.
You should also try this:
Right click on your project name in the Project Explorer view, then go to Run As and click on Local C/C++ Application

Pass arguments to main function at compiling time

I am a novice programmer learning C++ and the following question can be considered cross platform as I tried this both in Visual Studio 2010/12 and Qt Creator in Linux Mint.
I have set up the main() function of my program to accept command line arguments like this:
int main(int argc, char* argv[])
{
if(argc < 5)
{
printf("Not enough input parameters!\n");
printf("Usage:\n");
printf("'program' lamda1 lamda2 Attraction_Range Order_Param_Range\n");
return 0;
}
else
{
lamda1 = atof(argv[1]);
lamda2 = atof(argv[2]);
attRange = atof(argv[3]);
oRange = atof(argv[4]);
cout << lamda1 << lamda2 << attRange << oRange << endl;
}
}
However, when I hit Ctrl+F5 in Visual Studio or Ctrl+R in Qt Creator, to compile, the code forks to the if statement because I cannot input any parameters. Can I somehow pass initial arguments to my program, so that when it compiles, it can immediately go to the else part of the above example?
I am sorry if this is a duplicate/ wrong question, but I didn't even know how to search for this on the net.
In Visual Studio: How could I run a project with some parameters in Visual Studio?
In Qt Creator: QtCreator and Command Line Arguments

How do I run my code from the command line?

i have following code
#include <iostream>
using namespace std;
int main(int argc,char arg[]){
int a=arg[1];
int b=arg[2];
int c=a+b;
cout<<c<<endl;
return 0;
}
i am using windows 7 microsoft visual c++ 2010
how run it from command line?
Open a command prompt from the Start Menu. Use the CD command to change directories to where your exe is. type the name of your exe followed by the arguments.
foo.exe 1 2
or just
foo 1 2
Expect the output (once you've fixed your numerous code errors):
3
Once you compile this you get an executable. Navigate to the directory containing the executable and run it.
Go to google and look for a windows console tutorial. You need to start it from the console. Alternatively you can assign command line in the project properties. I'd recommend learning to do both.
BTW, this code almost certainly does not do what you think it does.
The compiled output of your program will be in the Debug or Release folder inside the solution folder (at least with default project settings). Just change to that directory and run the .exe file.
Open the Visual Studio Command Prompt (you can find it in the Start Menu)
cd to your source file directory
type:
cl.exe <your file name>.cpp
It will create a file .exe
Once your code is setup properly it would be something like this.
MyApp 2 3
Or similar
Navigate to the directory where the executable (.exe) is located. Then type the executable's name followed by two integer parameters.
C:\TestProg\> TestProg 5 6
The problems in your original example are corrected here:
#include <iostream>
#include <sstream>
int main(int argc, char *arg[])
{
std::stringstream sa;
std::stringstream sb;
int a;
int b;
int c;
if (argc >= 3)
{
// Convert string parameter into an integer.
sa.str(arg[1]);
sa >> a;
if (!sa)
{
return 1; // error
}
// Convert string parameter into an integer.
sb.str(arg[2]);
sb >> b;
if (!sb)
{
return 1; // error
}
}
else
{
return 1; // error
}
c = a + b;
std::cout << c << std::endl;
return 0;
}