Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,395 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,118 people online right now. Registration is fast and FREE... Join Now!




help with validating character inputs

 
Reply to this topicStart new topic

help with validating character inputs

dexterandnoodles
24 Oct, 2006 - 11:05 AM
Post #1

New D.I.C Head
*

Joined: 24 Oct, 2006
Posts: 2


My Contributions
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

CODE
#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;

}

User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Help With Validating Character Inputs
25 Oct, 2006 - 02:51 AM
Post #2

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(dexterandnoodles @ 24 Oct, 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

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:
CODE
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:

CODE
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 Oct, 2006 - 02:55 AM
User is offlineProfile CardPM
+Quote Post

dexterandnoodles
RE: Help With Validating Character Inputs
25 Oct, 2006 - 09:02 AM
Post #3

New D.I.C Head
*

Joined: 24 Oct, 2006
Posts: 2


My Contributions
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!!
CODE

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

}

User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Help With Validating Character Inputs
26 Oct, 2006 - 12:35 AM
Post #4

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(dexterandnoodles @ 25 Oct, 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!!
CODE

#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 ...
CODE

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....
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:51AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month