In: Computer Science
Write a C++ program that will require users to enter an automobile VIN number and you will check certain aspects: The VIN number must be 17 characters exactly in length The VIN number may contain only letters and numbers The first character is the location of manufacture; we will check for this to be 1 to 5 (made in North America). The 10th character indicates the manufacture year. We will check for this being a letter between A and I (years 2010 to 2018)
Deliverables: Word document with copy of code and screenprints of all test runs.
writing a program that will require users to enter the VIN number of their car. You want to write a function named chkVIN() that will check for a certain things (see below). The chkVIN() function will test if the VIN number meets the rules shown above. The function will be passed a character array (not a string variable) containing the VIN number. It is required that the function use a pointer in processing the array. Here is the prototype for the function: bool chkVIN(char vin[]); The function is passed a char array; it returns true if the VIN passes all tests and false if any tests fail. It returns immediately after reporting a problem. In the main function, use the following code to declare a VIN number variable: const int SIZE = 30; char VIN[SIZE]; We are using an array of larger than 17 because the user might enter more characters. Our main program will have a loop that will enter VIN numbers and check them by passing the array to chkVIN(). chkVIN() returns fail if the VIN fails any tests and returns true if the VIN passed all tests. The problems will be displayed by chkVIN(). The main function just reports if the VIN passed or failed and asks if the user wants to enter more VIN numbers. Test your code with these values:
|
||||||||||||||
Step 2: Processing Logic |
||||||||||||||
You may use this pseudocode for the program or code it differently as long as you meet the requirements listed above (using the function, passing a char array, function uses a pointer).. Place Program Documentation Header here. Declare two int constants SIZE and NUMCHARS (SIZE is 30; NUMCHARS is 17) bool chkVIN(char vin[]) //function prototype Main Function Declare character array: char vin[SIZE] Declare string variable, answer, and assign “yes” for loop control Display "Verify VIN Number Program" Display "- The VIN number should be exactly 17 characters long" Display "- The VIN number should only contain letters and numerals” Display "- The VIN number should indicate the car is manufactured in North America.” Display "- The VIN number should be for a car manufactured in 2010 or later.” While answer equals “yes” //start of while loop Display “Enter your VIN number: “ Input into char array using the cin.getline function //important to use cin.getline(arrayName, SIZE) if chkVIN(vin) equals true then Display “The VIN number is valid and acceptable” else Display “The VIN number was invalid or unacceptable” end if
Display “Do you want to enter another VIN number? (yes or no): “ input answer End While Loop END main function //Start chkVIN function bool chkVIN(char VIN[]) //check for 17 characters if strlen(VIN) not equal to NUMCHARS then Display “VIN number must have “ + NUMCHARS + “ characters” return false End if //check that all characters are digits or letters //NOTE: to check for letters or digits, we use the isalpha(char) and isdigit(char) functions //These return true if the character is a letter or digit, respectively. //They return something else if not a letter or digit – not necessarily false. //Pass a pointer to the array – the array name – plus the for loop iterator variable For Loop 0 to NUMCHARS if current character is a letter, continue loop else if current character is a digit, continue loop else Display “VIN number can only contain letters and digits” return false End If End for Loop //check that the first character in the array is in the range 1 to 5 //remember these are characters – not numbers – so you need single quote marks if first character is less than ‘1’ or the first character is greater than ‘5’ then Display “VIN number should indicate a car manufactured in North America but did not” return false End If //check that the 10th character is between A and I; allow for small and capital letters //suggestion: use the toupper(char) function and then compare to ‘A’ and ‘I’ //Must use array name and offset to pass to toupper() if 10th character is less than ‘A’ or 10th character greater than ‘I’ then Display “VIN number should indicate a car manufactured in 2010 or later, but did not” return false End If //if we got here, we passed all the error checks return true End chkVIN function |
C++ code(according to the pseudo code provided):
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
bool chkVIN(char []);
#define SIZE 30
#define NUMCHARS 17
int main()
{
char vin[SIZE];
string d = "yes";
cout<<"Verify VIN Number Program\n";
cout<<"The VIN number should be exactly 17
characters long\n";
cout<<"The VIN number should only contain
letters and numerals\n";
cout<<"The VIN number should indicate the car is
manufactured in North America.\n";
cout<<"The VIN number should be for a car
manufactured in 2010 or later.\n";
while(d=="yes")
{
cout<< "Enter your VIN number: ";
cin.getline(vin, SIZE);
if(chkVIN(vin))
cout<<"The VIN number is valid and acceptable\n";
else
cout<<"The
VIN number was invalid or unacceptable\n";
cout<<"Do you want to enter
another VIN number? (yes or no): ";
getline(cin,d);
}
return 0;
}
bool chkVIN(char vin[])
{
char *p = vin; //pointer to array
if(strlen(vin)!=NUMCHARS)
{
cout<<"VIN number must have
"<<NUMCHARS<<" characters.\n";
return false;
}
for(int i=0;i<NUMCHARS;i++,p++)
{
if(isalpha(*p))
continue;
else if(isdigit(*p))
continue;
else
cout<<"VIN
can only contain letters and digits.\n";
return
false;
}
p = vin; //again pointer initialises to starting
position
if(*p<'1' || *p>'5')
{
cout<<"VIN number should
indicate a car manufactured in North America but did not.\n";
return false;
}
if(toupper(*(p+9))<'A' ||
toupper(*(p+9))>'I')
{
cout<<"VIN number should
indicate a car manufactured in 2010 or later, but did
not.\n";
return false;
}
return true;
}
OUTPUT: