Hi I'm a highschool student in need of help. I am interested in C++ as I wish to become a programmer one day. I started using Visual Studio for C++ and I'm running a few simple lines of code. But everytime I press (Ctrl + F5) or Local Windows Debugger it shows my line of code but it closes out immediately after it runs, making it near impossible to read. The code is the classic "Hello World!" code. Maybe it has to do something with return 0;?
EDIT: Here's my code.
#include <iostream>
using namespace std;
int main()
{
int mark = 90;
if (mark < 50) {
cout << "HES OLD" << endl;
}
else {
cout << "Hes not old!" << endl;
}
}
When making console applications, I use the following lines at the end of my main function:
std::cout << "\nPaused. Press Enter to continue.\n";
std::cin.ignore(100000, '\n');
The idea is to display a prompt and then wait for the Enter key to be pressed.
Related
I am following the C++ Debugging tutorial section (https://help.eclipse.org/photon/index.jsp) in the Eclipse Photon documentation. I have followed all the instructions creating the first C++ project and I'm at the 'Debugging project' section. My problem is that after I create my debug perspective and hit the 'Debug' button the debugger never stops on my set breakpoint. In the Debug window it just says and the loop of the program doesn't even produce any output at all. If you run the program normally the loop prints to the console, but nothing happens in debug perspective.
I have tried deleting my debug perspective and creating a new one and I'm having the same issues.
I have uploaded screenshots of my debug configuration.
https://imgur.com/a/MXYHxJl
https://imgur.com/a/eEU47Ht
https://imgur.com/a/koOf08x
#include <iostream>
using namespace std;
int main () {
// Say HelloWorld five times
for (int index = 0; index < 5; ++index)
cout << "HelloWorld!" << endl;
char input = 'i';
cout << "To exit, press 'm' then the 'Enter' key." << endl;
cin >> input;
while(input != 'm') {
cout << "You just entered '" << input << "'. "
<< "You need to enter 'm' to exit." << endl;
cin >> input;
}
cout << "Thank you. Exiting." << endl;
return 0;
}
I found a solution to my problem following instructions in this other post: Eclipse C++ MinGW - Can not Lauch Program <Terminated> The 2nd answer is what worked for me.
I had to right click on my project - - > properties - -> Run/Debug settings - - > click your launch configuration and click 'edit'. Once inside the edit screen click the 'environment' tab and add the following variable
Name = PATH
VALUE = %PATH%;C:\MINGW/BIN
I did not have anything set inside the environment and changing to the above has made the debugger stop at the correct breakpoints within the program.
This question already has answers here:
How to keep the console window open in Visual C++?
(23 answers)
Closed 4 years ago.
My IDE is Visual Studio 2017.
I am pretty new in C++ programming so I need a help about understanding principles of creating a new C++ project in Visual Studio.
So, in my first solo attempt i just chose a empty project option and afther that i chose to add new item and i write this sample code:
#include <iostream>
using namespace std;
int main()
{
return 0;
}
Afther this step and afther steps with compiling, building and a starting without debugging i did not get any message or consol window with time of code execution or option for entering any key for ending.
What is needed for getting this kind of information at the end of code?
You shouldn't use system("pause"); you can read here why. It's platform dependent and adds a huge overhead loading all the Windows specific intstructions.
So you should choose nicer alternatives: std::cin.get() for example. Which will work most of the time. Well, except if there had been input before (std::getline or std::cin). If you're creating a program with user input - use std::cin.ignore() twice to guarantee a "press enter to continue" effect:
#include <iostream>
int main() {
int a;
std::cin >> a;
std::cin >> a;
std::cin >> a; //etc
std::cout << "press enter to exit - - - ";
std::cin.ignore(10000, '\n');
std::cin.ignore(10000, '\n');
return 0;
}
also please don't use namespace std; read here why.
If you don't like this 3-liner (because it looks ugly) you can pack it in a void function and treat the whole thing as a black box:
void pause() {
std::cout << "press enter to exit - - - ";
std::cin.ignore(10000, '\n');
std::cin.ignore(10000, '\n');
}
int main(){
pause();
return 0;
}
Converting Blaze's comment to an answer
Go to Tools->Options->Debugging and look for an option called "Automatically close the console when debugging stops" and ensure that this option is not activated.
I did not get any message or consol window with time of code execution or option for entering any key for ending
Because you didn't ask for it.
What is needed for getting this kind of information at the end of code?
To perform input (see std::cin and operator<<) and output (see std::cout and operator>>). Example:
#include <iostream>
int main()
{
std::cout << "Press enter to terminate\n";
std::cin.get();
}
I used visual studio 2013 two days ago.
But now... It does not working.
I tested below c code and cpp code.
//#include <stdio.h>
#include <iostream>
using namespace std;
void main(){
//printf("hi");
//cout << "test" << endl;
int a;
cin >> a;
cout << a;
}
The result of run is just blinking console.
And does not appear "Press Enter..."
result
If I try to debug then Visual studio stop and no response.
I do reset all options :(
Your console is showing the output but not waiting for you to see it.
you need to add break point or for workaround you can use
cout << a;
int ch = std::cin.get();
}
you must be able to see the value of a.
EDIT: ohh you would need fflush too, my fault too miss it.
cout << a;
fflush(stdin);
int ch = std::cin.get();
}
After finding a bunch of threads on this I did not find any on Windows, only Linux, Ubuntu etc.
In short what I am trying to find out is how to turn canonical mode off so that input in the console results in instant input without having to press enter.
Longer version. This is what I am trying to do right now:
When the PGM is pausing (text based game) the user is asked to enter an arrow key to move the player around on the 2D array. But cin.get(), cin.ignore() and some other things I've tried all require pressing enter before it will continue.
The infamous system("pause>nul"); does actually work, but as I've read in various other places is very bad practice.
This is part of the code. the cout statements are only for testing purpose:
//While loop
if(GetAsyncKeyState(VK_UP)){
cout << "up" << endl;
}
else if(GetAsyncKeyState(VK_DOWN)){
cout << "down" << endl;
}
else if(GetAsyncKeyState(VK_LEFT)){
cout << "left" << endl;
}
else if(GetAsyncKeyState(VK_RIGHT)){
cout << "right" << endl;
}
else{
break;}
//pauze and check for arrow key input here
You can use getch also on windows:
I know it is C, and you got the deprecated warning, but it works...
this code run in a loop till you press Enter:
check what happen when you press an arrow key...
#include <stdio.h>
#include<conio.h>
int main ()
{
int c;
do {
c=getch();
printf("%d\n",c);
} while (c != 13);
return 0;
}
I am currently learning C++ for a programming course. I noticed that my professor's program automatically closes at the end of their program. It usually prompts the user for an input, and then upon entering an input, the program closes. How would I code that? I only know using return 0 gets me "Press Any Key to Continue"
Note: this is a .exe file
If your program doesn't wait for any input, it runs and finally exits from the program. On exiting, the console automatically closes. I assume, to run the program you're clicking on the .exe, as opposed to runnng the program from cmd.exe, or you run the program from visual studio itself without debugging.
You could just put the following line before return 0;:
std::cin.get();
It will wait for some input and then proceed.
use getch(); before return; statement
Return 0 to give "press any jey to continue" is debugger-specific behavior. Running your compiled exe outside the debugger usually wont show that.
The simple code below does a little more than you're asking for (it repeats what you typed in) but still gives the general idea.
#include <iostream>
using namespace std;
int main() {
cout << "enter something" << endl;
string stuff;
cin >> stuff;
cout << "You entered " << stuff << " you insensitive clod" << endl;
return 0;
}
it is easy, at the end of your main() function put this:
int x;
cin >> x;
this define a new variable and tries to fill it with user-input and then the program will not be terminated till the user gives it input. This is how the program reaches the Press any key to continue, finally you exit the program with a 0 argument and the console window will be destroyed automatically since it is the main window of the process.
I recommend to use:
std::cin.clear();
std::cin.sync();
std::cin.get();
cause there may be times when you need to write something and you will need to press ENTER which will make
std::cin.get();
useles. As it will remeber the first time you pressed ENTER and close the window.
Sample:
#include <iostream>
#include <string>
int main()
{
std::string name;
std::cout << "Your name: ";
std::cin >> name; \\ <--Place where you press ENTER <--------------
std::cout << "Hi, " << name << ".";
std::cin.get();
return 0;
}