Thread: forcing user input

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Milwaukee
    Posts
    28

    forcing user input

    Hello,

    I have several input prompts in my code but the default of zero keeps making the code run right through the prompts. How do I clean out the input and make the user change the value so the program stops and waits for input?


    Code:
    int choice;          char Col_line[100];
               printf("How Much? \n");
                     
                fgets(Col_line, sizeof(Col_line), stdin);
                sscanf(Col_line, "%d", &choice);

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Post some more code that actually shows the problem. This small code snippet only asks the user for a single choice and does nothing with that choice.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Milwaukee
    Posts
    28
    Code:
    int choice;          char Col_line[100];
               printf("How Much? \n");
                      
                fgets(Col_line, sizeof(Col_line), stdin);
                sscanf(Col_line, "%d", &choice);
    
    
    
    
    if if(choice*price > *my_money_ptr)
                     {
                     printf("You don't have enough Money!\n");
                     printf("Please try again.....\n");
                     //goto start_buy;
                     }
                     else
                     {
                     *my_money_ptr = (*my_money_ptr - (choice * price));
                     *my_units_ptr = choice;
                     }
                     }

    The int is always zero and it never asks me to change it

    Thanks

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    First of all I don't see where choice is ever set to 0 (unless you mean the user enters 0).

    I still don't understand what the problem is. Do you want to force the user to enter a non-zero value? Or do you want to detect whether the user entered a valid number and ask them again if it's not?

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Milwaukee
    Posts
    28
    I need the program to stop and make the user input something. the default of zero is making it not prompt any input and running to the end

    Thank you!

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You need to learn how to ask questions the smart way, as per this link.

    Your problem description is unclear, the code you have shown is barely even relevant to what you have described, and you are not providing useful responses when people ask you to clarify. Being a forum member does not equip people to read your mind.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You mean that the program is not pausing for you to enter anything?

    I've seen some people put a space at the start or end of the format string to scanf to deal with that.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest posting code that can be compiled.

    Or, at least post a complete function.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Code:
                fgets(Col_line, sizeof(Col_line), stdin);
                sscanf(Col_line, "%d", &choice);
    Why not just:
    Code:
    if (scanf("%d", &choice) != 1) {
        /* error */
    }
    --

    Code:
    if if(choice*price > *my_money_ptr)
    You should post your full working code. We can decide what to ignore and what to look at, don't omit something because you don't think it's relevant. It also goes without saying that the code you post should be able to compile.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i need help with getting input from the user.
    By nyekknyakk in forum C Programming
    Replies: 7
    Last Post: 08-17-2010, 10:22 PM
  2. using user input as a var.
    By nubi in forum C++ Programming
    Replies: 13
    Last Post: 05-06-2003, 12:27 PM
  3. user input
    By unregisterd in forum C Programming
    Replies: 5
    Last Post: 12-30-2002, 05:22 AM
  4. user input
    By hen in forum C Programming
    Replies: 16
    Last Post: 06-27-2002, 11:09 PM
  5. User input?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-23-2001, 07:13 PM