Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Language: C++ Peter Parker is working on a program that will help him keep track

ID: 641220 • Letter: L

Question

Language: C++

Peter Parker is working on a program that will help him keep track of the product numbers of his web shooter components. Currently, his program asks for a model number as well as a part number. He wants to store this information as one string in his database. Help him write a function that will combine these pieces of information.

The function should accept a C-string by reference. The function will not return a value. Assume all necessary libraries have been included. The function should work as follows:

use a C-string (size 10) to hold user input

ask the user to type in the model number

the model number should be 4 characters long

the first two characters should be capital letters

the last two characters are digits

ask the user to type in the part number

the part number should be 5 digits

the model number and part number should be stored in the parameter

there should be a hyphen (-) between the model and part numbers

do not assume the user will enter valid data

if invalid input, continue to prompt the user for valid data

Explanation / Answer

#include <iostream>
#include <string>

using namespace std;

void getComponent(char str[11]){
   string temp;
   int flag = 1;
   while(true){
       cout << "Enter the model Number: ";
       cin >> temp;
       if(temp.size() == 4){
           flag = 1;
           for(int i = 0; i < 2; ++i){
               if(temp[i] > 90 || temp[i] < 65){
                   flag = 0;
               }
           }
           for(int i = 2; i < 4; ++i){
               if(temp[i] > 57 || temp[i] < 48){
                   flag = 0;
               }
           }
           if(flag){
               break;
           }
       }
   }
   for(int i = 0; i < 4; ++i){
       str[i] = temp[i];
   }
   str[4] = '-';
   while(true){
       cout << "Enter the Part Number: ";
       cin >> temp;
       if(temp.size() == 5){
           flag = 1;
           for(int i = 0; i < 5; ++i){
               if(temp[i] > 57 || temp[i] < 48){
                   flag = 0;
               }
           }
           if(flag){
               break;
           }
       }
   }
   for(int i = 0; i < 5; ++i){
       str[i + 5] = temp[i];
   }
   str[10] = '';
}

int main(){
   char str[11];
   getComponent(str);
   cout << "Spider man's component is " << str << endl;
   return 0;
}