Thread: Program thrown into loop when character entered instead of flaot

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    41
    isdigit()

    i.e if(isdigit(variable))
    {
    do this
    }
    else
    getchar();
    Last edited by strickyc; 07-04-2009 at 07:49 PM.

  2. #2
    Registered User
    Join Date
    Jul 2009
    Posts
    8
    Thanks,

    When I woke up this morning, thats what I was thinking (isdigit). Then I logged on here and saw your comment, I entered it and it works well. Thanks.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    8
    Can you impose a time limit on a command like getchar() ???

    I got the program to stop and not loop if the user enters a character rather than a number, but now the program stops and waits for the user to hit a key when they enter a number.
    Code:
    //   Enter purchase amount and verify purchase amount
    	do { // do-while loop start
    		printf("\n\nEnter the total purchase amount: $");
    		scanf("%f", &fPurchase_Amount);
    	if (fPurchase_Amount <= 0.0)
    		printf("\nYou did not enter a valid number, please try again");
    	if (isdigit(fPurchase_Amount));
    	else
    		getchar();
    	} //end do-while loop	
    	while (fPurchase_Amount <= 0.0);

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by alekbabich View Post
    Can you impose a time limit on a command like getchar() ???

    I got the program to stop and not loop if the user enters a character rather than a number, but now the program stops and waits for the user to hit a key when they enter a number.
    ...
    One way is to schedule an alarm signal (SIGALRM) to be sent to the process after a number of seconds have elapsed, given as an argument to alarm().
    Write a signal handler that sets a flag upon receipt of SIGALRM; check the value of flag in the caller to decide whether to continue or exit the program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C keep a loop going whilst program continues on
    By fortune2k in forum C Programming
    Replies: 6
    Last Post: 03-11-2009, 08:44 AM
  2. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  3. Using While Loop to write a program
    By Cyberman86 in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2008, 12:52 PM
  4. Unexplained "unhandled exception"
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 04-19-2007, 11:19 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM