#include <iostream> #include <string> void getSurname(string&); void getFName(string&); void getBalance(float&); struct clients { string sname; string fname; float due; }; int main(void) { struct clients client; getSurname(client.sname); getFName(client.fname); getBalance(client.due); return 0; } void getSurname(string& n) { n = ""; cout << "Enter client surname: "; cin >> n; } void getFName(string& n) { n = ""; cout << "Enter client first name: "; cin >> n; } void getBalance(float& b) { b = 0; cout << "Enter client account balance: "; cin >> b; }
Since the functions are void type functions they cannot return a value. The solution is to use reference parameters.
Each of the functions has a single reference parameter. We just hand over the reference to the structure field, eg client.surname.
Copyright © 1999 - 2001
David Beech