modularize program code by using functions. You will produce two solutions: a) using global access functions, and b) using classes and member functions.

a) Using Access Functions

Write five Invoice access functions: setInvoice(), computeAmount(), getAmount(), printData() and a global client function computeAmount().

Function setInvoice() has five parameters of types Invoice, character array (for name and for phone), long (for account number), double (for gallons used). It sets the fields of its Invoice parameters using the values of its other parameters. Call this function in the first loop in main() after the test for array overflow.

Function computeAmount() has two parameters of type Invoice and double (for gallons used). It sets the value of the Invoice field 'amount' by adding the flat fee ($12) and the price per gallon ($0.05) multiplied by the number of gallons used (use global const symbolic values). Call this function in the first loop in main() after the call to setInvoice().

Function printData() has one parameter of type Invoice. It prints the fields 'name' in width 16 and 'phone' in width 14 (both are left-aligned), the field 'account' (in width 11, right-aligned), the field 'gallons' (in width 9, right-aligned), the field 'amount' (in width 8, right-aligned). Call this function from the second loop in main() instead of printing bill data explicitly.

Function getAmount() has one parameter of type Invoice. It returns the value if the parameter's field 'amount'. It is called from the global client function computeAmount(). This function has two parameters, an Invoice array and the count of valid elements in the array. It returns a double value that the total of all amounts in the array. This function computeAmount() defines a double local variable 'amount', goes in a FOR loop through all valid elements of the parameter array, calls getAmount() for each valid component of the parameter array and adds the return value of getAmount() to 'amount'. At the end, computeAmount() returns the accumulated tally. Call this function from main() to initialize the value of local variable 'total' (after the printing loop).

Pass structure variables by reference; pass built-in types by value; use the const modifier for structure and for array parameters that do not change as the result of the function execution. There is no need to use prototypes or multiple files: you will do that later. Instead, place the functions in the source code file between the structure definition and the start of main().

Dani AI

Generated

Short checklist for the common errors seen on this assignment (for and others): check string handling for char arrays, initialize numeric fields before use, enforce array bounds when reading input, and be careful with iostream manipulators (they persist and setw applies only to the next insertion). 's pointer to someone with the same problem and 's link may point to related fixes, but the items below target the usual compile/run mistakes directly.

Safe copying into fixed-size char buffers (instead of unsafe strcpy) and guaranteeing a terminator will stop many runtime bugs:

#include <cstring>

void safeCopy(char dest[], const char* src, std::size_t destSize) {
  std::strncpy(dest, src, destSize - 1);
  dest[destSize - 1] = '\0';
}

Formatting output with widths and alignment often confuses people because std::left/std::right persist and std::setw applies to the next field only. A compact pattern for one-line output:

#include <iomanip>
#include <iostream>

std::cout << std::left << std::setw(16) << inv.name
          << std::setw(14) << inv.phone
          << std::right << std::setw(11) << inv.account
          << std::setw(9) << inv.gallons
          << std::setw(8) << std::fixed << std::setprecision(2) << inv.amount
          << '\n';

For totals and object design: always initialize accumulators to 0.0 and limit loops to the valid count. If you move to a class-based solution, keep getters const and member computeAmount() to encapsulate pricing. Example skeleton:

class Invoice {
  char name[...];
  char phone[...];
  long account;
  double gallons = 0.0;
  double amount = 0.0;
public:
  void set(...);
  void computeAmount();      // sets amount
  double getAmount() const;  // returns amount
  void printData() const;
};

Final quick checks: include the right headers (<cstring>, <iomanip>, <iostream>), ensure every char buffer is large enough (leave space for '\0'), pass structs by reference when mutating, and keep the array count consistent between input, print, and total computations.

Recommended Answers

All 2 Replies

ummm, i dont know about ayone else but

there might be someone named haelly that has the exact same problem as you?

ask that person ^^ BM

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.