Multiple function selection

Discussion in 'C Programming' started by Les Coover, Nov 26, 2003.

  1. Les Coover

    Les Coover Guest

    I have tried writing the selection process after
    scanf many different ways. No matter what
    is selected program execution goes to the
    add_data function. How can I get this to work?

    /* em_at01.c */

    #include <stdio.h>

    void add_data(void);
    void print_data(void);
    void query_data(void);

    int main(void)
    {
    int select;
    printf("\nWHAT DO YOU WANT TO DO?\n\n");
    printf("%s\n\n", "SELECTION PROCEDURE");
    printf("%s\n", " 1 Add Record/s");
    printf("%s\n", " 2 Print Database");
    printf("%s\n", " 3 Query Database");
    printf("%s\n", " Ctrl + C Exit");

    scanf("%d", select);
    if (select = 1)
    add_data();
    else if (select = 2)
    print_data();
    else if (select = 3)
    query_data();
    else

    return 0;
    }

    void add_data(void)
    {
    printf("this is add_data (void)");
    }

    void print_data(void)
    {
    printf("this is print_data (void)");
    }

    void query_data(void)
    {
    printf("this is query_data (void)");
    }

    Thanks, Les
     
    Les Coover, Nov 26, 2003
    #1
    1. Advertising

  2. Les Coover

    Ed Morton Guest

    On 11/26/2003 11:25 AM, Les Coover wrote:
    > I have tried writing the selection process after

    <snip>
    > if (select = 1)


    NOTE: you mean "==", not "=". By the way, you may want to use a switch statement
    instead of a chain of "if"s here....

    Ed.
     
    Ed Morton, Nov 26, 2003
    #2
    1. Advertising

  3. Les Coover

    Les Coover Guest

    "Ed Morton" <> wrote in message
    news:...
    >
    >
    > On 11/26/2003 11:25 AM, Les Coover wrote:
    > > I have tried writing the selection process after

    > <snip>
    > > if (select = 1)

    >
    > NOTE: you mean "==", not "=". By the way, you may want to use a switch

    statement
    > instead of a chain of "if"s here....
    >
    > Ed.
    >


    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.

    Tried

    scanf("%d", select);
    switch (select)
    {
    case 1:
    add_data();
    break;
    case 2:
    print_data();
    break;
    case 3:
    query_data();
    break;
    }

    does same thing.

    Even tried

    switch (select)
    {
    case 1:
    add_data();

    case 2:
    print_data();

    case 3:
    query_data();

    }

    Same thing.

    Les
     
    Les Coover, Nov 26, 2003
    #3
  4. Les Coover

    Dan Pop Guest

    In <Gk5xb.41503$yJ.18575@okepread02> "Les Coover" <> writes:

    >I have tried writing the selection process after
    >scanf many different ways. No matter what
    >is selected program execution goes to the
    >add_data function. How can I get this to work?


    Ever considered reading a C book?

    There are several mistakes in your program and all of them are of the
    most basic kind. You really have no clue about C.

    This homework assignment really begs for an array of function pointers,
    but you have plenty of other, most basic things, to learn until then.

    >/* em_at01.c */
    >
    >#include <stdio.h>
    >
    >void add_data(void);
    >void print_data(void);
    >void query_data(void);
    >
    >int main(void)
    >{
    > int select;
    > printf("\nWHAT DO YOU WANT TO DO?\n\n");
    > printf("%s\n\n", "SELECTION PROCEDURE");
    > printf("%s\n", " 1 Add Record/s");
    > printf("%s\n", " 2 Print Database");
    > printf("%s\n", " 3 Query Database");
    > printf("%s\n", " Ctrl + C Exit");
    >
    > scanf("%d", select);


    Wrong. Also no attempt to check whether the function call succeeded.

    > if (select = 1)


    Wrong.

    > add_data();
    > else if (select = 2)


    Wrong.

    > print_data();
    > else if (select = 3)


    Wrong.

    > query_data();
    > else


    Wrong.

    > return 0;
    >}
    >
    >void add_data(void)
    >{
    >printf("this is add_data (void)");


    Wrong.

    >}
    >
    >void print_data(void)
    >{
    >printf("this is print_data (void)");


    Wrong.

    >}
    >
    >void query_data(void)
    >{
    >printf("this is query_data (void)");


    Wrong.

    >}


    Do yourself a favour and compile your code with gcc -Wall -O. It would
    have caught and reported many of your mistakes.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Nov 26, 2003
    #4
  5. Les Coover

    Les Coover Guest

    "Les Coover" <> wrote in message
    news:Cl6xb.41778$yJ.5251@okepread02...
    >
    > "Ed Morton" <> wrote in message
    > news:...
    > >
    > >
    > > On 11/26/2003 11:25 AM, Les Coover wrote:
    > > > I have tried writing the selection process after

    > > <snip>
    > > > if (select = 1)

    > >
    > > NOTE: you mean "==", not "=". By the way, you may want to use a switch

    > statement
    > > instead of a chain of "if"s here....
    > >
    > > Ed.
    > >

    >
    > 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.
    >
    > Tried
    >
    > scanf("%d", select);
    > switch (select)
    > {
    > case 1:
    > add_data();
    > break;
    > case 2:
    > print_data();
    > break;
    > case 3:
    > query_data();
    > break;
    > }
    >
    > does same thing.
    >
    > Even tried
    >
    > switch (select)
    > {
    > case 1:
    > add_data();
    >
    > case 2:
    > print_data();
    >
    > case 3:
    > query_data();
    >
    > }
    >
    > Same thing.
    >
    > Les
    >
    >

    Thanks for help Ed, here is how I solved the problem:

    /* em_atr03.c */

    #include <stdio.h>
    #include <string.h>

    void add_data(void);
    void print_data(void);
    void query_data(void);

    int main(void)
    {
    int select;
    char color[100];

    printf("\nWHAT DO YOU WANT TO DO?\n\n");
    printf("%s\n\n", "SELECTION PROCEDURE");
    printf("%s\n", " yellow Add Record/s");
    printf("%s\n", " green Print Database");
    printf("%s\n", " blue Query Database");
    printf("%s\n", " Ctrl + C Exit");

    scanf("%99s", color);
    if(strcmp(color, "yellow")== 0)
    add_data();
    if(strcmp(color, "green")== 0)
    print_data();
    if(strcmp(color, "blue")== 0)
    query_data();
    else
    return 0;
    }

    void add_data(void)
    {
    printf("this is add_data (void)");
    }

    void print_data(void)
    {
    printf("this is print_data (void)");
    }

    void query_data(void)
    {
    printf("this is query_data (void)");
    }

    Les
     
    Les Coover, Nov 26, 2003
    #5
  6. Les Coover

    Eric Sosman Guest

    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.

    --
     
    Eric Sosman, Nov 26, 2003
    #6
  7. Dan Pop wrote:

    > In <Gk5xb.41503$yJ.18575@okepread02> "Les Coover" <> writes:
    >


    >>
    >>int main(void)
    >>{
    >>int select;
    >>printf("\nWHAT DO YOU WANT TO DO?\n\n");
    >>printf("%s\n\n", "SELECTION PROCEDURE");
    >>printf("%s\n", " 1 Add Record/s");
    >>printf("%s\n", " 2 Print Database");
    >>printf("%s\n", " 3 Query Database");
    >>printf("%s\n", " Ctrl + C Exit");
    >>
    >>scanf("%d", select);

    >
    >
    > Wrong. Also no attempt to check whether the function call succeeded.


    The bigger problem here, which I only just noticed, is that he's passing
    an int where scanf expects a pointer to int.

    scanf("%d", &select);

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Nov 26, 2003
    #7
  8. Kevin Goodsell wrote:

    > Dan Pop wrote:
    >
    >> In <Gk5xb.41503$yJ.18575@okepread02> "Les Coover"
    >> <> writes:
    >>

    >
    >>>
    >>> int main(void)
    >>> {
    >>> int select;
    >>> printf("\nWHAT DO YOU WANT TO DO?\n\n");
    >>> printf("%s\n\n", "SELECTION PROCEDURE");
    >>> printf("%s\n", " 1 Add Record/s");
    >>> printf("%s\n", " 2 Print Database");
    >>> printf("%s\n", " 3 Query Database");
    >>> printf("%s\n", " Ctrl + C Exit");
    >>>
    >>> scanf("%d", select);

    >>
    >>
    >>
    >> Wrong. Also no attempt to check whether the function call succeeded.

    >
    >
    > The bigger problem here, which I only just noticed, is that he's passing
    > an int where scanf expects a pointer to int.
    >
    > scanf("%d", &select);
    >


    Sorry, I misread that. You caught the error I mentioned, and then went
    on to describe a second error. I thought you were only talking about the
    failure to check the return value.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Nov 26, 2003
    #8
  9. Les Coover

    Dan Pop Guest

    In <HP7xb.15861$> Kevin Goodsell <> writes:

    >Dan Pop wrote:
    >
    >> In <Gk5xb.41503$yJ.18575@okepread02> "Les Coover" <> writes:
    >>

    >
    >>>
    >>>int main(void)
    >>>{
    >>>int select;
    >>>printf("\nWHAT DO YOU WANT TO DO?\n\n");
    >>>printf("%s\n\n", "SELECTION PROCEDURE");
    >>>printf("%s\n", " 1 Add Record/s");
    >>>printf("%s\n", " 2 Print Database");
    >>>printf("%s\n", " 3 Query Database");
    >>>printf("%s\n", " Ctrl + C Exit");
    >>>
    >>>scanf("%d", select);

    >>
    >>
    >> Wrong. Also no attempt to check whether the function call succeeded.

    >
    >The bigger problem here, which I only just noticed, is that he's passing
    >an int where scanf expects a pointer to int.
    >
    >scanf("%d", &select);


    Why do you think I wrote "wrong"?

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Nov 27, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Niederberger
    Replies:
    2
    Views:
    16,617
    Christian Kaufhold
    Jan 7, 2005
  2. Andrew Crowe
    Replies:
    1
    Views:
    4,496
    Andrew Crowe
    Sep 13, 2004
  3. curiousity

    HOWTO autopost the selection list upon selection

    curiousity, Nov 21, 2003, in forum: ASP .Net Mobile
    Replies:
    0
    Views:
    280
    curiousity
    Nov 21, 2003
  4. Loebb
    Replies:
    0
    Views:
    210
    Loebb
    Feb 23, 2004
  5. Replies:
    1
    Views:
    217
Loading...

Share This Page