?
Solved

VALIDATING DATA USING GETCH() TO ONLY ALLOW DIGITS, "-" & "."

Posted on 2003-02-25
2
Medium Priority
?
246 Views
Last Modified: 2006-11-17
How do I use getch() to input numbers.  I want to validate the data to only allow for '-' sign for a negative number and '.' for a decimal.  There can only be one of each.  I also want to make it so that if the person uses the backspace and retypes something that data will be recognized.  Anything else I want to not allow typed.  Thanks in advance.  Also if you see anything crazy in my code you want to comment on feel free.  I am a newbie c++ programmer but validating data seems to be very hard for me to accomplish.  





MY CODE!




#include <string.h>
#include <iostream.h>
#include <cctype.h>
#include <cstdlib.h>
#include <conio.h>
#include <math.h>
#include "calculatorU.h"

int main()
{
        operationCalculator();
        getch();
        return 0;
}

void operationCalculator()
{


        float verifiedFirstParameter;
        float verifiedSecondParameter;

        verifiedFirstParameter = verifyFirstParameter();
        verifiedSecondParameter = verifySecondParameter();
        operations(verifiedFirstParameter, verifiedSecondParameter);
        calculateAgain();
        return;
}

float verifyFirstParameter()
{
        int bad;
        string verifyFirstParameterString;
        char negative;
        float negativeNumber;
        float negativeConversion;


        cout << "Please insert your first parameter: ";

        do
        {
                bad = 0;
                int decimal = 0;
                verifyFirstParameterString ="";

                cin >> verifyFirstParameterString;

                for (unsigned int verifyInputCounter = 0; verifyInputCounter < verifyFirstParameterString.length() ; verifyInputCounter++)
                {
                        char verifyFirstParameterCharacter = verifyFirstParameterString[verifyInputCounter];

                        if (isdigit(verifyFirstParameterCharacter) || verifyFirstParameterCharacter == '.')
                        {
                                if (verifyFirstParameterCharacter == '.')
                                        decimal++;
                        }
                                else
                                        bad++;

                        if (decimal > 1)
                                        bad++;
                }

                if (bad)
                  {
                  clrscr();
                  cout << "You may only enter a positive number with an optional decimal place.\n";
                  cout << "You will have the opportunity to make this number negative later.\n";
                  cout << "\n";
                  operationCalculator();
                  }

        } while(bad);


        do
        {
        cout << "\nTo make the first parameter a negative number type '-' or type 'c' to continue: ";

        negative = getch();



        switch(negative)
                {
                        case '-':
                                negativeNumber = (atof(verifyFirstParameterString.c_str()));
                                negativeConversion = negativeNumber * -1;
                                return (negativeConversion);

                        case 'c':
                                return (atof(verifyFirstParameterString.c_str()));;

                        default:
                                clrscr();
                                cout << "Please enter only '-' or 'c'. \n";


                }
        }while(true);





}

float verifySecondParameter()
{
        int bad;
        string verifySecondParameterString;
        char negative;
        float negativeNumber;
        float negativeConversion;

        cout << "\nPlease insert your second Parameter: ";

        do
        {
                bad = 0;
                int decimal = 0;
                verifySecondParameterString ="";

                cin >> verifySecondParameterString;

                for (unsigned int verifyInputCounter = 0; verifyInputCounter < verifySecondParameterString.length() ; verifyInputCounter++)
                {
                        char verifySecondParameterCharacter = verifySecondParameterString[verifyInputCounter];

                        if (isdigit(verifySecondParameterCharacter) || verifySecondParameterCharacter == '.' )
                        {
                                if (verifySecondParameterCharacter == '.')
                                        decimal++;
                        }
                                else
                                        bad++;

                        if (decimal > 1)
                                        bad++;
                }

                if (bad)
                  {
                  clrscr();
                  cout << "Please reenter your data using only numbers and only one decimal place.\n";
                  cout << "\n";
                  operationCalculator();
                  }

        } while(bad);

        do
        {
        cout << "\nTo make the second parameter a negative number type '-' or type 'c' to continue: ";

        negative = getch();


        switch(negative)
                {
                        case '-':
                                negativeNumber = (atof(verifySecondParameterString.c_str()));
                                negativeConversion = negativeNumber * -1;
                                return (negativeConversion);

                        case 'c':
                                return (atof(verifySecondParameterString.c_str()));;

                        default:
                                clrscr();
                                cout << "Please enter only '-' or 'c'. \n";


                }
        }while(true);

}

