Sterling Function

Hey there! I'm a little stuck with a program. I was hoping i could get someone to look over the files and also help me understand the logic [3 files, 2 .cpp and one .h]

//sterling.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

struct sterling
{
  int pounds;
  int shillings;
  int pence;



};

sterling make_sterling (int p, int s, int pe);
sterling make_sterling (int pe);
void print (sterling s_sterling);
sterling add (sterling s1, sterling s2);


// sterling.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "sterling.h"
#include <iostream>
#include <iomanip>
using namespace std;

sterling make_sterling (int p, int s, int pe)
{
  sterling temp; //local variable contents of temp are undefined 
  
  temp.pounds = p;
  temp.shillings = s;
  temp.pence = pe;

  return temp;

}

sterling make_sterling(int pe)
{
  sterling temp;
  
  temp.pounds = pe / 240; // 12 pence increment shillings by one
  // convert pence into pounds and the modulus into shillings
  pe %= 240; 
  
  temp.shillings = pe / 12;
  pe %= 12;

  temp.pence = pe;
  
  return temp;
  
}

sterling add (sterling s1, sterling s2)
{
  int  S1 = s1.pence + s2.pence;
  int  S2 = s1.shillings + s2.shillings;
  int  S3 = s1.pounds + s2.pounds;
  
  return make_sterling(S1, S2, S3);
}


// driver.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "sterling.h"
using namespace std;

int main ()
{
  sterling S = make_sterling(100);
  print(S);
  
  sterling P = make_sterling(1, 20, 30);
  print(P);
  
  sterling PE = add (S,P);
  print(PE);
  
  return 0;
}


I need to define a function named print, which accepts one sterling structure passed as a reference and prints it in the 3-decimal point notation: £6.3.5
i. You can print the pound sign with this code: cout << (char)156; (works on
Windows but may not work on other platforms)
e. Define a function named read that:
i. has a sterling structure pointer argument (i.e., pass-by-pointer as discussed in class)
ii. reads a sterling structure from the keyboard (3 separate prompts and 3 separate reads)
iii. returns the sterling structure through the pointer argument

I'm somewhat lost when it comes to what it's really asking. Would anyone care to help explain it?

with some of what is being asked, i don't really know where to put it

Sorry for the length :)
Last edited on
1
2
3
4
void print ( sterling& param )
{
    cout << char ( 156 ) << param -> pounds << char ( 46 ) << param -> shillings << char ( 46 ) << param -> pence;
}


As for problem e, you can read in input from the user with cin >> //... and then cstdlib to convert the string to a number then use a structure reference/dereference to set the memebers of the structure. Although it would probably be nicer to use methods.
Last edited on
There is a prototype for print() in sterling.h. You just need to put the full definition of that function in sterling.cpp.

One minor discrepancy, the description above says the parameter should be passed by reference but the existing prototype has the '&' missing (it should look like this, void print (sterling &s_sterling);

Do you understand how the currency works? 12 pennies make a shilling, either 20 shillings or 240 pennies make a pound.

Some of the functions need some care, to ensure that the value of shillings never exceeds 19, and pence never exceeds 11.

Then you need to add the read function (prototype in the .h file, full definition in the .cpp file).
Topic archived. No new replies allowed.