Thread: Give Error Message If User Inputs Wrong

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    16

    Lightbulb Give Error Message If User Inputs Wrong

    Code:
    #include<stdio.h>
    void main 
    {
    int i;
    int marks[10];
    
    
    for( i=0; i<10; i++)
    {
    printf("\n %d",i+1);
    scanf("%d",&marks[1]
    }
    
    }
    how to give error and ask user to re enter if user enter other then a number?
    Last edited by Aliano; 11-02-2013 at 11:22 AM.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    You need to check the return code of scanf. It returns the number of items successfully read, or EOF if there was a problem. You can check the return and then use a loop, but if you do so you'll probably run into trouble due to the scanf operates. Basically the easiest way I find is the following

    1. read a line
    2. read the input from that line
    3. if there is a problem, print an error and go to 1.

    Here is an example

    Code:
    #define SSIZE (10000)
    char s[SSIZE];
    
    int main()
    {
        int mark;
        while ((fgets(s, SSIZE, stdin)) != NULL) {
            if ((sscanf(s, "%d", &mark) != 1)) {
                printf("That is not a number. Try again.\n");
                continue;
            }
            printf("Got a number: %d\n", mark);
            break;
        }
        
        return 0;
    }

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You need to check the returned value of scanf, it normally returns the amount of successful inputs.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Sep 2013
    Posts
    16
    Code:
    void main
    {
    int number;
    char quit
    do
    {
    printf("Enter number:\n");
    if(scant("%d",&number) !=1 && scanf("%c", &quit))
    {
    printf("Invalid\n");
    printf("Enter number:\n");
    fflush(stdin);
    }
    
    }while(quit !='q' && quit !='Q');
    can i make like this? if user input 'Q' or 'q' end the loop! if user input other the a number except Q or q Print INVALID

  5. #5
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Looks like somebody needs a diaper change!
    i dont believe in competition in da field of cboard posts, u see, i believe in a collection of posts each 2 be admired for der own personal statement. when in doubt, ask Willy!

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It looks like you have several problems, mostly having to do with a lack of understanding of the language.

    First, you should be trying to compile and execute the code you write before posting it here. With respect to post #4, it's clear this was not done. You're missing a semi-colon after declaring "quit" and you wrote "scant" instead of "scanf". These are basic errors that, while permissible to make in the course of writing code, should easily be detected and corrected before posting.

    Secondly, you're trying to cram too much functionality into your code. I can virtually guarantee that your "if()" statement isn't doing what you think it is doing.

    Based on these observations, I feel it's a safe assumption that you're banging out code with little thought and wondering why it isn't working. You'll never get a decent program written that way.

    Writing a program should not begin with code. It should begin with a clarification of requirements and planning on how to achieve those requirements.

    Step 1: Write down, in words, what your program needs to do. Based on what you've posted, I would say it's something along the lines of: "Allow the user to enter up to ten numbers, and store them in an array. If the user does not enter a valid number, print an "invalid" message and ask again. The user can enter a specific ("sentinel") value to indicate they are finished." If you do not have clear instructions yourself for what you're trying to accomplish, you will not be able to program a computer to perform that task.

    Step 2: Plan your program. You will need an array to store the input. You will need a loop that will keep asking for numbers until ten numbers have been entered, or the "sentinel" value has been entered (indicating the user is done entering numbers). If the user enters an invalid value, you do not want that loop to continue until they enter a valid number, so it looks like you will need a validation loop within your main loop.

    Also note that trying to read for integers, and a character ('Q' or 'q') is, while possible, probably more difficult than you're ready to handle right now. Find a way to make this simpler. For instance, your array is called "marks", so if we assume that you're reading grades, it's probably a safe bet that negative numbers are not to be expected. Therefore, you can probably make your sentinel value -1 (if the user enters this number, it signifies they are finished entering values).

    Step 3: Now that you have the basic requirements and a rough plan, you can start to design your logic (you still shouldn't be coding yet at this point). Write down, on paper, a step by step algorithm for how to accomplish your task. Based on this, you can create a flow chart or pseudo-code for the logic of your program.

    Step 4: Now that you have a step by step approach planned out, you can begin coding. Don't bang out the code all at once - break it down into steps and just do one step at a time. (See "A Development Process" for a full example.) Compile and test often, each step of the way.

    For instance:

    1. Run a loop to read inputs from the user, store them in the array, the print out the array. Compile, test, and verify your results.
    2. Add code to handle the sentinel value; i.e. break out of the loop early if the user enters the predetermined value. Compile, test, and verify your results.
    3. Add code to validate each user input, and print an error message if an input is invalid. Compile, test, and verify your results.
    4. Add code to re-prompt the user for an input if the input was invalid. Compile, test, and verify your results.

    Approach your program with an informed, structural mindset and the whole process will go a lot smoother.

    Also, your main function should be like this:
    Code:
    int main(void)
    {
        // ...
    
        return 0;
    }
    FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com
    Last edited by Matticus; 11-04-2013 at 11:51 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Give More Inputs to My C Program
    By NBK Rules in forum C Programming
    Replies: 7
    Last Post: 03-27-2013, 01:06 PM
  2. Replies: 3
    Last Post: 05-11-2011, 02:53 PM
  3. How to handle inputs if the user inputs the wrong thing
    By bassist11 in forum C Programming
    Replies: 5
    Last Post: 09-22-2010, 04:28 AM
  4. How to reprompt after user inputs wrong number?
    By gnozahs in forum C Programming
    Replies: 11
    Last Post: 10-03-2009, 03:49 PM
  5. Replies: 1
    Last Post: 05-19-2006, 08:30 PM