Thanks guys.
Code supplied:

static void getuserinput(char *prompt, char *reply);

static int getans(char *prompt, char *choices);

int main(int argc, char **argv)
{

    char namebuf[NAME_MAX];
    char typebuf[NAME_MAX];
    char defltbuf[NAME_MAX];
    char codebuf[NAME_MAX];
    char *eols = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";

    fputs(eols, stdout);    // clear terminal window

    //char ans = getans("Make a choice.\n", "123");

    // Option variable name.
    getuserinput("Enter variable name: ", namebuf);
    // Option variable type.
    getuserinput("Enter variable type: ", typebuf);
    // Option default value.
    getuserinput("Enter variable default value: ",
                                defltbuf);
    // Option C code.
    getuserinput("Begining with an assignment operator,"
                        " enter C code for this option:\n ",
                            codebuf);

    return 0;
}

void getuserinput(char *prompt, char *reply)
{
    char buf[NAME_MAX];
    fputs(prompt, stdout);
    char *cp = fgets(buf, NAME_MAX, stdin);
    cp = strchr(buf, '\n');
    if (cp) *cp = '\0';    // don't need eol in the result
    strcpy(reply, buf);
} // getuserinput()

int getans(char *prompt, char *choices)
{
    /* Prompt the user with prompt then loop showing choices until
     * the user enters something contained in choices.
     * Alphabetic choices like "Yn" will be case insensitive.
    */
    char testchoices[16];
    char shortprompt[80];
    fputs(prompt, stdout);
    char c;
    strcpy(testchoices, choices);
    size_t i;

    // to be case insensitive when choices is alpha.
    for (i = 0; i < strlen(testchoices); i++) {
        testchoices[i] = toupper(testchoices[i]);
    }

    sprintf(shortprompt, "Choose one of: \"%s\" ", choices);
    while (1) {
        fputs(shortprompt, stdout);
        fflush(stdin);
        scanf(" %c", &c);
        c = toupper(c);
        if (strchr(testchoices, c)) {
            break;
        }
    }
    return c;
} // getans()


In my example this line is commented out:
    //char ans = getans("Make a choice.\n", "123");

On compilation in this state I get this result:

Enter variable name: avar
Enter variable type: int
Enter variable default value: 0
Begining with an assignment operator, enter C code for this option:
 = 1

which is exactly what I want.

OTOH when I take the comments off that line here is what I get:
Make a choice.
Choose one of: "123" 1
*Enter variable name: Enter variable type: *
Enter variable default value: 0
Begining with an assignment operator, enter C code for this option:
 = 1

So getans() is the origin of the problem.
I have spent quite some time rewriting that and in the above it is the
cleanest version I've written but it has clearly caused problems down the
track.

Thanks


On Thu, Sep 3, 2015 at 1:27 AM, Ángel González <an...@16bits.net> wrote:

> Robert Parker wrote:
> > Yet when I attempt the same in a C program, the system always writes
> > 2 prompts, then waits for a read.
> > Does not matter if I write(1, "..."); read(0, number, buffer); or use
> > fputs("...", stdout); fgets(.....
> > The result is the same.
> > And I have tried using readline without any better result.
>
> I completely agree with Bob. It looks like your C program were doing a
> loop (which your shell script doesn't!) and there's a wrong condition
> somewhere.
> But you need to show the code that is failing (provide a minimal
> program that exhibits the failure).
>



-- 
The Bundys, Cliven, Ted and Al. Great guys to look up to.

Reply via email to