3 Replies - 5138 Views - Last Post: 26 October 2006 - 01:35 AM Rate Topic: -----

#1 dexterandnoodles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-October 06

help with validating character inputs

Posted 24 October 2006 - 12:05 PM

well i'm trying to write this program to take a character input, then it needs to verify that it is first a number, then that it doesnt have multiple decimals (i.e. 34.54.34), and that the negative sign is in the right place if any. After that it should print out twice the inputed value. I can get it to do the last part, but its not validating for whatever reason. If i put in a letter it just says the number was 0. I had it validating once before, but it wouldnt accept digits (would say invalid data, like it should if a letter were entered), only letters.

anyway here's the code any help is greatly appreciated

#include <iostream>
#include <cctype>

 

using namespace std;

 

int main()
{
	double myInt;
	char myStr[30];
	int i,len;
	bool validData;

	do
	{
		cout << "Enter a number: ";
		gets (myStr);
  
		validData = true;
		len = strlen(myStr);
		i = 0;
  
		while ((i < len) && (validData == true)) 
		{
			if (i == 0)
			{
				if (isdigit(myStr[30]))
				{
					validData = false;
				}
			}
			else 
			{
				validData = false;

			}
			i++;
		}

		if (validData == true)
		{
			myInt = atof(myStr);
			cout << "Twice the value entered is: " << myInt*2 << endl;
		}
		else
		{
			cout << "Incorrect Data. Try again!!! \n"; 
		}

	} while (validData == false);

 return 0;

}



Is This A Good Question/Topic? 0
  • +

Replies To: help with validating character inputs

#2 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: help with validating character inputs

Posted 25 October 2006 - 03:51 AM

View Postdexterandnoodles, on 24 Oct, 2006 - 12:05 PM, said:

well i'm trying to write this program to take a character input, then it needs to verify that it is first a number, then that it doesnt have multiple decimals (i.e. 34.54.34), and that the negative sign is in the right place if any. After that it should print out twice the inputed value. I can get it to do the last part, but its not validating for whatever reason. If i put in a letter it just says the number was 0. I had it validating once before, but it wouldnt accept digits (would say invalid data, like it should if a letter were entered), only letters.

anyway here's the code any help is greatly appreciated

Hi i have a little idea where to start...

The code is a little hard to read as you have a while inside a do/while.

basic code outline:
do
{
  while( validData=true && i < len)
  {
  }
}while(validData = false );


you can see each loop relies on the same variable and they have opposing values... if you enter an invalid value and i >= len the loop will exit and then the outer loop exits too.

This suggests that something is actually changing valid to false and then you must exit the loop before you complete validation and conversion.

There is one lien of concern.. if (isdigit(myStr[30]))<<-- something is not quite right here

looking at your if statement if (i == 0) when this is not true, you set validData to false... do I think you need to change the logic to:

for ( int i = 0; i < len; i++)
{
  if ( ! isDigit ( myStr[i] )
	  validData = false;
}


this may need a little refining, but should get you back on track. I have reversed the logic you used as validData suggests its false when the data is not valid...

This post has been edited by gregoryH: 25 October 2006 - 03:55 AM

Was This Post Helpful? 0
  • +
  • -

#3 dexterandnoodles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-October 06

Re: help with validating character inputs

Posted 25 October 2006 - 10:02 AM

hey man thanks i've actually done alot of revising... the only problem i now have is checking the number of decimals. the way my code works now is i can enter the decimal first and its all good, like .40 or .508098 but if the decimal comes up any other place it reads it as invalid.... heres my new coding and again any help would be awesome!!
#include <iostream>
#include <cctype>

 

using namespace std;

 

int main()
{
	double myNum;
	char myStr[30];
	int i,len;

	bool validData;

	
	do
	{
		cout << "Enter a number: ";
		cin.getline (myStr,30);
  
		validData = true;
		len = strlen(myStr);
		i = 0;
  
		while ((i < len) && (validData == true)) 
		{
			if (i == 0)
			{
				if (((myStr[i] < '0') || (myStr[i] > '9')) && (myStr[i] != '-') && (myStr[i] != '.'))
				{
					validData = false;
				}
			}
			else
			{
				if ((myStr[i] < '0') || (myStr[i] > '9'))
				{
					validData = false;
				}
				 
			}
			
			i++;




		}

		if (validData == true)
		{
			myNum = atof(myStr);
			cout << "Two times the value entered is: " << myNum*2 << endl;
		}
		else
		{
			cout << "Incorrect Data. Try again!!! \n";
		}

	   

	} while (validData == false);

 return 0;

}


Was This Post Helpful? 0
  • +
  • -

#4 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: help with validating character inputs

Posted 26 October 2006 - 01:35 AM

View Postdexterandnoodles, on 25 Oct, 2006 - 10:02 AM, said:

hey man thanks i've actually done alot of revising... the only problem i now have is checking the number of decimals. the way my code works now is i can enter the decimal first and its all good, like .40 or .508098 but if the decimal comes up any other place it reads it as invalid.... heres my new coding and again any help would be awesome!!
#include <iostream>
#include <cctype>

 

using namespace std;

 

int main()
{
	double myNum;
	char myStr[30];
	int i,len;

	bool validData;

	
	do
	{
		cout << "Enter a number: ";
		cin.getline (myStr,30);
  
		validData = true;
		len = strlen(myStr);
		i = 0;
  
		while ((i < len) && (validData == true)) 
		{
			if (i == 0)
			{
				if (((myStr[i] < '0') || (myStr[i] > '9')) && (myStr[i] != '-') && (myStr[i] != '.'))
				{
					validData = false;
				}
			}
			else
			{
				if ((myStr[i] < '0') || (myStr[i] > '9'))
				{
					validData = false;
				}
				 
			}
			
			i++;




		}

		if (validData == true)
		{
			myNum = atof(myStr);
			cout << "Two times the value entered is: " << myNum*2 << endl;
		}
		else
		{
			cout << "Incorrect Data. Try again!!! \n";
		}

	   

	} while (validData == false);

 return 0;

}


HI

Sometimes the way to validate is easier than it appears.

you can simplify the process by using cin>>val if the val is declared as a float.

If you must still validate.. then you must think carefully about the way to proceed.
  • try putting the validation into its own function
I say this because you really want to inspecte every array member before deciding the input is invalid.. by using its own routine, you can guarantee that you can scan each and set/reset a couple of flags.

I suggested something lastime but it went through to the keeper ...
int numDecimals = 0;
int numNotDigits = 0;

for ( int i = 0; i < strlen(str);i++)
  switch ( str[i])
{
  case '.': // you think what goes here;
	  break;
  default:
	if ( ! isDigit (str[i]  ) //and here
}


This is a very simple filter to only work for the decinal and then uses the macro isdigit to perfrom the rest of the work....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1