SDDS ToolKit Programs and Libraries for C and Python
Loading...
Searching...
No Matches
query.c File Reference

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.

#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.
 

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 char s[100];
34 double val;
35
36 printf("%s [%g]: ", prompt, def);
37 if (!fgets(s, sizeof(s), stdin))
38 return def;
39 val = def;
40 if (*s)
41 sscanf(s, "%lf", &val);
42 return (val);
43}

◆ 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 55 of file query.c.

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

◆ 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 101 of file query.c.

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

◆ 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 78 of file query.c.

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

◆ 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 124 of file query.c.

124 {
125 char s[100];
126 short val;
127
128 printf("%s [%d]: ", prompt, def);
129 if (!fgets(s, sizeof(s), stdin))
130 return def;
131 val = def;
132 if (*s)
133 sscanf(s, "%hd", &val);
134 return (val);
135}