Thread: Validate data on input

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    23

    Validate data on input

    Hi folks,

    Nice to be on this forum.

    I'm kinda new to C++ and have been working my way through a few on-line tutorials and have bought myself an idiots guide book but have come to a halt with my little program !

    I've got a car database program that I've been working on and it was working fine until i decided to do a spot of validation on entry. When a user inputs a car registration number I want it only to be able to take 5 charcters and if it is any more or any less, to print a error message to the screen and then give the oppertunity to input the registration number again. What's the best way to go about this ? I tried a few things but the keep coming back with errors !

    Any help on this to point me in the right direction would be much appreciated !

    H_M

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You can do something like this:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	
    	string input;
    	bool goodInput;
    	
    	do
    	{
    		goodInput = true;
    		
    		cout<<"Enter exactly 5 characters: ";
    		getline(cin, input);
    		
    		if(input.length() != 5)
    		{
    			cout<<"Idiot!"<<endl;
    			goodInput = false;
    		}
    
    	}while(goodInput == false);
    	
    	return 0;
    }
    Last edited by 7stud; 03-08-2006 at 06:14 AM.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    ....or this:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	
    	string input;
    		
    	while(true)  //infinite loop
    	{
    		cout<<"Enter exactly 5 characters: ";
    		getline(cin, input);
    		
    		if(input.length() != 5)
    		{
    			cout<<"Idiot!"<<endl;
    		}
    		else
    		{
    			break;
    		}
    	}
    	
    	return 0;
    }
    Last edited by 7stud; 03-08-2006 at 06:15 AM.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    7stud, thank you for such a speedy reply - much appreciated !

    I decided on using your second post as a guide, as that looked like the easiest for me to use, and I amended it slightly to fit my little program, but, i still can't get it to work ! It still allows me to enter less than or more than 5 characters ! This is my little part that i am having trouble with:
    Code:
    // Create a new car record in the database
    void add_car(car_type cars[])
    {
    	int no_of_cars, next_record_no;
    	int length;
    	length = 5;
     
    	// Display heading
    	clear_screen();
    	cout << "                          ADD A CAR"
    		 << endl << endl << endl;
    
    	// Count how many cars are already in the database
    	no_of_cars = car_count(cars);
    
    	// Report to user
    	cout << endl << "There are " << no_of_cars << " cars in CarBase."
    		 << endl;
    
    	// Set the record number at which to add the new car
    	next_record_no = no_of_cars;
    
    	// If database is not full, add a car
    	if (no_of_cars < MAX_CARS)
    	{
    	    cout << endl << "Make:    " << flush;
    		gets(cars[next_record_no].make);
    		cout << "Model:   " << flush;
    		gets(cars[next_record_no].model);
    
    		//Do data Validation for registration number
    				
    		while(true)
    		{
    			cout << "Reg No:  " << flush;
    			gets(cars[next_record_no].regno);
    
    		if((cars[next_record_no].regno, length) != 5)
    		{
    			cout<<"The registration number must equal 5 characters. Please enter it again. " << endl;
    		}
    		else
    		{
    			break;
    		}
    		
    	}
    
    		cout << "Year:    ";
    		cin  >> cars[next_record_no].year;
    		cout << "Price:   ";
    		cin  >> cars[next_record_no].price;
    		cout << "Mileage: ";
    		cin  >> cars[next_record_no].mileage;
    		cout << "Engine size in cc:   ";
    		cin  >> cars[next_record_no].engine;
    	}
    	// If database is full, give message
    	else
    	{
    		cout << "No room for any more cars\a" << endl;
    	}
     
    	// Database has changed so save it
    	save_data(cars);
    }
    Can you see where I have went wrong !? It's where it starts with the comments "//Do data Validation for registration number". Just thought I would post all the code that I have for that prototype incase it helps ? Once I manage to get the registration number one working, I am going to do the rest, hopefully !

    Any pushes in the right direction would be greatly appreciated !

    H_M

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    if((cars[next_record_no].regno, length) != 5)
    Compare that to my if statement.

    Also, what type is regno? If it's not a string type, it doesn't have a length() method. If it's a char array, you have to use strlen() to get the length.
    Last edited by 7stud; 03-08-2006 at 10:22 AM.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    Hi 7stud,

    Once again, thank you very much for your reply and help on this.

    I've changed it a bit now and have been using strlen and, I can get the error message to appear, but, it doesn't matter how many chars I enter whether it is more or less than 5, it still appears ! This is driving me crazy ! Any ideas on why it won't go on to ask for the Year when 5 chars are enterted ? I'm sure i'm missing something but can't think what.
    Code:
    //Do data Validation for registration number
    				
    		while(true)
    		{
    			cout << "Reg No:  " << flush;
    			gets(cars[next_record_no].regno);
    
    		if ((cars[next_record_no].regno, strlen) != 5)
    		{
    			cout<<"The registration number must equal 5 characters. Please enter it again. " << endl;
    		}
    		else 
    		{
    			break;
    		}
    		
    		}
    Thanks

    H_M

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    Let's recap a little. I posted an if statement like this:
    Code:
    if(input.length() != 5)
    which you essentially applied like this:
    Code:
    if( (regno, length) != 5)
    Do you see any difference? C++ has very strict syntax, and you can't just add a comma and leave out the period and put the parentheses in a different location. Your next attempt was essentially:
    Code:
    if ( (regno, strlen)) != 5)
    in which you just propogated the mistakes from before, and added some new ones because the strlen() function is used differently than the length() function--look it up here:

    http://www.cppreference.com/

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    Hi 7stud,

    Again, thanks for all your help with this ! I'm still having the same problem trying to get the strlen() function to work with my program, but think I will get there in the end, hopefully !

    H_M

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I'm still having the same problem trying to get the strlen() function to work with my program, but think I will get there in the end, hopefully !
    Write a practice program that has two lines:

    1) declare and initialize a char array

    2) display the length of the string using strlen()

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by 7stud
    You can do something like this:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	
    	string input;
    	bool goodInput;
    	
    	do
    	{
    		goodInput = true;
    		
    		cout<<"Enter exactly 5 characters: ";
    		getline(cin, input);
    		
    		if(input.length() != 5)
    		{
    			cout<<"Idiot!"<<endl;
    			goodInput = false;
    		}
    
    	}while(goodInput == false);
    	
    	return 0;
    }
    Slight nit-picky change. Rather than change goodInput all the time you could use:
    Code:
    goodInput = false;
    do
    {
        cout << "Enter exactly 5 characters: ";
        getline(cin, input);
        if (input.length() != 5)
        {
            cout << "Idiot!" << endl;
        }
        else
        {
            goodInput = true;
        }
    }while (goodInput == false);
    And using a while you don't need an infinte loop with break:

    Code:
    goodInput = false;
    while (goodInput == false)
    {
        cout << "Enter exactly 5 characters: ";
        getline(cin, input);
        if (input.length() != 5)
        {
            cout << "Idiot!" << endl;
        }
        else
        {
            goodInput = true;
        }
    }
    And SPACEs help readability. Told you they were nit-picky...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    Hi guys,

    Just want to say thanks for all your help with this. I started again using the examples on here, done a couple of tweaks to make it suit my own program and it is working fine ! Even have the strlen() working for all the other inputs now which is great !!

    Now, onto another part, making sure duplicates aren't accepted ! If I get stuck, I'll be back !

    Thanks again guys, this has to be the no1 C++ forums on the net !

    H_M

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Need a lesson on input and output data
    By dispatch4599 in forum C++ Programming
    Replies: 16
    Last Post: 02-06-2008, 09:04 PM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. validate data in a dialog box
    By COBOL2C++ in forum Windows Programming
    Replies: 4
    Last Post: 09-22-2003, 01:55 PM