Like Tree2Likes

beginners question

This is a discussion on beginners question within the C Programming forums, part of the General Programming Boards category; I have a question....and I hope you guys can answer....Is there a way to make a selection (at the beginners ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    5

    beginners question

    I have a question....and I hope you guys can answer....Is there a way to make a selection (at the beginners level) without using control statements or loops. Say if user inputs 0 this happens....if user inputs 1 that happens. I am not looking for anyone to write code for me but I have been stuck on this for a while. I have tried using pointers and a few other things but my output always prints what is associated with 0 and 1. What should I research? Thanks ahead of time for your help.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    Quote Originally Posted by facebook1978200 View Post
    Is there a way to make a selection (at the beginners level) without using control statements or loops. Say if user inputs 0 this happens....if user inputs 1 that happens.
    The normal way is using 'if'.

    Code:
    if (input == 0)
       foo();
    else if (input == 1)
       bar();
    else
       printf("unexpected input\n");
    I would think 'if' is a control statement though. If you really insist on doing it "without control statements" then C has the conditional operator

    Code:
    printf("you got: %s\n", (input == 0 ? "zero" : input == 1 ? "one" : "(unknown)");

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    25
    Hey, facebook1978200.

    A great way to select an option based on an integer (0, 1, 2, etc) is using the "case switch." I'm not sure if this is exactly what you're looking for but it surely works like a charm in menu driven prompts and things of that nature.

    Lesson 5 in the c programming tutorial available on this website covers this topic quite well.

    I know that you said you didn't need us to write code for you, so I won't ruin the challenge for you if you want it that way. If you would like, however, I can provide some example code or more info. Just let me know!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,274
    Quote Originally Posted by facebook1978200
    Is there a way to make a selection (at the beginners level) without using control statements or loops. Say if user inputs 0 this happens....if user inputs 1 that happens.
    Is there any reason why you cannot use control statements?

    c99tutorial's conditional operator suggestion might work within the constraints of your curious question, but it can get a little hard to decipher when you start nesting them for many such conditions. Derek Lake's switch idea will work, but then it is also a "control statement".

    If the input in question are integers within a sufficiently small range, another method is to define functions to do each task, then store function pointers in an array. You then use the input to obtain the array element that is the function pointer, then call the given function. However, this might be a little beyond someone "at the beginners level".
    Derek Lake likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    5
    i am familiar with switch from reading a text....but using switch isnt an option....thx for the suggestion though

  6. #6
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    could the switch case statement be used?

    (3 mins too late lol)

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    I hope you know that "control statements" means a number of things and not just the "switch". Control statements are features which allow you to control the execution of statements, including: if, while, do while, for, switch, and even goto.

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    5
    Quote Originally Posted by laserlight View Post
    Is there any reason why you cannot use control statements?

    c99tutorial's conditional operator suggestion might work within the constraints of your curious question, but it can get a little hard to decipher when you start nesting them for many such conditions. Derek Lake's switch idea will work, but then it is also a "control statement".

    If the input in question are integers within a sufficiently small range, another method is to define functions to do each task, then store function pointers in an array. You then use the input to obtain the array element that is the function pointer, then call the given function. However, this might be a little beyond someone "at the beginners level".
    Head Spinning....thx for the reply I think c99tutorials answer will lead me to the right solution...I havent studied functions yet though....can you explain to me what the ("UNKNOWN") means?

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,501
    Perhaps it may be best to explain why you do not want to use control statements -> That way we can fully understand what you want to achieve.
    Fact - Beethoven wrote his first symphony in C

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    5
    Quote Originally Posted by whiteflags View Post
    I hope you know that "control statements" means a number of things and not just the "switch". Control statements are features which allow you to control the execution of statements, including: if, while, do while, for, switch, and even goto.
    yes I am aware..thx for your reply....

  11. #11
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,501
    Maybe an array of function pointers is what you are after

    User enters the array offset, and then the corresponding function is called.
    Fact - Beethoven wrote his first symphony in C

  12. #12
    Registered User
    Join Date
    Sep 2011
    Posts
    25
    Ah, good call laserlight. Come to think of it, case switch is just a specialized form of an if statement. Utilizing arrays by element number would control the flow of the code without using control statements.

    What kind of task is being performed depending on the selection in your case? With a minimal knowledge of arrays, and not knowing functions, you can still store things like strings, numbers, and other pieces of data in the elements of the array, and then calling the element of an array that your user has input into the prompt (you can also use command line arguments if you are familiar with these.)

    For instance, you could declare a new array of integers:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    int numbersToAdd[3];
    int number1,number2,garbage;
    numbersToAdd[0] = 25;
    numbersToAdd[1] = 30;
    numbersToAdd[2] = 50;
    printf("The numbers available for computation are: \n");
    printf("0 - %d\n", numbersToAdd[0]);
    printf("1 - %d\n", numbersToAdd[1]);
    printf("2 - %d\n", numbersToAdd[2]);
    printf("Please make your first selection by typing 0, 1, or 2 and then hitting enter.\n");
    scanf("%d",&number1);
    printf("Please make your second selection.\n");
    scanf("%d",&number2);
    printf("%d\n",numbersToAdd[number1]+numbersToAdd[number2]);
    scanf("%d",&garbage);
    return 0;
    }
    Last edited by Derek Lake; 01-13-2013 at 09:42 PM. Reason: Misplaced a printf

  13. #13
    Registered User
    Join Date
    Jan 2013
    Posts
    5
    Quote Originally Posted by Click_here View Post
    Perhaps it may be best to explain why you do not want to use control statements -> That way we can fully understand what you want to achieve.

    I have been trying my hand this long at my solution but I am coming up short...Here is what I am going for....The user gets prompted for a choice and will enter in a 0 or 1....entering a 0 will do an arithmetic operation and give an output and entering a 1 will do a different arithmetic operation giving a different output...the catch is no if, if else, switch, while, do while, or anything like that can be used.

  14. #14
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,501
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void invalid_in(void);
    void arith_1(void);
    void arith_2(void);
    
    int main(int argc, const char *argv[])
    {
        int user_in=0;
    
        void (*func_ptr_arr[3])(void) = {invalid_in, arith_1, arith_2};
    
        printf("Enter 1 for 1 + 1, or \nEnter 2 for 2 + 2\n");
        scanf("%d", &user_in);
    
        func_ptr_arr[user_in * ((unsigned int)user_in <= 2)]();
    
        return EXIT_SUCCESS;
    }
    
    void invalid_in(void)
    {
        puts("No Valid Input");
    }
    
    void arith_1(void)
    {
        printf("Result 1: %d", 1+1);
    }
    
    void arith_2(void)
    {
        printf("Result 2: %d", 2+2);
    }
    Fact - Beethoven wrote his first symphony in C

  15. #15
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    520
    Do'h I was gonna post the obligatory function pointers attached to array-indexes code... :P

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginners question on inline C
    By retrodans in forum C Programming
    Replies: 11
    Last Post: 03-30-2011, 07:36 AM
  2. C beginners question
    By incipientmind in forum C Programming
    Replies: 5
    Last Post: 02-25-2008, 02:06 PM
  3. beginners question
    By c++prog in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2008, 04:41 PM
  4. Beginners question
    By jamesstuart in forum C Programming
    Replies: 9
    Last Post: 09-24-2007, 04:24 PM
  5. Beginners OOP question
    By Skusey in forum C++ Programming
    Replies: 2
    Last Post: 11-06-2006, 06:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21