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?