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