SDDSlib
Loading...
Searching...
No Matches
query.c File Reference

Provides functions to prompt the user for various types of numerical input. More...

#include "mdb.h"

Go to the source code of this file.

Functions

double query_double (char *prompt, double def)
 Prompts the user for a double precision number.
 
float query_float (char *prompt, float def)
 Prompts the user for a float precision number.
 
long query_long (char *prompt, long def)
 Prompts the user for a long integer.
 
int query_int (char *prompt, int def)
 Prompts the user for an integer.
 
short query_short (char *prompt, short def)
 Prompts the user for a short integer.
 

Detailed Description

Provides functions to prompt the user for various types of numerical input.

This file contains functions that display prompts to the terminal for different numerical types, show default values, and return the user-entered value or the default if no input is provided.

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, C. Saunders, R. Soliday

Definition in file query.c.

Function Documentation

◆ query_double()

double query_double ( char * prompt,
double def )

Prompts the user for a double precision number.

Displays a prompt with a default value. If the user does not enter a number, the default value is returned.

Parameters
promptThe message to display to the user.
defThe default double value to return if no input is provided.
Returns
The double value entered by the user or the default value.

Definition at line 32 of file query.c.

32 {
33 static char s[100];
34 static double val;
35
36 printf("%s [%g]: ", prompt, def);
37 fgets(s, 99, stdin);
38 val = def;
39 if (*s)
40 sscanf(s, "%lf", &val);
41 return (val);
42}

◆ query_float()

float query_float ( char * prompt,
float def )

Prompts the user for a float precision number.

Displays a prompt with a default value. If the user does not enter a number, the default value is returned.

Parameters
promptThe message to display to the user.
defThe default float value to return if no input is provided.
Returns
The float value entered by the user or the default value.

Definition at line 54 of file query.c.

54 {
55 static char s[100];
56 static float val;
57
58 printf("%s [%g]: ", prompt, def);
59 fgets(s, 99, stdin);
60 val = def;
61 if (*s)
62 sscanf(s, "%f", &val);
63 return (val);
64}

◆ query_int()

int query_int ( char * prompt,
int def )

Prompts the user for an integer.

Displays a prompt with a default value. If the user does not enter a number, the default value is returned.

Parameters
promptThe message to display to the user.
defThe default integer value to return if no input is provided.
Returns
The integer entered by the user or the default value.

Definition at line 98 of file query.c.

98 {
99 static char s[100];
100 static int val;
101
102 printf("%s [%d]: ", prompt, def);
103 fgets(s, 99, stdin);
104 val = def;
105 if (*s)
106 sscanf(s, "%d", &val);
107 return (val);
108}

◆ query_long()

long query_long ( char * prompt,
long def )

Prompts the user for a long integer.

Displays a prompt with a default value. If the user does not enter a number, the default value is returned.

Parameters
promptThe message to display to the user.
defThe default long integer value to return if no input is provided.
Returns
The long integer entered by the user or the default value.

Definition at line 76 of file query.c.

76 {
77 static char s[100];
78 static long val;
79
80 printf("%s [%ld]: ", prompt, def);
81 fgets(s, 99, stdin);
82 val = def;
83 if (*s)
84 sscanf(s, "%ld", &val);
85 return (val);
86}

◆ query_short()

short query_short ( char * prompt,
short def )

Prompts the user for a short integer.

Displays a prompt with a default value. If the user does not enter a number, the default value is returned.

Parameters
promptThe message to display to the user.
defThe default short integer value to return if no input is provided.
Returns
The short integer entered by the user or the default value.

Definition at line 120 of file query.c.

120 {
121 static char s[100];
122 static short val;
123
124 printf("%s [%d]: ", prompt, def);
125 fgets(s, 99, stdin);
126 val = def;
127 if (*s)
128 sscanf(s, "%hd", &val);
129 return (val);
130}