Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

input validation

Status
Not open for further replies.

HossTheGreat

Technical User
Nov 6, 2003
7
0
0
US
Ok, I've been working on a project. I'm supposed to be taking char input from a user, examining whether or not it is a valid integer and then converting it from char to int. I've got it working within the main function, however I need this to be a seperate function. I was given the following function prototype to use:

Use the following prototype for the function:
bool ReadInt(int *value);

Below is how I have it working thus far. I'm just a little stumped on the other part.

#include <iostream.h>
#include <stdlib.h>
#include <string.h>

int main ()
{
char userText[30];
int userValue;
bool bValid;
unsigned int i;

cout << &quot;Enter an integer:&quot;;

cin.getline(userText,30);

bValid = true;

// Check for minus sign
if (userText[0] == '-')
{
// There was one, there must be at least
// two characters, or it is invalid
if (strlen(userText) < 2)
{
bValid = false;
}
}
else
{
// No minus sign, let's make sure it starts
// with a digit
if ((userText[0] < '0') || (userText[0] > '9'))
{
bValid = false;
}
}

// Check all the other characters to make sure
// that they are all digits
for (i = 1; i < strlen(userText); i++)
{
if ((userText < '0') || (userText> '9'))
{
bValid = false;
}
}

// See if there were any errors
if (bValid)
{
// There were no errors
userValue = atoi(userText);
cout << &quot;You entered a valid value\n&quot;;
cout << userValue << endl;
}
else
{
// The string is invalid
cout << &quot;You entered an invalid value\n&quot;;
}

return 0;
}
 
hey there seems to be compilation problem... did ur code compile??

0)The file <iostream.h> cannot be opened
1)unidentified identifier bool
2)syntax error cout
etc etc
Is this a Valid C code ;-)

i guess its c++
 
I am not compiling it... may be you can compile and check. This will give you an idea of how code can be broken in to functions.

Hope this helps.

int main ()
{
int userValue;
char userText[30];

cout << &quot;Enter an integer:&quot;;

cin.getline(userText,30);
if(checkAndReturnInt(userText, &userValue)
{
cout << &quot;Value of Int&quot;<<userValue;
}
else
{
cout << &quot;No Valid Int Entered&quot;;
}

return 0;
}

bool checkAndReturnInt(char *userText, int *retVal)
{
int userValue;
bool bValid;
unsigned int i;

bValid = true;

// Check for minus sign
if (userText[0] == '-')
{
// There was one, there must be at least
// two characters, or it is invalid
if (strlen(userText) < 2)
{
bValid = false;
}
}
else
{
// No minus sign, let's make sure it starts
// with a digit
if ((userText[0] < '0') || (userText[0] > '9'))
{
bValid = false;
}
}

// Check all the other characters to make sure
// that they are all digits
for (i = 1; i < strlen(userText); i++)
{
if ((userText < '0') || (userText> '9'))
{
bValid = false;
}
}

// See if there were any errors
if (bValid)
{
// There were no errors
userValue = atoi(userText);
cout << &quot;You entered a valid value\n&quot;;
cout << userValue << endl;
// Return The value back
*retVal = userValue;

}
else
{
// The string is invalid
cout << &quot;You entered an invalid value\n&quot;;

}
return(bValid)
}
 
Your code is compiling and running well but as gasingh said you can easily put evrything in a function with the prototype your teacher was asking.
Paste all your existing code in the body
of the function bool ReadInt(int *userValue)
{
}
with the following modifs:
1. Remove int userValue;
2. Replace userValue = atoi(userText);
by *userValue = atoi(userText);
3. Replace return 0; by return bValid;
4. Get out all cout<< messages . These will be put in the caller program

Your code should look like here:

Code:
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
bool ReadInt(int *userValue)
{
    char userText[30];
    bool bValid;
    unsigned int i;

    cout << &quot;Enter an integer:&quot;;

    cin.getline(userText,30);

    bValid = true;

    // Check for minus sign
    if (userText[0] == '-')
    {
        // There was one, there must be at least
        // two characters, or it is invalid
        if (strlen(userText) < 2)
        {
            bValid = false;
        }
    }
    else
    {
        // No minus sign, let's make sure it starts
        // with a digit
        if ((userText[0] < '0') || (userText[0] > '9'))
        {
            bValid = false;
        }
    }

    // Check all the other characters to make sure
    // that they are all digits
    for (i = 1; i < strlen(userText); i++)
    {
        if ((userText [i]< '0') || (userText[i]> '9'))
        {
            bValid = false;
        }
    }

    // See if there were any errors
    if (bValid)
    {
        // There were no errors
        *userValue = atoi(userText);
      //  cout << &quot;You entered a valid value\n&quot;;
       // cout << userValue << endl;
    }
    else
    {
        // The string is invalid
      //  cout << &quot;You entered an invalid value\n&quot;;
    }
 
    return bValid;
}
int main ()
{
	int iVal = 0;
	bool b1 = ReadInt(&iVal);
    if (b1)
	{
	  cout << &quot;You entered a valid value\n&quot;;
       cout << iVal << endl;
	   }
	else
	{
		  cout << &quot;You entered an invalid value\n&quot;;
	}
	return 0;	
}

-obislavu-
 
Thank you guys very much. I'll go ahead and try it out tonight after work. I appreaciate your time.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top