Thread: error handling question

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    1

    error handling question(code in post)

    below is a exerpt of what I have for them to enter the input for the matrix. I am attempting to be able to stop the error, when the person enters a number it works, but when they enter a chara it spams out text till the end not allowing anymore user input. Is there any script of code I could use to check each one before it caused a problem or maybe not use floating as my input. Not sure if this is to broad but thanks for the help if you can.


    printf("Please start entering the terms of the first matrix below.\n");

    printf("Matrix 1 row 1 colum 1: ");
    scanf( "%f", &array_a[0][0]);
    printf("Matrix 1 row 1 colum 2: ");
    scanf( "%f", &array_a[0][1]);
    printf("Matrix 1 row 1 colum 3: ");
    scanf( "%f", &array_a[0][2]);
    printf("Matrix 1 row 2 colum 1: ");
    scanf( "%f", &array_a[1][0]);
    printf("Matrix 1 row 2 colum 2: ");
    scanf( "%f", &array_a[1][1]);
    printf("Matrix 1 row 2 colum 3: ");
    scanf( "%f", &array_a[1][2]);
    printf("Matrix 1 row 3 colum 1: ");
    scanf( "%f", &array_a[2][0]);
    printf("Matrix 1 row 3 colum 2: ");
    scanf( "%f", &array_a[2][1]);
    printf("Matrix 1 row 3 colum 3: ");
    scanf( "%f", &array_a[2][2]);
    Last edited by plexis; 09-30-2002 at 08:49 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's because you're using scanf, which leaves the newline in the buffer. You need something like:
    Code:
    void clearstdin( void )
    {
        while(fgetc(stdin)!='\n');
    }
    This basicly will pull all input upto and including the newline from the buffer. So after you use scanf, do:

    scanf( "%d", &myvar );
    clearstdin( );

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Why don't you use loops to enter the values into the array?
    It would make your code alot shorter.

    Mr. c.

  4. #4
    Registered User Seek n Destroy's Avatar
    Join Date
    Sep 2002
    Posts
    9
    I think you could use...
    scanf("%[1234567890]f",&store);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM