Output to windows cmd prompt - c++

I have been told to make an exe file that will output 5 lines. I have been given these instructions:
Your program must not hang after running. I will execute it at command-line and will expect it to finish when the program is done.
I tried going to properties -> linker -> system - > console. It seemed to do what he wanted when I ran ctrl-f5. I then made an exe and now it disappears, and it doesn't print to the console.
I am using a simple cout program ex:
int main()
{
cout<<"hello"<<endl;
return 1;
}
Edit
I'm using visual studio 2013, and I am running from the command line.
Note I'm not asking for the window to stay open, but printing to the console itself.
I am not pressing ctrl-f5, but going to cmd.exe and then to the executable. I have tried the release version as well as the debug version.

If I have understood your question. Try following and add useful parts to your code. Not needed lines are commented out.
// ConsoleApplication1.cpp : Defines the entry point for the console application.
// (Just 2 ways to wait, befoore command-line console vanishes.)
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << "It's Foo's Bar!\n";
system("pause"); //preferred way.
//cin.get(); //2nd way, not as good.
return 0;
}

Related

C++ "Hello world" shows no output

I installed Codeblocks on my Windows 10 computer. To check that everything works fine, I first compiled the simple C program
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello world!\n");
return 0;
}
That works without problem but when I try the C++ equivalent:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return(0);
}
Then the "command prompt" window opens but no output is shown. I can see in taskmanager that the program is running but as said without any visible output. I also tried running the program directly from the command line but with the same effect. Anyone any ideas?
I found the issue. There was still an older version of MinGW installed in a different folder. I deleted all instances of MinGW, and codeblocks as well. Adter I reinstalled codeblocks everything worked as it should.
This Guy solved similar problem with Codeblocks.
Remove the following Global compiler setting:
-Wl,-subsystem,windows

c++ win32 console application is not recognized as an internal or external command operable program or batch file

so I just installed vs2015, and I'm having issues starting a project. For my intro school programming class, we are learning with the win32 console. I started up my own project at home so I could work on my projects at home, but I'm having issues starting. Thanks in advance for any help. Link to image
Your issue is annoying yet simple to fix, before I answer it you're using visual studio 2015 which means by default you would have been "given " a header file and your main would have looked like the following.
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
You can modify the main function, as in remove every thing but int main() but I would suggest not doing this.
The answer to your question is, your program will not compile because you are not using #include "stdafx.h"
simply put that in and your projects will work assuming you code them right...
Also after looking at your picture more closely ( next time post your code here)
I see that your only source file showing is named R4.Que? Rename it R4.cpp

The system cannot find the file specified. in Visual Studio

I keep getting this error with these lines of code:
include <iostream>
int main()
{
cout << "Hello World" >>;
system("pause");
return 0;
}
"The system cannot find the file specified"
The system cannot find the file specified usually means the build failed (which it will for your code as you're missing a # infront of include, you have a stray >> at the end of your cout line and you need std:: infront of cout) but you have the 'run anyway' option checked which means it runs an executable that doesn't exist. Hit F7 to just do a build and make sure it says '0 errors' before you try running it.
Code which builds and runs:
#include <iostream>
int main()
{
std::cout << "Hello World";
system("pause");
return 0;
}
The code should be :
#include <iostream>
using namespace std;
int main() {
cout << "Hello World";
return 0;
}
Or maybe :
#include <iostream>
int main() {
std::cout << "Hello World";
return 0;
}
Just a quick note: I have deleted the system command, because I heard it's not a good practice to use it. (but of course, you can add it for this kind of program)
I had a same problem and this fixed it:
You should add:
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib\x64 for 64 bit system
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib for 32 bit system
in Property Manager>Linker>General>Additional Library Directories
Another take on this that hasn't been mentioned here is that, when in debug, the project may build, but it won't run, giving the error message displayed in the question.
If this is the case, another option to look at is the output file versus the target file. These should match.
A quick way to check the output file is to go to the project's property pages, then go to Configuration Properties -> Linker -> General (In VS 2013 - exact path may vary depending on IDE version).
There is an "Output File" setting. If it is not $(OutDir)$(TargetName)$(TargetExt), then you may run into issues.
This is also discussed in more detail here.
This is because you have not compiled it. Click 'Project > compile'. Then, either click 'start debugging', or 'start without debugging'.
I resolved this issue after deleting folder where I was trying to add the file in Visual Studio. Deleted folder from window explorer also. After doing all this, successfully able to add folder and file.
I was getting the error because of two things.
I opened an empty project
I didn't add #include "stdafx.h"
It ran successfully on the win 32 console.

XCode C++ Ignoring Main Function

I have most bizarre and probably simple to solve problem but it's driving me crazy.
I'm using XCode 4, I've started with an empty project added a new file main.cpp containing:
#include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
cout << "Hi There!" << endl;
}
Then I added a target called main and added main.cpp to it's sources and compiled and run to be greeted with:
Hello, World!
In return.
Why is XCode adding and compiling it's own main function and how to I tell it to use mine?
It also compiles and runs successfully with an empty main.cpp.
Thanks.
First try cleaning and rebuilding.
If that doesn't work sounds like there is another file with a main in it that is being included in the build.
In the Project File, go into the "Build Phases" tab, and see what is listed under "Compile Sources". The source of your problem might be there.

Normal C++ code in Qt doesnt build and run

I am using Qt under linux, if it matters.
I ran successfully under Geany (a simple c++ compiler) the following:
//my first program in C++ Hello World!
#include <iostream>
using namespace std;
int main ()
{cout << "Hello World!";
return 0;}
I opened Qt source file and copied the exact same code and i can't build or run.
Thank you for your responses to this simple problem.
If you did what I think you did, you didn't open this as a project, which is the only place where you can build and run (I think).
Try the following.
- Open Qt Creator.
- Go to File->New File or Project
- At the bottom, select "Qt4 Console Application"
- Select a location; it might be nice to create a folder called "hello_world" or something to store the project in.
- A new project will have been created. Copy over the main.cpp file in sources with your code. My code looked like this:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!\n";
return 0;
}
Hit "Build All"
Hit "Run"
This worked for me. Hope this helps!