This question already has answers here:
C++ - Hold the console window open?
(11 answers)
Closed 6 years ago.
I have a problem with the c++ code below. The problem is that after the user has typed in the input, the black screen disappears very fast. I would like the black screen to stay up until I have pressed the enter button. I have tried using the cin.get(), but I am new to c++, and I don't understand what is wrong. (I don't get an error code, it's just that I would like the black screen to stay). I am using Visual Studio.
#include <iostream>
#include<string>
using namespace std;
int main() {
string password = "Hello";
cout << "Enter password" << flush;
cin.get();
string input;
cin >> input;
if (input == password) {
cout << "The password is correct" << endl;
cin.get();
}
if (input != password) {
cout << "Access denied" << endl;
cin.get();
}
return 0;
}
It closes because it reaches the end of the main(), which means there is nothing more to do, and the function returns.
A simple solution would be to use getChar() function before the return statement, this will leave the window open until you type a character (any character) on the keyboard.
Related
I'm writing a program in C++ that at some points, will need to throw an application notification in the Taskbar. I have added an image to show what I mean.
Previously I have done some search on this, but with no luck, I've only managed to find solutions where the program creates a dialogue box and throws that to the user.
I'd just like to ask, is something like this possible with C++? I've created a small program where I would need something like this to work:
#include <iostream>
#include <string>
using namespace std;
string input;
int main() {
while (1) {
cout << "Type \"test\" to throw a test prompt: ";
cin >> input;
if (input == "test") {
// throw prompt
}
else {
cout << "That wasn't \"test\"";
cout << '\n';
}
}
system("pause");
}
Call the FlashWindowEx function to flash a window's taskbar button.
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();
}
This question already has answers here:
C++ Issue with cin and CTRL + Z
(2 answers)
Closed 5 years ago.
I'm going through B. Stroustrup's Programming Principles and Practice Using C++ and I encountered a problem doing one of the exercises. I'm using Windows 10 (I believe, that's relevant here).
The code below is supposed to ask the user for words, which are to be printed back. However, if a word is "broccoli", then instead of "broccoli", "BLEEP" will be printed.
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
inline void keep_window_open() { char ch; cin >> ch; }
int main()
{
string bad_word = "broccoli";
cout << "type in some words\n";
vector <string> words;
for (string temp; cin >> temp; )
words.push_back(temp);
cout << "There are " << words.size() << " words in vector 'words'.\n";
for (string word : words) {
if (word != bad_word)
cout << word;
else
cout << "BLEEP";
}
keep_window_open();
return 0;
}
So, I have my program ask me for words, I print some, then hit Ctrl+Z + Enter, and nothing happens. If I do that again, the console closes. Why doesn't it print the number of words as specified by this line?
cout << "There are " << words.size() << " words in vector 'words'.\n";
Why does the program finish execution after I hit Ctrl+Z+Enter the second time?
I want to understand what the problem really is here.
Thank you.
ctrl-z is an end of file signal.
This disables reading from the input stream (cin).
So your keep window open function returns immediately without waiting(because reading is not possible), and your console closes.
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;
}
This question already has answers here:
Basic I/O not working in Visual C++ 2010?
(2 answers)
Closed 8 years ago.
I still don't know how this works with Visual Studio.
Supposedly it keeps my console open, but it doesn't. It still flashes and closes.
Am I doing something wrong?
#include <iostream>
using namespace std;
int main()
{
int a;
cout << "Please enter an integer: ";
cin >> a;
if (a == 1)
{
cout << endl << "You typed 1.";
}
else
cout << "That's not 1.";
cin.get();
return 0;
}
As it was already pointed out in the comments the problem is that the input buffer contains the symbol of the Enter key that is read by cin.get();
You can use either the following sequence
char c;
cin >> c;
Or before cin.get() you should call cin.ignore. For example
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
cin.get();
In the last case you must include header <limits>