C-PLOT

Scientific Graphics and Data Analysis

12.21.10. - Useful routines for interactive running



Several routines are available to provide easy interaction with the user of your fitting function while maintaining compatibility with quiet mode (used with plot filters) and with command files.

For printing to the screen, use the routine msg() rather than printf(). The routine msg() suppresses output when quiet mode is in effect. Otherwise, its behavior is identical to the standard printf().

For reading input from the keyboard (or command file) the following four routines are available:
get_dnum(prompt, num)   /* Input double from user */
char    *prompt;
double  *num;

get_inum(prompt, num)   /* Input integer from user */
char    *prompt;
int     *num;

get_snum(prompt, num)   /* Input string from user */
char    *prompt, *num;

yesno(prompt)           /* Check for positive response */
char    *prompt;
In each of these routines, if prompt is nonzero, the string it points to is printed, and in the first three routines, the current value of the double, integer or string pointed to by num also is printed. In each case, a line of text is then read from the keyboard (or command file) and scanned for something to stuff into the location pointed to by num. If no appropriate value is found on the line of text, the contents of num remain unchanged.

The return values of the first three functions are: 1 if the user simply enters <return>(.C num unchanged); 0 if the user entered something; and -1 if there was an end-of-file (the command file finished or the user entered a ^D). (Also, the externally defined integer eofflag is nonzero on an end-of-file condition after attempting input.)

The return values of the routine yesno() are: 1 if the input from the keyboard (or command file) begins with y, Y or 1; 0 if nothing is entered; and -1 for anything else. Examine the value eofflag to distinguish an end-of-file from a negative response.

Here is a sample code fragment showing how these routines might be used.
char    file[128] = "good_data";
double  norm = 1;

new_options() {
        if (yesno("Change options (NO)") > 0) {
                if (get_snum("New file name", file) == 0) {
                        /* open the file */
                }
                if (eofflag)
                        return;
                if (get_dnum("Normalization", &norm) == 0)
                        return;
        }
        if (eofflag)
                return;
        /* and so on */
}
A typical dialogue produced by the above might look like:
Change options (NO)? y
New file name (good_data)? something_else
Normalization (1)? 876.1
Notice how the current values are given in parenthesis. If the user enters a ^D as a response to any of the prompts, the function will return to the calling routine.