Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Accept default value at user input

Rusty

Active Coder
Hello again
Trying to find a method that when prompted to enter a value, the user presses the 'Enter' key for the program to accept a default value. Apparently there is not a consistent ascii code of 'Enter' that I can use to compare, if I understand it correctly. Here is a snippet of my example. I used the highlight option this time- not sure if correctly.

btw, the code, in entirety, works. Thanks.

Edit- I don't see anything highlighted :confused:

[CODE title="Snippet" highlight="1-21"]
int h, temp, response = 0;
int local_press = 760;

printf("\n1\tAtmospheric pressure known (mmHg) <760>\n");
printf("2\tAltitude and air temperature known\n");
printf("\nPlease enter a choice (1 or 2): ");
response = get_user_input();

if (response == 1 )
{
printf("\n Enter Atmospheric Pressure [mmHg] <760>");
local_press = get_user_input();
}
else
{
printf("\n Enter Altitude [Meters]:");
h = get_user_input();
printf("\n Enter Air Temperature [Celcius]: ");
temp = get_user_input();
local_press = calc_atm_press(h, temp);
}
[/CODE]
 
I used the highlight option this time- not sure if correctly.
What I meant was selecting 'C' in the language when you include code ;).
Here an example:
C:
#include <stdio.h>

int main()
{
    char c = getchar();

    if(c == '\n')
        puts("Enter.");
    else
        printf("Key: %c.\n", c);

    return 0;
}
The character '\n' equals 'enter'.
If the value of the character is 'enter', you use the default value. Otherwise, you read the rest of the string (With scanf or something).
 
Last edited:

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom