Thread: Help with validation rule

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    14

    Help with validation rule

    Hi I am writing some code but need a validation in one part

    Code:
    for(int i=0; i < size; i++)
      {
      system("cls");
    cout <<"\nPlease enter the Cars registration: " << (i+1) << ": ";
    cin >> autos[i].carreg;
    cout <<"\nPlease enter the make of Car: ";
    cin >> autos[i].carmake;
     cout <<"\nPlease enter the Model of Car: ";
    cin >> autos[i].carmodel;
     cout <<"\nPlease enter the Cars milage: ";
    cin >> autos[i].carmile;
    cout <<"\nPlease enter the Cars age: ";
    cin >> autos[i].carage;
    cout<<"\n";
      }
    I need it so that you cannot enter a minus value for car age or milage, im sure i know this but my mind has gone totally blank.
    Thanks in advance for any help.
    snooki

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Without knowing the types of the variables (eg are they unsigned) the obvious response is to do a run time check on the value.
    Code:
    autos[i].carage = -1;
    while (autos[i].carage < 0)
    {
        cout <<"\nPlease enter the Cars age: ";
        cin >> autos[i].carage;
    
        if (autos[i].carage < 0) complain_bitterly();
    }
    Last edited by grumpy; 03-30-2010 at 11:38 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. set validation
    By jmarsh56 in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2005, 08:49 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Rule of 72
    By sonict in forum C++ Programming
    Replies: 12
    Last Post: 01-23-2003, 08:31 PM
  4. [C#] Validation class for events?
    By gicio in forum C# Programming
    Replies: 4
    Last Post: 01-03-2003, 12:19 PM
  5. Who should rule the world?
    By CoderBob in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 02-07-2002, 07:01 AM