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

I need help getting a table printed out like the following. This is in C program

ID: 667433 • Letter: I

Question

I need help getting a table printed out like the following. This is in C programming/

/*Advice: Print a customer's info on a line followed on subsequent lines
with indented traits.
Example Output:
ID Customer Name
Trait Value
11111 BOB WIRE
GENDER M
EXERCISE BIKE
EXERCISE HIKE
SMOKING N
22222 MELBA TOAST
GENDER F
BOOK COOKING
33333 CRYSTAL BALL
SMOKING N
GENDER F
EXERCISE JOG
EXERCISE YOGA
*/
void printCustomerData(Customer customerM[], int iNumCustomer)
{
int i;
int j;
// Print a heading for the list of customers and traits
// printf("Needs a heading ");
for (i = 0; i < iNumCustomer; i++)
{
// Print the customer information

// Print each of the traits
for (j = 0; j < customerM[i].iNumberOfTraits; j++)
{
// Print a trait

}
}
}

Explanation / Answer

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include "cs2123p0.h"

int main(int argc, char *argv[])
{
Customer customerM[MAX_CUSTOMERS]; // customer array
int iNumberOfCustomers = 0; // number of customers in customerM

getCustomerData(customerM, &iNumberOfCustomers);

printCustomerData(customerM, iNumberOfCustomers);

return 0;
}

/*
Replace with a good function header. Please see my programming standards.
*/
void getCustomerData(Customer customerM[], int *piNumCustomer)
{
FILE *pfileCustomer; // input stream file
char szInputBuffer[MAX_LINE_SIZE + 1]; // input buffer for fgets

int iNumTrait = 0; // Number of traits for the current customer
char szRecordType[11]; // record type of either CUSTOMER or TRAIT
int i = -1; // current customer subscript. -1 indicates
// not on a customer yet
int iScanfCnt; // scanf returns the number of successful inputs
char *pszRemainingTxt; // After grabbing a token, this is the next
// position. This will be after the delimiter
// unless the string was terminated by a zero
// byte, then it will be on the zero byte.
pfileCustomer = stdin;

// read data input lines of text until EOF. fgets returns NULL at EOF
while (fgets(szInputBuffer, MAX_LINE_SIZE, pfileCustomer) != NULL)
{
// if the line is just a line feed, skip it.
if (szInputBuffer[0] == ' ')
continue;
  
// print the input buffer as is (it also has a linefeed)
printf("%s", szInputBuffer);

// get the CUSTOMER or TRAIT command
pszRemainingTxt = getToken(szInputBuffer, szRecordType, sizeof(szRecordType)-1);

// see if getting a customer or a trait
if (strcmp(szRecordType, "CUSTOMER") == 0)
{
i++;
// see if we have too many customers to fit in the array
if (i >= MAX_CUSTOMERS)
ErrExit(ERR_TOO_MANY_CUST
, "Invalid input file, max customers is %d"
, MAX_CUSTOMERS);

iNumTrait = 0; // since we have a new customer, reset his/her number of traits
customerM[i].iNumberOfTraits = iNumTrait;
iScanfCnt = sscanf(pszRemainingTxt, "%6s %20[^ ] "
, customerM[i].szCustomerId
, customerM[i].szCustomerName);
  
// Check for bad input. scanf returns the number of valid conversions
if (iScanfCnt < 2)
{
WARNING("Expected ID and name, received %d successful values"
, iScanfCnt);
continue;
}
}
else if (strcmp(szRecordType, "TRAIT") == 0)
{
// what if we haven't received a CUSTOMER record yet
if (i < 0)
ErrExit(ERR_BAD_INPUT
, "TRAIT record without CUSTOMER");

// what if we have too many traits
if (iNumTrait >= MAX_TRAITS)
ErrExit(ERR_BAD_INPUT
, "Too many traits for Customer ID %s, only %d traits allowed"
, customerM[i].szCustomerId
, MAX_TRAITS);

iScanfCnt = sscanf(pszRemainingTxt, "%10s %12s"
, customerM[i].traitM[iNumTrait].szTraitType
, customerM[i].traitM[iNumTrait].szTraitValue);

// Check for bad input. scanf returns the number of valid conversions
if (iScanfCnt < 2)
{
WARNING(
"Expected trait type and value, received %d successful values"
, iScanfCnt);
continue;
}
iNumTrait++;
customerM[i].iNumberOfTraits = iNumTrait;
}
else
{
WARNING("Bad Command in input, found '%s'"
, szRecordType);
continue;
}
}
*piNumCustomer = i + 1;
}

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