Thread: How to read single or multiple value from user without using array or loop?

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    16

    How to read single or multiple value from user without using array or loop?

    Hi guys, just wondering how can I read a user input without using loop or array method?

    I have a requirement which allowed user to enter a random combination with alphabet A, B and C.

    For example, user allowed to input A only, or A and B, or A, B and C and so on.

    How it work with
    Code:
    scanf()
    ?

    As I know if I am using
    Code:
    scanf("%c%c%c", &x,&y,&z)
    , it forced user to enter 3 times. How can I allowed user to enter freely between the 3 alphabet and apply condition accordingly?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    No arrays you say?

    So you can't even do this?
    Code:
    char buff[BUFSIZ];
    fgets(buff,BUFSIZ,stdin);
    At least you can then verify how many chars were entered, and whether they were the right ones.


    You could use scanf("%s",buff); but it's still an array.

    The problem with using scanf("%c") is that it will block if there is no input. So there's no way to input say one letter, and then test to see if there is a second letter.


    Or you could use something non-blocking, but dependent on your choice of operating system and compiler.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    16
    Quote Originally Posted by Salem View Post
    No arrays you say?

    So you can't even do this?
    Code:
    char buff[BUFSIZ];
    fgets(buff,BUFSIZ,stdin);
    At least you can then verify how many chars were entered, and whether they were the right ones.


    You could use scanf("%s",buff); but it's still an array.

    The problem with using scanf("%c") is that it will block if there is no input. So there's no way to input say one letter, and then test to see if there is a second letter.


    Or you could use something non-blocking, but dependent on your choice of operating system and compiler.
    So means, there's no way to achieve?

    I just give some basic overview. E.g.

    `Please enter F for Food, B for Beverage, D for dessert:`

    User can enter F and B, or any letter asked for and submit then go to nested if for condition.

    So there's no way to achieve if not using array to capture user entered?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If the user can enter "FB", then what (other than an array) would you store them in?

    Sure you can read "F" and then "B" (in either order), but that would mean a loop.

    Your requirements are so hopelessly constrained (don't use any obvious things) to the point that it seems more like an exercise to discover the tutor's stupid magic trick rather than teaching any useful programming skills.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2016
    Posts
    16
    Quote Originally Posted by Salem View Post
    If the user can enter "FB", then what (other than an array) would you store them in?

    Sure you can read "F" and then "B" (in either order), but that would mean a loop.

    Your requirements are so hopelessly constrained (don't use any obvious things) to the point that it seems more like an exercise to discover the tutor's stupid magic trick rather than teaching any useful programming skills.
    Yeah, that's true. Besides array and looping, I can't think of a way to make this magic happened. So just asking around see if make sense, haha.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-07-2020, 06:42 PM
  2. multiple condition for single variable in single line
    By merafiq in forum C Programming
    Replies: 2
    Last Post: 03-13-2016, 05:26 PM
  3. Replies: 21
    Last Post: 02-19-2015, 12:16 AM
  4. Replies: 3
    Last Post: 03-11-2011, 02:49 AM
  5. Single line user inputted string to Array?
    By Sparrowhawk in forum C Programming
    Replies: 11
    Last Post: 12-14-2008, 08:10 PM

Tags for this Thread