Help with validation rule

This is a discussion on Help with validation rule within the C++ Programming forums, part of the General Programming Boards category; Hi I am writing some code but need a validation in one part Code: for(int i=0; i < size; i++) ...

  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,289
    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%.

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, 07: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, 07:31 PM
  4. [C#] Validation class for events?
    By gicio in forum C# Programming
    Replies: 4
    Last Post: 01-03-2003, 11:19 AM
  5. Who should rule the world?
    By CoderBob in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 02-07-2002, 06:01 AM

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