void operations(float verifiedFirstParameter, float verifiedSecondParameter)
{

        char operation;
        float sum = verifiedFirstParameter + verifiedSecondParameter;
        float difference = verifiedFirstParameter + verifiedSecondParameter;
        float product = verifiedFirstParameter * verifiedSecondParameter;
        float quotient = verifiedFirstParameter / verifiedSecondParameter;
        float remainder = (int)verifiedFirstParameter % (int)verifiedSecondParameter;
        float power = pow(verifiedFirstParameter * (verifiedSecondParameter * verifiedFirstParameter));

        do
        {
        cout << "\nPlease choose one of the following operations: \n";
        cout << "\n\tPress '+' to add the two parameters.\n";
        cout << "\n\tPress '-' to subtract the two parameters.\n";
        cout << "\n\tPress '*' to multiply the two parameters.\n";
        cout << "\n\tPress '/' to divide the two parameters.\n";
        cout << "\n\tPress '%' to find the remainder of the two parameters.\n";
        cout << "\n\tPress '~' to calculate the power of the two parameters.\n";
        cout << "\nEnter your choice now: ";


                operation = getch();

                switch(operation)
                {
                        case '+':
                                cout << "\n" << "\n" << verifiedFirstParameter << " + " << verifiedSecondParameter << " = " << sum << endl;
                                return;
                        case '-':
                                cout << "\n" << "\n" << verifiedFirstParameter << " - " << verifiedSecondParameter << " = " << difference << endl;
                                return;
                        case '*':
                                cout << "\n" << "\n" << verifiedFirstParameter << " * " << verifiedSecondParameter << " = " << product << endl;
                                return;
                        case '/':
                                cout << "\n" << "\n" << verifiedFirstParameter << " / " << verifiedSecondParameter << " = " << quotient << endl;
                                return;
                        case '%':
                                cout << "\n" << "\n" << verifiedFirstParameter << " % " << verifiedSecondParameter << " = " << remainder << endl;
                                return;
                        case '~':
                                cout << "\n" << "\n" << verifiedFirstParameter << " ~ " << verifiedSecondParameter << " = " << power << endl;
                                return;
                        default:
                                clrscr();
                                cout << "\nPlease enter a valid character as listed below. \n";

                }
        }while(true);
}

void calculateAgain()
{
        string yesOrNo;

        do
        {
                cout << "\nWould you like to do another calculation? ";

                cin >> yesOrNo;

                if (yesOrNo == "yes")
                {
                       clrscr();
                       operationCalculator();
                }
                        if (yesOrNo == "no")
                                exit(0);

                else
                        clrscr();
                        cout << "Please choose either 'yes' or 'no'. \n";
        }while(true);

}




0
Comment
Question by:nalvoz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 8

Expert Comment

by:Exceter
ID: 8022594
Try this,

char c;
float f;
char number[30];
int t = 0;

cout << "Enter a number using the characters 0 through 9 . and - " << endl;

while(1)
{
     c = getch();

     if( c - 48  > -1 && c -48 < 10  || c == 45 || c == 46 )
     {
          number[t] = c;
          cout << c;
          t++;
     }

     if( c == 8 )
     {
          cout << (char)8 << " " << (char)8;
          number[t] = '\0';
          t--;
     }

     if( c == 13 )
          break;
}

number[t] = '\0';

f = atof( number );

cout << endl <<  f << endl;

Exceter
0
 
LVL 8

Accepted Solution

by:
Exceter earned 320 total points
ID: 8030028
nalvoz?
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question