Thread: Check if user presses the Enter key

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    23

    Check if user presses the Enter key

    I'm writing a program where 10 values are entered and then the max, min, and mean of the user entered values are displayed.

    Now i need to add a part where if the user does not want to enter 10 values, they can just press the enter key and 10 defined numbers that i make will be used instead.

    What I need help with is how to check if the user only presses the enter key. I've never done this before considering i am in basic c programming and i don't know where to start. If anyone can help me out it'd be greatly appreciated.

    Thanks

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
      if (getchar() == '\n')
        puts("User pressed Enter");
    So... flip answers aside... post your code, lets see what we can do...
    Last edited by CommonTater; 09-18-2011 at 07:18 PM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How are you reading your numbers? Are your numbers only single digits? Your input, and how you read it, or how you consider it valid or not, should guide you here.

    scanf - scans specifically formatted input
    getchar - read a single character
    fgets + sscanf - read a block of input, terminated by length or hitting enter + scan it for a specific format

    There are multiple ways to read input. You can look in the FAQ for various articles on getting input.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    23
    should i use scanf if I'm using floats?

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Why not? %f for float, %lf for double. There are many ways to obtain input, if you are using scanf you can check the return value to determine what you need to do.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    For that matter, the easiest way might be to do something like this...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main (void)
      { 
        int array[10];
    
        // fill with random numbers
        srand(time());
        for (int i = 0; i < 10; i++)
           array[i] = rand % 1000;
    
        // get user's input
        printf("Enter 10 numbers : ");
        for(int i = 0; i < 10; i++)
           if (scanf("%d",  &array[i]) != 1)
             break;
    
       // display array
       printf("Here is the list  ");
       for(int i = 0; i < 10; i++)
          printf("%d" , array[i]);
    
    
    
       // do all your clever math stuff here
    
    
    
      return 0;
    }
    If at any time the user makes an inapproriate input it will continue on with the random numbers seeded into the array from that point on...

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    23
    I still cannot get this to work. If the user enters a alphabet character and presses enter it says the user pressed the enter key, but if they just press enter it doesn't do anything but wait for the next floating point number.

    Here is my code, did i forget anything?:

    Code:
    #include <stdio.h>
    #define size 3
    
    void main(void)
    {
    
    float num[size];
    int i;
    float min;
    float max;
    float avg;
    float sumofnum;
    
    printf("Enter 10 floating point numbers: \n");
    for (i = 0; i < size; i++)
    {
    	scanf("%f", &num[i]);
    }
    
    
    /*Check if character is entered*/
    
    if(getchar() =='\n')
    {
    /*Minimum Number*/
    min = num[0];
    
    for (i = 0; i < size; i++)
    {
    	if(num[i] < min)
    	{
    		min = num[i];
    	}
    }
    
    /*Maximum Number*/
    max = num[0];
    
    for (i = 0; i < size; i++)
    {
    	if(num[i] > max)
    	{
    		max = num[i];
    	}
    }
    
    /*Average of Numbers*/
    
    for (i = 0; i < size; i++)
    {
    	sumofnum += num[i];
    }
    
    	avg = sumofnum / size;
    
    printf("The minimum number you have entered is: %.2f\n", min);
    printf("The maximum number you have entered is: %.2f\n", max);
    printf("The average of the numbers you entered is: %.2f\n", avg);
    }
    else
    	printf("You pressed enter\n");
    
    return;
    }
    Thanks

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are mixing your types of input functions. C really isn't a fan of that. Why don't you just use fgets + sscanf.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... look at your code...
    First of all ... learn some consistent scheme of indentation. The indents should let you see, very clearly whare blocks of code start and end... Look at my post (#6 in the thread) for an example.

    Your code still does not recognize the "user pressed enter" which you can only do by testing the return value of scanf() ... once again look at my example to see how it works... As your code is now, the user has no choice but to enter 10 numbers. they can hit enter all they want and it's just going to keep right on waiting for all ten numbers.

    You seriously need to look at the examples and directions people are giving you and you need to look the functions up in your C library docs...
    Really, my friend... help files are your friends!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf to return after user presses enter?
    By sp2 in forum C Programming
    Replies: 9
    Last Post: 04-23-2008, 05:33 PM
  2. Getting user key presses
    By Mavix in forum C# Programming
    Replies: 4
    Last Post: 08-09-2007, 01:49 PM
  3. user can hit enter all day long
    By Ash1981 in forum C Programming
    Replies: 7
    Last Post: 01-01-2006, 05:33 AM
  4. mkae it so when the user presses...
    By Rune Hunter in forum C++ Programming
    Replies: 10
    Last Post: 09-27-2004, 10:30 PM
  5. execute until user presses a key?
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-31-2002, 11:31 PM