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 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...
1/Write a C program that asks the user to enter 5 nums and adds each number...
1/Write a C program that asks the user to enter 5 nums and adds each number to a total. The program should then display the total. To add to the total, the program must call the function void sum(int value, int *ptotal) passing the value the user entered and the address of the total (a local variable in main). The function should add the value to the total. Note that the function is void and does not return anything. Don't...
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
Write a C program that loops and asks you every time to enter the name of...
Write a C program that loops and asks you every time to enter the name of a text file name, then it will read the file and perform statistics on its contents and display the results to the screen and to an output file called out.txt. The input files can be any text file. The program should calculate these integers as it reads character by character as we did in the last class example filecopy and make use of the...
Write in C++ Example Enter number of miles travelled 12340 Enter number of hours in trip...
Write in C++ Example Enter number of miles travelled 12340 Enter number of hours in trip 460 file.txt Miles: 12340 Hours: 460 MPH: 26.83 Question: Write a program that does the following things: 1 ) Ask the user for number of miles travelled (should be in getData function and number of hours in trip (should be in getData function) [Note: Use reference parameters to have access to these values in main] 2 ) Calculate the miles per hour(MPH) for the...
In C++ For this assignment, you will write a program to count the number of times...
In C++ For this assignment, you will write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of 31 characters named word An integer named count Functions Write the following functions: int main(int argc, char* argv[]) This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array...
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT