Thread: How do I check data entered and generate an error

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    5

    How do I check data entered and generate an error

    I'm writing a class assignment and we're supposed to check the validity of the data entered. Keeping it simple, I can write the number of francs, and do the loop if it's a negative number, but what if the user enters one or more alpha characters or just presses ENTER. How do I generate the error condition then? I've tried to figure out ISALPHA and ISDIGIT but it didn't work, or at least I'm not good enought to figure it out.

    Code:
    #include <stdio.h>
    #include <system.h>
    
    main()
    {
    /* 	
    	Author: Ken Buckner
    	Created May 11, 2004
    	Version 2.0
    */
    	/* Declare variables */
    	int error;
    	float conversion_rate, francs;
    
    	/* Initialize variables */
    	error = 0;
    	conversion_rate = 5.47396;
    	francs = 1.0;
    	/* Conversion rates downloaded on April 30, 2004 */	
    	
    		/* Output prompt */
    		printf("Currency Conversion.\n\n");
    		printf("Please enter the amount of French Francs you'd like to convert.\n\n");
    		
    		/* Start DO WHILE loop */	
    		do   
    		{	/* Reads user input */
    			scanf("%lf", &francs);
    			/* Checks user input, if not valid, sets ERROR to 1 (ON) */
    			if(error = (francs <= 0.0))
    			{
    				/* Clears the screen */
    				clrscr();
    				/* If Error prompt */
    				printf("Please enter a valid amount.\n\n");
    			}	
    		}
    		/* End DO WHILE loop, if not ERROR */
    		while (error);
    		
    		/* Clears the screen */
    		clrscr();
    		/* Output information, with calculation */
    		printf("%.3f French Francs = $%.2f U.S. Dollars.\n\n", francs, (francs / conversion_rate));
    		printf("Conversion rates accurate as of April 30, 2004.\n");
    		
    	return(0);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try the FAQ.

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

Popular pages Recent additions subscribe to a feed