Thread: isdigit problem

  1. #1
    hurry up
    Guest

    Unhappy isdigit problem

    i want to get the no of rows for my matrix but 1st want to check whether the user has input an integer or not.if the user has input a non int then i ask the user again and again to enter the no of rows till he input integer.the code so for is;


    char row[5];

    printf("Enter the Number of rows : ");
    scanf("%s",&row);

    int counter=0;
    int counter1=0;

    while( counter < 5 )
    {
    if ( !isdigit( row[counter] ) )
    {
    printf("Please enter again :");
    scanf("%s",&row);
    counter1=1;
    }

    if(counter1==0)
    counter++;
    else
    {

    counter1=0;
    counter=0;
    }
    }

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Also, when posting code, use code-tags.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Try the FAQ
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    21
    your call to scanf should be
    Code:
    scanf("%s", row);
    no '&' is needed for a string.
    However what is there to stop the user from entering more than the allowed chars ie 4 + '\0'. Try using fgets if you want to put this number into a string but you will have to allow for the '\n' char that fgets grabs if the number entered is less than the maximum. Changing row to an int and using scanf as suggested in Salems post would be the simpler option, you wouldn't need isdigit then.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with isdigit()
    By Mastrchief in forum C++ Programming
    Replies: 5
    Last Post: 10-14-2007, 02:19 PM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM