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

Hello, I\'m trying to change this code I wrote last week from using strings to u

ID: 3889681 • Letter: H

Question

Hello, I'm trying to change this code I wrote last week from using strings to using c-strings (char arrays and functions) to read and output data. Here is the code I have so far:

https://paste.ofcode.org/PRuPU3WxT3efneELBZKNsM

Here are the instructions for the updated code:

1. Main program
a. Output instructions to the user to enter the work order in the appropriate format:
i. First 56 digits represent the customer number followed by the letter w
ii. The digits following the letter w represent the year
iii. The remaining digits (up to 5) represent the order number
b. Input the work order into a char array of size 20
c. Breakdown the work order by calling the functions described below
d. Output the components of the work order

i. Length

ii. Location of the letter w
iii. Customer number
iv. Year
v. Order number


2. Create and use the following functions to break out the components of the work order:
a. Built-in function strlen (workOrder)
i. Returns the length of the char array
b. int findW (char[], int);
i. Searches the workOrder to find the location of the letter w
ii. Returns the array location of the letter w
c. long getCustNum (char[], int);
i. Transfers the characters in the work order prior to the letter w into a local customer number character array
ii. Converts the local customer number character array to type long
iii. Returns the long value of the customer number
d. int getYear (char[], int);
i. Returns the two digist following the letter w as type int
e. long getOrderNum (char[], int, int);
i. Transfers the characters in the work order following the letter w into a local char array
ii. Converts the local char array to type long
iii. Returns the long value

Thank you so much!

Explanation / Answer

#include<iostream>
#include<string.h>
#include<stdlib.h>

using namespace std;

int findW(char data[], int n){
   int pos;
   for (int i =0; i<n; i++){
      if (data[i] == 'w'){
         pos = i;
         break;
      }
   }
}

long getCustNum(char data[], int n){
   
    long num;
    char a[5];
    for (int i = 0; i<n; i++)
        a[i] = data[i];
    num = atol(a);
    return num;   
}

long getOrderNum(char data[], int n, int m){
   
    long num;
    char a[4];
   
    for (int i=n; i<m; i++)
         a[i-n] = data[i];

   
    num = atol(a);
    return num;  
        
}

int getYear(char data[],int n){

   int num;
   char a[2];
   a[0] = data[n];
   a[1] = data[n+1];
  
   num = atoi(a);
   return num;
}

int main(){

   char workOrderNumber[20] = "91800w940770";
  
   int len = strlen(workOrderNumber);
   int pos = findW(workOrderNumber, len);
   long cust_no = getCustNum(workOrderNumber, pos);
   long order_no = getOrderNum(workOrderNumber,pos+3,len);
   int year = getYear(workOrderNumber,pos+1);

   cout << "The length is " << strlen(workOrderNumber) << endl;
   cout << "The location of the w is " << pos << endl;
   cout << "The customer number is " << cust_no << endl;
   cout << "The year of the order is " << year << endl;
   cout << "The order number is " << order_no << endl;
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote