×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

actual code keyboard error checking code 2 (to attention of 2ffat)

actual code keyboard error checking code 2 (to attention of 2ffat)

actual code keyboard error checking code 2 (to attention of 2ffat)

(OP)
Here is the actualcode which i am trying to develope


calculation()
{

    do
    {
     clrscr();

cout<<"Pleae enter a value for resistance"<<endl;
    cin>>R;

while (cin.fail())
     {
     cout<<"You have eneter a illegal value please try again"<<endl;
     cin>>R;
        }

cout<<"Pleae enter a value for capacitance"<<endl;
     cin>>C;

     while (cin.fail())
     {
     cout<<"You have eneter a illegal value please try again"<<endl;
     cin>>C;
        }

cout<<"Pleae enter a value for inductance"<<endl;
     cin>>L;

        while (cin.fail())
     {
     cout<<"You have eneter a illegal value please try again"<<endl;
     cin>>L;
        }



    cout<<"========================================================="<<endl;
    cout<<"You have entered the following values"<<endl;
    cout<<"Resistance (Ohms) = "<< R <<endl;
    cout<<"Inductance (Heneries) = "<< L <<endl;
    cout<<"Capacitance (Farads) = "<< C <<endl;
    cout<<"========================================================="<<endl;
    cout<<"The resonant frequency for a series RLC circuit with the"<<endl;
    W=1/(L*C);
    cout<<"above component values is W (rads) = "<<W;
    cout<<" frequency (Hz) = "<< W/(2*3.14)<<endl;

    cout <<"The circuit also has a qualtity factor Q ="<<endl;
    cin>>quit;
}while(quit=='Y');



    }

RE: actual code keyboard error checking code 2 (to attention of 2ffat)

cooldud69,

    I haven't forgotten you but I've been very busy at work. My suggestion is to collect your input into a string or character array then translate the string into a floating point number. The cin stream already does this when you input a string into a float. The problem with most of these schemes (e.g., atol) is that they simply stop translating when they encounter an alpha instead of a numeric.

    From your description, you want to know when they enter an invalid entry. My (not so eligant) suggestion is to use the ctype functions on each character in the string. By checking each character in the input, you can tell if the input is incorrect of not. For example,

string Input;
bool Check = true;
cin >> Input;

for (int x = 0; x < Input.length(); x++)
{
    char Z = Input[x];
    if (!isalnum(Z) && !ispunct(Z))
    {
        // Z is a control character
        Check = false;
    }
    else
    {
        if (!isnum(Z) && (Z != '.'))
        {
            // Character is not a number or period
            Check = false;
        }
    }
}
if (!Check)
{
    cout << "Not a valid number!!!!" << endl;
}


    Of course, this is off the top of my head so there probably errors in it but you should get an idea. If you use a character array, you may have to check for the null character instead of the length of the string. You best bet is to make this a function that you can call for each input.

    If you want me to post some actual code, let me know here and I'll see what I can do.

James P. Cottingham

International Veneer Co., Inc.
All opinions are mine alone and do not necessarily reflect those of my employer.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close