Question

In: Computer Science

Write a C++ program that will require users to enter an automobile VIN number and you...

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:

VIN

Expected Result

2ABCD1234AB123456

Valid VIN number

2ABCD

Error: VIN must have 17 characters

2ABCD1234AB123456789

Error: VIN must have 17 characters

2ABCD1234AB---456

Error: VIN can only contain letters and digits

XABCD1234AB123456

Error: Not manufactured in North America

2ABCD1234XB123456

Error: Not manufactured in 2010 or later

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

Solutions

Expert Solution

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:



Related Solutions

IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
write a C++ program that permits users to enter the following information about your small company’s...
write a C++ program that permits users to enter the following information about your small company’s 5 employees and then writes the information to a file “employee.txt”: ID No.   Sex (M/F) Hourly Wage Years with the Company Make sure that you open the file for write using the two-step process of “trial read followed by write” so that an existing file with the same name does not get inadvertently clobbered. Close all open files after completing read or write operations....
Write a C program that allows users to enter three runners name (up to 20 characters)...
Write a C program that allows users to enter three runners name (up to 20 characters) and their runtime (2 decimals) (1) First and Last name (2) Running time for 100 m ex. 9.96 seconds your program should rank them in ascending order of their runtime. example: usian Bolt - 9.96 sec - First Place John doe - 9.99 sec - second Place Pete Urel -10.11 sec - third place BUT they fan tie for first place and no second...
Write a program in Java that asks users to enter a series of marks (one at...
Write a program in Java that asks users to enter a series of marks (one at a time) until a signal is entered to stop. Have your program ensure that each mark entered is between 0 and 100. When done, output the number of marks entered and the average of all marks. Also output the highest mark and the lowest mark, and the range between the highest and lowest mark.
Write a C++ program that keeps asking user to enter a positive integer number until a...
Write a C++ program that keeps asking user to enter a positive integer number until a sentinel value (999) is entered. Then for each positive number entered by the user, find out how many digits it consists. (Hint: divide a number by 10 will remove one digit from the number. You can count how many divisions it needs to bring the number to 0.) An example of executing such a program is shown below. Note that the user input is...
In C#, write a console-based program that asks a user to enter a number between 1...
In C#, write a console-based program that asks a user to enter a number between 1 and 10. Check if the number is between the range and if not ask the user to enter again or quit. Store the values entered in an array. Write two methods. Method1 called displayByVal should have a parameter defined where you pass the value of an array element into the method and display it. Method2 called displayByRef should have a parameter defined where you...
C++ [2] Write a program that prompts the user to enter a non-negative decimal number and...
C++ [2] Write a program that prompts the user to enter a non-negative decimal number and a base in the range 2 <= base <= 16. Write a function multibaseOutput() that displays the number in the specified base. The program terminates when the user enters a number of 0 and a base 0. Run: Enter a non-negative decimal number and base (2 <= B <= 16) or 0 0 to terminate: 155 16     155 base 16 is 9B Enter...
Write a C++ program to allow a user to enter in any positive number greater than...
Write a C++ program to allow a user to enter in any positive number greater than or equal to zero. The program should not continue until the user has entered valid input. Once valid input has been entered the application will determine if the number is an abundant number or not and display whether or not the number is an abundant number. If the user enters in a 0 the program should quit. An abundant number is a number n...
write a c++ program . Ask the user to enter a number less than 100. Test...
write a c++ program . Ask the user to enter a number less than 100. Test the input to make sure it is correct, and use a while loop to continuously ask the user for correct input value if they don't follow the input rule. After receiving the correct input, test the number to see if it is even or odd. If it is odd, use a while loop to do the following: Output to the monitor all odd numbers...
URGENT write a c++ program which asks the user to enter a positive number which is...
URGENT write a c++ program which asks the user to enter a positive number which is less than or equal to 9. Do not let the user continue until he or she enters a number within range. once a valid number is entered, multiply it by itself. if the resulting number is even, display a message with the number followed by the words even. If it is odd, display the number followed by the words is odd. must use a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT