Press Anykey to Continue in Linux C++

Press Any Key to Continue function in C

Use the C Standard Library function getchar() instead as getch() is not a standard function, being provided by Borland TURBO C for MS-DOS/Windows only.

printf("Let the Battle Begin!\n");
printf("Press Any Key to Continue\n");
getchar();

Here, getchar() expects you to press the return key so the printf statement should be press ENTER to continue. Even if you press another key, you still need to press ENTER:

printf("Let the Battle Begin!\n");
printf("Press ENTER key to Continue\n");
getchar();

If you are using Windows then you can use getch()

printf("Let the Battle Begin!\n");
printf("Press Any Key to Continue\n");
getch();
//if you press any character it will continue ,
//but this is not a standard c function.


char ch;
printf("Let the Battle Begin!\n");
printf("Press ENTER key to Continue\n");
//here also if you press any other key will wait till pressing ENTER
scanf("%c",&ch); //works as getchar() but here extra variable is required.

Press anykey to continue in Linux C++

In *nix, terminals usually wait for a whole line of input before sending anything to the program. Which is why the example code you posted said "Press Enter to Continue...";, and then discarded everything until the next newline.

To avoid that, you should put your terminal in non-canonical mode, which can be done using the POSIX termios(3) functions, as explained in How to check if a key was pressed in Linux?.

Wait for press enter in C inside a while loop?

If this code (with additional fflush)

#include <stdio.h>
#include <stdlib.h>
int main()
{
int choice;
while(1){
printf("1.Create Train\n");
// print other options
printf("\nEnter Your choice : ");
fflush(stdin);
scanf("%d", &choice);
// do something with choice
// ...
// ask for ENTER key
printf("Press [Enter] key to continue.\n");
fflush(stdin); // option ONE to clean stdin
getchar(); // wait for ENTER
}
return 0;
}

does not work properly.

Try this code (with loop):

#include <stdio.h>
#include <stdlib.h>
int main()
{
int choice;
while(1){
printf("1.Create Train\n");
// print other options
printf("\nEnter Your choice : ");
fflush(stdin);
scanf("%d", &choice);
// do something with choice
// ...
// ask for ENTER key
printf("Press [Enter] key to continue.\n");
while(getchar()!='\n'); // option TWO to clean stdin
getchar(); // wait for ENTER
}
return 0;
}

Press ENTER to continue (Linux & C)

Please try the one below and let me know. I think that getChar is used by that scanf you wrote. A while reading a separate char can rule that out. I wrote \r in case you use other operating systems.

void setDeposit(int account, int amount)
{
printf("You have successfully transfered %d EUR to the account number %d\nPlease press ENTER to continue\n", account, amount);

char myChar = 0;
while (myChar != '\n' && myChar != '\r') {
myChar = getchar();
}
}

Detailed information about the case can be found within this thread.

How do I read in the Enter key as an input in C?

You can use the getc function in stdio.h to wait until a key is pressed. This C code will wait for the enter key to be pressed then output "Continued!":

#include <stdio.h>

int main(){
printf("Press any key to continue");
getc(stdin);
printf("Continued!");
return 0;
}

Press Enter to Continue in C


printf("Press enter to continue\n");
char enter = 0;
while (enter != '\r' && enter != '\n') { enter = getchar(); }
printf("Thank you for pressing enter\n");

How to do press any key to continue prompt?

Just place braces {} for the appropriate if (as pointed out in the comments) and your program will work.

Note also that there is no need to use ifstream twice as you did in your program.

#include <fstream>
#include <string>
#include <iostream>

int main()
{

std::string fileName;
std::cout<<"Enter filename"<<std::endl;
std::getline(std::cin, fileName);

std::ifstream file(fileName);

std::string line;
int count = 1;
if(file)
{
while (std::getline(file, line))
{
std::cout << count << ". " << line << '\n' ;

if (count % 25 == 0)
{
std::cout<<"Press enter to continue"<<std::endl;
std::cin.get();
}
count++;
}
}
else
{
std::cout<<"file cannot be opened"<<std::endl;
}

return 0;
}


Related Topics



Leave a reply



Submit