Run .exe file of C code using Eclipse - c++

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

Related

How to put custom input for debugging in Visual Studio Code

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.

SDL not printing to console

I'm trying to use SDL with Visual Studio 2019 but my programs are only showing an empty console. At the moment I just want to be able to compile my program with the SDL libraries.
#include <iostream>
#include <SDL.h>
int main(int argc, char** argv)
{
std::cout << "yee haw!" << std::endl;
return 0;
}
This code is just giving me a console with the text:
(process 32) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
Where I would want 'yee haw!' preceding it.
It works fine when I take out the #include <SDL.h> (but I want the SDL.h)
I've heard that SDL now redirects to a stdout.txt file but I couldn't find that anywhere. I've also tried displaying a window with code from a tutorial I found, but that also gives me the empty console.
I'm using Visual Studio 2019 on Windows and SDL 2.0.9
Thanks!
By default, SDL uses a macro hack to replace the main function. The user defined main function must be in the following format:
int main(int argc, char** argv)
{
// whatever
return 0;
}
Alternatively, if you don't want this behaviour you can use SDL_SetMainReady.
#define SDL_MAIN_HANDLED
#include <SDL.h>
int main()
{
SDL_SetMainReady();
// whatever
return 0;
}

runtime errors not showing up in debugger

I've got a couple of programs written in CPP which crash. Some are graphics based, others are command line. Usually they include a while loop and crash after a few minutes.
I attached them to GDB and compiled and executed in Dev C++, and they don't show any sign of crashing. I can run them for hours on end and there are no problems. But once intake it out of that environment they crash in a couple of minutes.
For example I have this code:
#include <cstdio>
#include <cstdlib>
int main (int argc, char * args[]){
char sysstr[100];
if (argc<2){
printf("No .java file given...");
}else{
sprintf(sysstr,"java %s",args[1]);
try {
system(sysstr);
}
catch (int lol){
}
}
return 0;
}
It runs fine within DevC++, but outside windows tells me after it has finished that it stops working.
Help?

Xcode reading chars from file in c++

I have the following code to read a character from a file:
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, const char * argv[])
{
ifstream f("text.txt");
char c;
f.get(c);
cout << c << endl;
return 0;
}
and my text.txt file contains:
hello world!
However, when I run this on Xcode, I get an inverted question mark as the output.
It works fine on terminal, but not on Xcode. Does anyone know why this happens?
I'm using Xcode to debug some code, but I cant do that anymore because this problem is causing a lot of other errors in my program.
Your text.txt file will not be at the executable path.
Go to your Build Phases - > Copy Files -> Add Your text file
Make sure that:
Destination should be Products Directory
Copy only when installing should be unchecked

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;
}