Thread: Problem getting the input from a temp variable into the Array

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

    Problem getting the input from a temp variable into the Array

    Hi guys,

    I'm having another wee problem with a wee program that I'm trying to create. I managed to get the 'Validation' working so that it would reject an input that wasn't equal to five chars, but, now I want it to check on input, if there are any registration numbers already like that in the database, but I can't get it to work.

    On input, I'm intending on getting the input placed into a temporary variable which after it has passed the length validation will then be checked against the other records in the array and if it exists, output a message - but, when I'm trying to copy the input into the array using 'strcpy', something is going wrong as it's not being copied in but is copying everything else so that my records on the db look weird as one of them is blank ! Does anyone have any ideas ??

    Here is the part of the program I'm 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;
    	int min_engine_size = 900;
    	int max_engine_size = 6000;
    	int i;
    	i = 0;
    	bool unique = false;
    	char temp_regno[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);
    
    		//Data Validation for registration number
    do
    {
    				
    		do
    		{
    			cout << "Reg No:  " << flush;
    			gets(temp_regno);
    
    			length = strlen(temp_regno);
    
    		if (length != 5)
    		{
    			cout<<"The registration number must equal 5 characters. Please enter it again : " << endl;
    		}
    		}
    		while (length != 5);
    
    		//Duplicate registration number check
    
    			i= 0;
    
    		while (strcmp(temp_regno, cars[i].regno) !=0 && i < next_record_no -1)
    		{
    			i++;
    		}
    
    		if (strcmp(temp_regno, cars[i].regno) ==0)
    		{
    			cout<<"This registration number already exists. Please re-enter a unique number : " << endl;
    			unique = false;
    		}
    
    		else 
    		{
    			unique = true;
    			strcpy (temp_regno, cars[i].regno);
    			strcpy (temp_regno, cars[next_record_no].regno);
    		}
    }
    while (unique == false);
    
    		
    
    		cout << "Year:    ";
    		cin  >> cars[next_record_no].year;
    		cout << "Price:   ";
    		cin  >> cars[next_record_no].price;
    		cout << "Mileage: ";
    		cin  >> cars[next_record_no].mileage;
    
    		// Do engine size validation. Engine size must be between 900cc and 6000cc
    
    		do
    		{
    			cout << "Engine size in cc:   ";
    			cin  >> cars[next_record_no].engine;
    
    		if (cars[next_record_no].engine < min_engine_size || cars[next_record_no].engine > max_engine_size)
    		{
    			cout<<"The engine size must be between 900cc and 6000cc. Please enter it again : " << endl;
    		}
    		}
    
    		while (cars[next_record_no].engine < min_engine_size || cars[next_record_no].engine > max_engine_size);
    
    	}
    	// 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);
    }
    Any help or advice on this matter would be greatly appreciated.

    H_M

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    gets(cars[next_record_no].make);
    cout << "Model:   " << flush;
    gets(cars[next_record_no].model);
    
    //Data Validation for registration number
    do
    {
    
        do
        {
            cout << "Reg No:  " << flush;
            gets(temp_regno);
    gets() is evil. If you were using C, I'd recommend switching to fgets() instead. However, since you're using C++, I'd say to use cin.get() or perhaps some version of getline().


    That said...
    Code:
    char temp_regno[5];
    
        length = strlen(temp_regno);
    
        if (length != 5)
        {
            cout<<"The registration number must equal 5 characters. Please enter it again : " << endl;
        }
    }
    while (length != 5);
    You can only store a maximum of 5 characters in temp_regno. To use strlen correctly, one of those must be a NULL terminator which means you could only safely put 4 usefull characters into temp_regno preserving the last one for the NULL so that the strlen call will work. You must increase the size of your character array to 6 so that you can have 5 meaningful characters and the NULL terminator as the 6th.

    This is almost certainly affecting the strcmp calls you are using to do the registry number comparison which could be throwing things off.

    All of this would be much simpler if you were using string objects instead of character arrays.

    Haven't looked at anything else in your code...
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

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

    Thanks for your reply, it's much appreciated.

    I am just really learning C++ so thought that gets would be the best to use - what's wrong with it can I ask ??

    I have changed the size of the character array to 6 as you suggested, but I am still having the same problem. I think it's got something to do with where I've put the strcpy lines, but if I put them infront of the second while, then it's just telling me that the number exists regardless of what is entered. It's really puzzling me !

    Also, how would I use a string object ??


    Thanks for all help.

    H_M

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I am just really learning C++ so thought that gets would be the best to use - what's wrong with it can I ask
    It has no protection against buffer overflow.
    You might prompt for 5 characters, but if someone types in
    "I wonder if this will crash the program"
    then all those extra characters are going to overwrite something you didn't want overwriting.

    Since this is C++, all the strlen() and strcpy() and strcmp() should go as well.
    The std::string class has lots of safe ways of dealing with strings.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Placing array in temp table
    By cjohnman in forum C Programming
    Replies: 3
    Last Post: 04-16-2008, 12:53 PM
  2. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM