C++ Wait for User Input

Wait for user input in C?

From the GNU C Library Manual:

Function: char * fgets (char *s, int count, FILE *stream)

The fgets
function reads characters from the stream stream up to and including a
newline character and stores them in the string s, adding a null
character to mark the end of the string. You must supply count
characters worth of space in s, but the number of characters read is
at most count − 1
. The extra character space is used to hold the null
character at the end of the string.

So, fgets(key,1,stdin); reads 0 characters and returns. (read: immediately)

Use getchar or getline instead.

Edit: fgets also doesn't return once count characters are available on the stream, it keeps waiting for a newline and then reads count characters, so "any key" might not be the correct wording in this case then.

You can use this example to avoid line-buffering:

#include <stdio.h>
#include <termios.h>
#include <unistd.h>

int mygetch ( void )
{
int ch;
struct termios oldt, newt;

tcgetattr ( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr ( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr ( STDIN_FILENO, TCSANOW, &oldt );

return ch;
}

int main()
{
printf("Press any key to continue.\n");
mygetch();
printf("Bye.\n");
}

Wait for user input in while(1) loop

It doesn't even wait for the user input (which it's supposed to do.).

The program is not waiting to get the input means there is some character left in input buffer. possibly \n from previous input. So clear the input buffer before reading input.

you can put,

getchar();
scanf("%c",&invoer);

before scanf() inside loop;

fgets doesn't wait for user input in my C code

The fgets() function reads at most one less than the number of characters specified by size from the given stream and stores them in the string str.

Since all your variables are single characters, you're asking fgets for single byte. It reads one less, to keep room for a termimating mull byte, which is zero.

fgets reads strings not characters. You need to store it in a string, not a character. If you want to store just one character, allocate two bytes. One for the character, one for the terminating null byte.

char nome[2];
fgets(nome, sizeof(nome), stdin);

But I suspect you want to store more than one character. You have to allocate enough memory, plus one.

// enough memory for 100 characters, pus the terminating null byte.
char nome[101];
fgets(nome, sizeof(nome), stdin);

Or, better yet, allocate a single large buffer and copy it into a properly sized amount of memory.

char line[4096];
printf("Digite o nome: ");
fgets(line, sizeof(line), stdin);
char *nome = strdup(line);

printf("Digite o CPF: ");
fgets(line, sizeof(line), stdin);
char *cpf = strdup(cpf);

Program doesn't wait for user input with scanf( %c ,&yn);

printf("Please enter an output filename: ");    
scanf("%s",&outfilename);

When you enter the second string and hit the ENTER key, a string and a character are placed in the input buffer, they are namely: the entered string and the newline character.The string gets consumed by the scanf but the newline remains in the input buffer.

Further,

scanf("%c",&yn);

Your next scanf for reading the character just reads/consumes the newline and hence never waits for user input.

Solution is to consume the extra newline by using:

scanf(" %c", &yn);
^^^ <------------Note the space

Or by using getchar()

You may want to check out my answer here for a detailed step by step explanation of the problem.

C++ wait for user input

Several ways to do so, here are some possible one-line approaches:


  1. Use getch() (need #include <conio.h>).

  2. Use getchar() (expected for Enter, need #include <iostream>).

  3. Use cin.get() (expected for Enter, need #include <iostream>).

  4. Use system("pause") (need #include <iostream>, Windows only).

    PS: This method will also print Press any key to continue . . . on the screen. (seems perfect choice for you :))


Edit: As discussed here, There is no completely portable solution for this. Question 19.1 of the comp.lang.c FAQ covers this in some depth, with solutions for Windows, Unix-like systems, and even MS-DOS and VMS.

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;
}

Scanf and While Loop not waiting for user input

The problem is that after the user entered their year and pressed enter the new line character was left in the input buffer so in the next pass it reads '\n' and exits the loop. Use scanf(" %c", &again); inside the while loop.

int main (void)
{
char again;
printf("Would you like to check if a year is a leap year? y or n\n");
scanf("%c", &again);
while (again == 'y')
{
leapyear();
printf("Would you like to check if a year is a leap year? y or n\n");
scanf(" %c", &again);
}
return 0;
}

Why does the program exit, without waiting for the user input?

Please see the modified code.

scanf("%c",&a);

Problem: scanf reads \n (ASCII:10) from the input buffer, and does not wait further for user input. The value stored in a will be 10 (\n). Since a is not equal to "Y' or 'N', program exits.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
int n,i;
int *ptr,*old_ptr;
int sum = 0;
char a;

printf("Enter the number of elements to be added: ");
scanf("%d",&n);

ptr = calloc(n,sizeof(int));
old_ptr=ptr;

printf("Enter the elements:\n");

for(i = 0;i < n;i++){
scanf("%d",ptr);
sum = sum + *ptr;
ptr++;
}
printf("The sum total of the numbers is: %d\n",sum);

printf("Do you want to enter more numbers ?Press Y/N: \n");
getchar();
scanf("%c",&a);
if(a == 'Y'){
printf("You have entered: %c\n\n",a);
ptr = realloc(old_ptr,sizeof(int)*n);

printf("Enter the elements:\n");

for(i = 0;i < n;i++){
scanf("%d",ptr);
sum = sum + *ptr;
ptr++;
}
printf("The total of the numbers is: %d\n\n\n",sum);
}
if(a == 'N'){
printf("Program finished!");
}
return 0;
}

Changes required are:

  1. Change ptr = old_ptr to old_ptr=ptr.
    Reason: Assignment operator's associativity is right to left.

  2. Change scanf("%d",&ptr) to scanf("%d",ptr)
    Reason: ptr holds the required address. No need of &.

  3. Put the getchar function to read the extra \n

  4. Remove the extra \n in the printf statements.

After these modifications, it worked. Please see the image.Sample Image



Related Topics



Leave a reply



Submit