1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Validating data

Discussion in 'C' started by kie, Nov 14, 2006.

  1. kie

    kie New Member

    Joined:
    Nov 14, 2006
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hey all

    Basically, I am trying to get my head around data validation. I am very new to C, in fact I have only been doing it a few days.

    I am trying to make a simple currency converter that validates that the data is numerical. Alos, I would like the app to exit if the user enters exit (in upper or lower case)

    This is what I have so far

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
    	float input; //for input
    	float rate =1.89712; //for rate
    	float result; //for output
    
       printf("Please enter the amount you want to convert in GBP. "); //Get users input
       scanf ("%f",&input);
    
       clrscr();
    	result = input * rate; //do some maths
       printf("£%.2f is equal to %.2f euros\n",input, result);//show output
    
    }
    
    it does what I want but as I said, I need it to check the user has entered correct data and if not, loop back so they can input again. Also, I need it to exit when the user enters enter.

    edit: oh yea, btw, I only want it to convert from one to another at the mo

    Thankyou!
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    You put
    Code:
     printf("Please enter the amount you want to convert in GBP. "); //Get users input
     scanf ("%f",&input);
    into a loop till you get if every input is correct. isdigit function will help you know if its a digit
     

Share This Page