Les Coover wrote:
>
> Tried
>
> scanf("%d", select);
> if (select == 1)
> add_data();
> else if (select == 2)
> print_data();
> else if (select == 3)
> query_data();
> else
>
> Wont call any function, focus goes back to prompt.
> [...]
First, you should check whether the scanf() call
actually converted anything. You've told it to expect
a number, but if it instead encounters a Q or a $ or
some other non-numeric garbage it will be unable to
produce a value for `select'. The value returned by
the scanf() function tells you how many items were
successfully converted; in your case, any value other
than 1 means there was trouble.
Second, when scanf() *does* encounter trouble of
this sort, it can be tricky to recover from it and
proceed. You can't just re-issue the scanf() call,
because the unconvertable garbage is still sitting
there, unconsumed, and will bollix the second call
just as surely as it bollixed the first. scanf()
looks easy, but it's difficult to use for interactive
input. See Question 12.19 (in fact, see all of 12.12
through 12.20) in the comp.lang.c Frequently Asked
Questions (FAQ) list
http://www.eskimo.com/~scs/C-faq/top.html
Finally, whenever you're confronted with some
inexplicable behavior in your program it's often helpful
to take a look at the values the program is actually
chewing on, as opposed to the values you *thought* it
was ingesting. One way to do this is with a debugger;
my personal feeling is that debuggers are overkill for
simple problems, and that you can do at least as well
by adding a few printf() or fprintf() calls at strategic
points in the program. In your case, `select' is clearly
the variable directly involved in whatever's going on, so
just after reading it and before using it I suggest you
printf ("You chose (I think) %d\n", select);
In situations like this, the surprising outputs are the
most illuminating.
--