How to undo system("pause") [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm making a program in C++ and did some experimenting. I included the system('pause') function. Now obviously I cannot close the program. How do I undo it and quit the program? Do I really need to restart my PC?
Basically I pranked my self. Here is the src code
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
char answer;
cout << "Can gh0st be VIP?:(y=Yes/n=No)";
cout << endl;
cin >> answer;
if (answer == 'y')
{
cout << "thanx ande =)";
cout << endl;
}
else if (answer == 'n')
{
cout << "time to pay ande ahaha!\a\a\a\a\a\a\a...*with at least 200 more of these*";
cout << endl;
}
else
cout << "just put the letter y or n lol try it again";
cout << endl;
system("pause");
return 0;
}

Press "ENTER". .

Instead of doing system("pause") I would encourage doing a
cin.ignore();
cin.get();
This is a longer form of pause that I have found is very easy to use. And also you just hit the little red square close to the help menu.

pause is not part of C or C++, this is a command from Windows Command Prompt, which is called using the system() syscall.
Once system() is called, your thread hangs at that point until whatever program or command you call with system() is concluded and returns the exit code.
pause is a shell command that will wait for any key to be pressed by the user. Any key pressed will interrupt pause and your program will continue.
Just make sure the key you press is NOT the reset button.

Related

How to make a console application close automatically after command in C++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I would like to know How to make a console application close automatically after command in C++?
I want my program to ask "1) continue 2) Exit" and obviously proceed with the selected choice.
This is what I got so far:
#include <iostream>
int main() {
int choice;
std::cout << "1) continue 2) Exit. \n\n";
std::cout << std::endl;
std::cin >> choice;
std::cout << std::endl;
if (choice == 1) {
std::cout << " choice 1 selected, let's continue!";
}
if (choice != 1) {
//then close automatically?
}
}
If you really want to shut your window automatically when debugging stops,
"To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops."
Edit:
I forgot to mention that this is for Visual Studio 2019, Community Edition.

Redirecting to a webpage in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am making a program in C++, in which a web link is there so that when i type an input or simply press enter it will redirect me to that link which i have entered.
For example if i have 3 options and i choose option A, the program will redirect me to that link which is in the option A.
Here's a sample:-
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
system("COLOR B1");
int choice;
cout << "Menu" << endl;
cout << "1. Pasta" << "\n";
cout << "2. Cold Drink" << "\n";
cout << "Your choice (1-2)" << "\n";
cin >> choice;
if(choice == 1)
{
cout << "Thanks" << "\n"; //Here i want a url and when i choose 1 it
//will direct me to that url
}
else if(choice == 2)
{
cout << "Thanks" << "\n"; // And here also...
}
else
{
return 0;
}
}
Please help.
Thanks
In Windows desktop programs you can use the ShellExecute API with the open operation to open a URL with the default application (generally a web browser).
ShellExecute(NULL, L"open", L"https://example.com", nullptr, nullptr, SW_SHOWNORMAL);
Store apps can not use ShellExecute at all, but you can use the UWP LaunchUriAsync.
task = Windows::System::Launcher::LaunchUriAsync(
ref new Windows::Foundation::Uri("https://example.com"));
The various other platforms often have their own API's. In general you want to find and use them and not to assume any particular browser executable is on the path preventing users from using another or installing to a non-default location (or getting broken by updates, etc.).

Black screen in c++ disappears to fast [duplicate]

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.

Click the alphabet twice? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
void password()
{
int i=0, j;
char str[30], ch;
gotoxy (23,10);
cout<< "Welcome to our project! \n";
delay (800);
gotoxy (23,11);
cout<< "Please enter the password = ";
while ((ch=getch())!=13)
{
ch=getch();
str[i]=ch;
i++;
clrscr();
gotoxy (20,20);
cout<< "Enter Password = ";
for (j=0; j<i; j++)
cout<< "*";
}
str[i]='\0';
if (strcmp(str,"script")!=0)
{
cout<< endl;
cout<< "Incorrect Password! \n";
cout<< "Access Denied! \n";
getche();
exit (0);
}
}
i made this a while back, and i came across a problem. So the problem is that, there are no errors whatsoever when I compile the program and run it. The only issue is that when it asks me to enter the password for the script, I have to PRESS every alphabet TWICE and it shows up once (for example, password is TOLS, I have to press it like TTOOLLSS and it shows up as TOLS). How can I fix it?
Remove the line:
ch=getch();
since you already got the char in the line:
while ((ch=getch())!=13)

C++ How check characters in real time with _getch() [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
i need in my loop get character in realtime and check it by conditions. If user press whatever except enter, program works fine. Can anyone help me ? thanks !
while (read != '\n')
{
cout << "Enter character:\n";
read = _getwch();
if (read == '\n') {
cout << "You pressed : ENTER\n";
}
else {
cout << "Your character is: \"" << read << "\"\n\n";
read = '\0';
}
}
include
using namespace std;
int main()
{
cout << "Press the ENTER key";
if (cin.get() == '\n')
{
cout << "Good job.\n";
}
else
{
cout << "I meant ONLY the ENTER key... Oh well.\n";
}
return 0;
}
This code will help in detecting the ENTER key when pressed.
Hope this helps you.