Help needed for validing input

This is a discussion on Help needed for validing input within the C Programming forums, part of the General Programming Boards category; Code: /* This source file is for current calculation using voltage/resistance. */ #include <stdio.h> #include <stdlib.h> #include <conio.h> //Function prototype ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    15

    Question Help needed for validing input

    Code:
    /*
    	This source file is for current calculation using
    	voltage/resistance. 
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    //Function prototype
    //char UserChoice(void);
    
    //char choice;
    
    void main(void)
    {
    	//Declaration
    	float volts, resistance, current;
    		
    	//do
    	//{
    		//Do loop to chk for invalid user input
    		do
    		{
    			//Clear the screen
    			system("cls");
    						
    			//Heading
    			printf("CALCULATE CURRENT USING VOLTAGE / RESISTANCE\n");
    			printf("==========================================\n");
    			
    			//Get input
    			printf("\nEnter voltage (in volts): ");
    			scanf("%f",&volts);
    			printf("Enter resistance (in ohms): ");
    			scanf("%f",&resistance);
    			
    			if(volts < 0)
    			{
    				printf("Invalid voltage! Please try again.\n");
    			}
    			
    			if (resistance <= 0)
    			{
    				printf("Invalid resistance! Please try again.\n");
    			}
    
    			if (volts < 0 || resistance <= 0)
    			{
    				getche();
    			}
    			
    		} while (volts < 0 || resistance <= 0);
    
    		//Calculation
    		current = volts/resistance;
    
    		//Output
    		printf("\nThe voltage is %.2f V.\n",volts);
    		printf("The resistance is %.2f %c.\n",resistance,234);
    		printf("The current is %.2f A.\n",current);
    	
    		//UserChoice();
    
    	//}while(choice == 'Y');
    	
    
    }


    1st time test input ==>100 for volts, p for resistance
    result===> get a error msg and loop back

    continue 2nd test input===>p for volts,100 for resistance
    result===>voltage 100,resistance 100.......

    Qns: How do i write a condition tat will filler out all input except intergers/floating numbers?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    int main(void) not void main(). Using system calls is not very smart and is somewhat risky.
    Woop?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > #include <conio.h>
    Try not to use non-standard headers.

    Read the FAQ.

    Read a line using fgets(), and check the return result of fgets()
    Scan that line for data which interests you using say sscanf() and check that return result as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  2. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 03:13 PM
  3. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 10:28 PM
  4. input validation - new to programming
    By bigzeppelin2k in forum C Programming
    Replies: 2
    Last Post: 10-31-2003, 05:44 PM
  5. input files in C
    By LWisne in forum C Programming
    Replies: 4
    Last Post: 09-30-2002, 06:24 AM

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