validation nightmare

This is a discussion on validation nightmare within the C++ Programming forums, part of the General Programming Boards category; im working on a calculator and im having difficulty working out how to test the user entered number ive done ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    4

    Exclamation validation nightmare

    im working on a calculator and im having difficulty working
    out how to test the user entered number ive done it before but
    ive forgot how to do it the easy way, ive tried loads of
    difficult ways and none work on my compiler or when integrated
    into my program any help would be gratefully recieved. i need to loop until the correct input has been entered by the user

    // scientific calculator

    #include <iostream.h> // inut and output
    #include <ctype.h> // the header file allows the toupper function
    #include <windows.h> // allows windows system functions system("CLS")
    #include <math.h> // allows the sqrt sin, cos, log, tan, alog, and acos math functions

    // slimmed down version of the code im working on

    double getNumber() // cant sus out how to validate user input
    // within this function tried the cin.good method and failed
    // to get it working within my code


    {
    double b;

    cout <<"Please enter a number : ";
    cin >>b;

    return b;
    }

    double multiply(double* x, double* y)
    {

    return *x * *y;

    }
    int main()
    {
    double* x;
    double* y;
    double z;
    char s;
    double num1, num2;

    cout <<" Enter * to multiply "<<"\n";
    cout <<"Please enter your option : ";
    cin >> s;

    system("CLS");
    s = toupper(s);
    switch (s)
    {
    case '*' :

    cout <<"********************************"<<"\n";
    cout <<"* You opted to multiply *" <<"\n";
    cout <<"********************************" <<"\n";
    x = &num1;
    y = &num2;
    *x = getNumber();
    *y = getNumber();
    z = multiply(x, y);

    cout <<*x << " * " <<*y << " = " << z << "\n";
    cout <<"The pointer are : " << &num1 <<" and " << &num2 <<"\n";

    break;
    default:
    cout <<"***** Do you wish to perform a calculation ***** " <<"\n";
    }
    return 0;
    }


    // i tried the following method but it wont work on my compiler
    // it should work

    #include <iostream>
    #include <cstdlib> // system call
    #include <ctype> // isdigit()
    #include <cmath> // atof (ASCII to float (double) )
    #include <iomanip> // setprecision()

    inline void pause() { system("PAUSE"); }

    int main(void)
    {
    char* input = " ";
    double numVal;
    bool valid = true;

    std::cout << "Enter a numeric value: -> ";
    std::cin >> input;

    for ( unsigned int i = 0; i < strlen(input); i++ ){

    if ( !isdigit(input[i]) && input[i] != '.'){

    std::cerr << "\nError! " << "'" << input[i] << "'"
    << " is an invalid character.\n\n";
    valid = false;
    }
    }

    if ( valid ){
    numVal = atof(input);
    std::cout << setiosflags(std::ios::fixed)<< std::setprecision(2)
    << "\nnumVal = " << numVal << "\n\n";
    }

    pause();
    return 0;
    }
    Last edited by andrew_lillis; 10-27-2002 at 08:49 AM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    There are plenty of ways, here's one:
    Code:
    #include <iostream>
    #include <cstdlib>
    
    double get_dbl()
    {
      double ret;
    
      std::cin>> ret;
    
      if ( !std::cin )
        throw 1;
    
      return ret;
    }
    
    int main()
    {
      double d;
      
      try {
        d = get_dbl();
    
        std::cout<< d <<std::endl;
      }
      catch ( int code ) {
        std::cerr<<"error: "<< code <<" -- Invalid number\n";
        exit ( EXIT_FAILURE );
      }
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    13
    Try: ( additional std:: before setiosflags() )
    Code:
    if ( valid ){
    numVal = atof(input);
    std::cout << std::setiosflags(std::ios::fixed)<< std::setprecision(2)
    << "\nnumVal = " << numVal << "\n\n";
    }
    That worked on my sys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 05-10-2009, 09:51 AM
  2. set validation
    By jmarsh56 in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2005, 08:49 AM
  3. DATE <TIME.H> data validation
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 08-07-2005, 02:10 AM
  4. validation routine help plz
    By rajesh23 in forum C Programming
    Replies: 8
    Last Post: 09-23-2003, 08:21 AM
  5. [C#] Validation class for events?
    By gicio in forum C# Programming
    Replies: 4
    Last Post: 01-03-2003, 12:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21