?
Solved

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

Posted on 2003-02-25
2
Medium Priority
?
254 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
  • 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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

616 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