Numeral verification

This is a discussion on Numeral verification within the C Programming forums, part of the General Programming Boards category; Hi everyone! We were assigned a project not too long ago that asks us to ask a user to input ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    3

    Numeral verification

    Hi everyone!

    We were assigned a project not too long ago that asks us to ask a user to input numbers into a matrix. The user also chooses its dimensions.

    My questions is how would I incorporate a validation for the dimensions. Obviously, the only valid dimensions are numerical so I want an error to display if the user types a letter or a special character such as apostrophe or backslash.

    My thoughts:

    I was thinking that I could have it check the ASCII numerical value and limit those that range from 48 - 57 (numbers 0 - 9) but I cannot seem to get this to work. I would want the number that the ASCII code refers to be used as the dimensions not (48 - 57). Maybe there is an easier way to do this?

    Code:
    int main(void)
    {
    //Declare necessary variables
    int rowSize = 0;
    int columnSize = 0;
    
    do
    {     //Beginning of column while loop
    
    printf("Please enter the number of columns: "); scanf("%d", &columnSize); //Verify column size if (columnSize > 10)
    printf("ERROR: The maximum number of columns is 10.\n");
    else if(columnSize < 0)
    printf("ERROR: Invalid number of columns. Must be greater than 0.\n");
    } while (columnSize > 10 || columnSize < 0); ... Same for the row of the matrix.
    Thanks for any and all help!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You need to check the return value of scanf().... You can look it up in your C Library documentation.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    3
    Hi Tater,

    I'm not sure how to check the return value because I'm using Putty, a remote logon unix program. Any suggestions? Maybe you or someone else knows how to do that. I'm somewhat new to programming. I have a basic understanding but I would not call myself proficient or knowledgeable about C.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by ButtahNBred View Post
    I'm not sure how to check the return value because I'm using Putty, a remote logon unix program. Any suggestions?
    You do it the same way you wrote the rest of the code; With a text editor

    scanf(), fscanf()
    Code:
    int returnvalue = scanf(...);

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by ButtahNBred View Post
    I'm not sure how to check the return value because I'm using Putty,
    That ... is one of the best non sequiturs ever. You use the return value of scanf just like you would use the return value of any other function, like sqrt:
    Code:
    foo = sqrt(9);
    foo = scanf(...);
    Alternatively, you can use getchar + isdigit.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    3
    hahaha. Well, I'm dumb. I know what a return value is.

    Thanks for the help. Hopefully, I will be able to make some more progress...

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ButtahNBred View Post
    Hi Tater,

    I'm not sure how to check the return value because I'm using Putty, a remote logon unix program. Any suggestions? Maybe you or someone else knows how to do that. I'm somewhat new to programming. I have a basic understanding but I would not call myself proficient or knowledgeable about C.
    Look up scanf() in your C library documentation... You check the value in your code... it's got nothing to do with how you're connected.
    Last edited by CommonTater; 09-03-2011 at 01:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Roman Numeral Counter
    By luigiramez12 in forum C Programming
    Replies: 11
    Last Post: 03-30-2011, 03:12 PM
  2. Roman Numeral troubles....
    By h4rrison.james in forum C Programming
    Replies: 16
    Last Post: 01-15-2009, 09:26 PM
  3. Roman Numeral^^
    By only1st in forum C++ Programming
    Replies: 6
    Last Post: 04-19-2007, 11:58 AM
  4. Roman Numeral...T_T
    By only1st in forum C++ Programming
    Replies: 12
    Last Post: 04-12-2007, 11:06 AM
  5. Roman Numeral Convertor
    By mike1000 in forum C Programming
    Replies: 2
    Last Post: 11-24-2005, 02:53 PM

Tags for this Thread


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