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

#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!

Member Avatar for iamthwee

The only sure fire way to do this is expect the input as chars, do the validation from there. If it is good pass it on to the next stage which will convert it to float or double.

I recommend you read this:

http://www.daniweb.com/tutorials/tutorial45806.html

so store the data as chars
check if it says exit
check if it is a number
if it's a number convert to float
else display an error

Member Avatar for iamthwee

Yup,

Also, avoid using non-standard header like <conio.h> and non-standard functions like clrscr()

Ok

I thought I may have to do this

I have basicallysolved the first problem using some code i found

I now have this. I will try to get rid of the non standard code later, any suggestions what I could use to clear the screen?

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
   char temp[80];  //temp storage
	double input;  //users input
	char *endptr;
   int valid = 1;//after conversion
	float rate =1.89712; //for rate
	float result; //for output

	do
	{
		printf("Please enter an amount you wish to convert without the £.\n");
		fgets(temp, 80, stdin);  //get data
		input = strtod(temp, &endptr);

   if (endptr == temp)
   {
   clrscr();
			printf("You have entered incorrect data.  Please ensure you type numbers only!\n\n");
			valid = 0;
   }
   else
   {
   		valid = 1;
   }
   } while (!valid);  //data is valid
   	clrscr();
		result = input * rate; //do some maths
   	printf("£%.2f is equal to %.2f euros\n",input, result);//show output

}

Now I need to get the exit bit working. I know that when the user types exit, it would be stored in temp. when i try to use an if statement in line 19 like this

if (temp == "exit");
{
printf("exit");
}

I get an Lvalue required error

Any help would be appreciated, thanks

Member Avatar for iamthwee

You cannot compare string using the == operator in c.

Instead use strcmp (google if you must)

There is no standard way to clear the screen.

If you must use it then I guess clrscn() will do?

I have tried this before and had a few problems

The code I am trying is

if(strcmp(temp, "exit"));
   {
   printf("I would exit here!");
   }

it seems to give the error text whatever the user enters

Member Avatar for iamthwee

I have tried this before and had a few problems

The code I am trying is

if(strcmp(temp, "exit"));
   {
   printf("I would exit here!");
   }

it seems to give the error text whatever the user enters

Remove the semicolon from the if condition:

if(strcmp(temp, "exit"));

Just for clarity you might also consider adding

if(strcmp(temp, "exit")==0)


http://www.cppreference.com/stdstring/strcmp.html

without the semi colon, it doesn't do anything

Member Avatar for iamthwee

without the semi colon, it doesn't do anything

That mean's something else is wrong. (I.e with the rest of the code)

Trust me you don't need the semicolon at the end of the if statement.

Try putting a load of printf(s) in each body of the loops to find out what's going on.

Member Avatar for iamthwee

In fact, ask yourself what exactly are you doing here?

if (endptr == temp)

Also if you have changed your code, you might as well post the entire lot, because you could have changed something else which I wouldn't necessarily know about that is affecting other parts.

it just seems to bypass that code

the output it like this

Please enter an amount you wish to convert without the £.
exit_
You have entered incorrect data. Please ensure you type numbers only!
Please enter an amount you wish to convert without the £.
exit_
You have entered incorrect data. Please ensure you type numbers only!
1_
£1.00 is equal to 1.90 euros

Member Avatar for iamthwee

Ok so basically all you need is to get the program to exit when the user types in "exit".

Ask yourself why it might be bypassing it.

this is where i get confused see

i could be doing it all wrong or it could be because of my variable types

Member Avatar for iamthwee

this is where i get confused see

i could be doing it all wrong or it could be because of my variable types

Post ALL your code you have to date please.

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
   char temp[80];  //temp storage
	double input;  //users input
	char *endptr;
   int valid;//after conversion
	float rate =1.89712; //for rate
	float result; //for output

	do
	{
		printf("Please enter an amount you wish to convert without the £.\n");
		fgets(temp, 80, stdin);  //get data
		input = strtod(temp, &endptr);

   if(strcmp(endptr, "exit")==0)
   break;

   if (endptr == temp)
   {
   //clrscr();
			printf("You have entered incorrect data.  Please ensure you type numbers only!\n\n");
         printf("%s",endptr);
			valid = 0;
   }
   else
   {
   		valid = 1;
   }
   } while (!valid);  //data is valid
   	//clrscr();
		result = input * rate; //do some maths
   	printf("£%.2f is equal to %.2f euros\n",input, result);//show output

}
Member Avatar for iamthwee
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
   char temp[80];  //temp storage
	double input;  //users input
	char *endptr;
   int valid = 0;//after conversion
	float rate =1.89712; //for rate
	float result; //for output

	do
	{
		printf("Please enter an amount you wish to convert without the Pound\n");
		fgets(temp, 80, stdin);  //get data
		input = strtod(temp, &endptr);
		
		
     
      char *newline = strchr(temp, '\n'); /* search for newline character */
      if ( newline != NULL )
      {
         *newline = '\0'; /* overwrite trailing newline */
      }

      //printf("text = \"%s\"\n", temp);
   


   if(strcmp(temp, "exit")==0)
   {
                   printf("IT gets here");
   valid = 1;
   }
   //else if (endptr == temp)
   //{
   
     //    printf("You have entered incorrect data.  Please ensure you type numbers only!\n\n");
      //   printf("%s",endptr);
			//valid = 0;
  // }
  // result = input * rate; //do some maths
   	//printf("%.2f is equal to %.2f euros\n",input, result);//show output
   
   } while (valid == 0);  //data is valid
   	//clrscr();
		
  getchar();
}

This works if you type exit it would exit the loop.

Notice the important part in red which is discussed in that link I gave you before

Member Avatar for iamthwee

Your complete code would go something like this:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
   char temp[80];  //temp storage
	double input;  //users input
	char *endptr;
   int valid = 0;//after conversion
	float rate =1.89712; //for rate
	float result; //for output

	do
	{
		printf("Please enter an amount you wish to convert without the Pound\n");
		fgets(temp, 80, stdin);  //get data
		input = strtod(temp, &endptr);
		
		
     
      char *newline = strchr(temp, '\n'); /* search for newline character */
      if ( newline != NULL )
      {
         *newline = '\0'; /* overwrite trailing newline */
      }

     
   


   if(strcmp(temp, "exit")==0)
   {
                  
   valid = 1;
   }
   else if (endptr == temp)
   {
   
         printf("You have entered incorrect data.  Please ensure you type numbers only!\n\n");
         
   }
   else if (endptr != temp)
   {
   result = input * rate; //do some maths
   	printf("%.2f is equal to %.2f euros\n",input, result);//show output
    }
   } while (valid == 0);  //data is valid

}

Note the red parts

ah right

i was looking on that page and I didnt think of using that

thanks for your help

:D

fgets(temp, 80, stdin);  //get data
	input = strtod(temp, &endptr);
	
      char *newline = strchr(temp, '\n'); /* search for newline character */
      if ( newline != NULL )
      {
         *newline = '\0'; /* overwrite trailing newline */
      }

This works if you type exit it would exit the loop.

Yes this works, but it's very inefficient. As you say, wee, think about what is in temp and where it came from. Where could the '\n' be? Only at the end of the string. Why search?

fgets(temp, 80, stdin);  //get data
input = strtod(temp, &endptr);
length = strlen(temp) - 1; /* get the end of the string */
if ( temp[length] == '\n' )
{
    temp[length] == '\0';  /* overwrite trailing newline */
}

And in C, defining the variable in the middle of the code is not legal. This isn't C++ ya know... ;)


And kie, please learn to format your code. It will help immensely as your programs get larger.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.