User-Defined functions

So, I have a few errors within my program, and I'm not quite sure how to fix them. On another note, how would I get data from one user-defined function and use it in another user-defined function, such as a printing one. I don't think I have it right in my code. Thanks.

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <stdlib.h>
#include <string>


using namespace std;

string createCustID(string fname, string lname); 
	
void createAccount(int accountNumber, double deposit); 

void printAccount(string newID, int& accountNumber, double& deposit); 



int main()
{
	

string createCustID(string fname, string lname); 
{
	string fname;
	string lname;
	
	
	cout << "Enter the customer's first name: " << endl;
	cin >> fname;

	cout << "Enter the customer's last name: " << endl;
	cin >> lname;

	string newID = fname[0]+lname[0]+lname[1]+lname[2]+lname[4]; //(fname[0])**it says that im trying to convert it from INT.  Not sure why.....
	cout << newID;
}


void createAccount (int accountNumber, double deposit);
{
	int accountNumber;
	double deposit;

	if (deposit >= 0 && accountNumber > 0)
		
		{ cout << "Enter the numerical account number(must be above Zero): " << endl;
			cin >> accountNumber;
			cout << "Enter your initial deposit: " << endl;
			cin >> deposit;

			return deposit; //somehow its saying that im converting this from double to int, so theres a possibility of losing data.
			return accountNumber;
		}
	else 
	{ cout << "Invalid account number or deposit." << endl;
	}
}

void printAccount(int& accountNumber, string newID, double& deposit); // somewhere around here or in the createAccount function, i havent initialized deposit, so it's causing a break.
{ string newID;
	 int accountNumber;
	 double deposit;

	cout << newID << accountNumber << "   $ " << deposit << endl; 
}

	system("PAUSE");

	return 0;
}
Current code with annotated errors:

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <stdlib.h>
#include <string>
#include <iomanip>


using namespace std;

string createCustID(string fname, string lname); 	
double createAccount(int accountNumber, double deposit); 
void printAccount(string newID, int& accountNumber, double& deposit); 


string fname;
string lname;
int accountNumber;
double deposit;
	
int main()
{
	string newID;
	newID = createCustID; // "=" Error: no operator "=" matchese these operands.

	system("PAUSE");

	return 0;
}
	
string createCustID(string fname, string lname)
{
	string newID = "";
	
	cout << "Enter the customer's first name: " << endl;
	cin >> fname;

	cout << "Enter the customer's last name: " << endl;
	cin >> lname;

	string newID = fname[0]+lname[0]+lname[1]+lname[2]+lname[4]; //still getting the conversion error with fname.
	cout << newID;
	
	return newID;
}


double createAccount (int accountNumber, double deposit) //i think it's correct to use double createAccount, but not 100% sure.
{
	

	if (deposit >= 0 && accountNumber > 0)
		
		{ cout << "Enter the numerical account number(must be above Zero): " << endl;
			cin >> accountNumber;
			cout << "Enter your initial deposit: " << endl;
			cin >> deposit;

			return accountNumber; 
			return deposit;       
		}
	else 
	{ cout << "Invalid account number or deposit." << endl;
	}
}

void printAccount(int& accountNumber, string newID, double& deposit)
{

	cout << newID << accountNumber << "   $ " << deposit << endl; 
}
Did you have any compilation errors or warnings? If so post them - it makes it easier.

LIne 22 does not provide any arguments expected by the function. The function doesn't need args because you get the info inside the function.

Also you have global variables - which is bad, at least put them into main.

The createAccount does need args, and you should use references to the variables accountNumber and deposit, so these values are changed in main, this way the function can be void.

And you won't have the conceptual error on lines 57 and 58. A function can only return 1 value, and when a return statement is encountered, control returns to just after where the function was called - so anything after a return statement in a block of statements is never reached. There is no return statement in the else starting at line 60, which is an error because the function must return a double.

Line 50: This should be a loop instead of if. Use a while loop with a bool variable to control when it exits the loop.

HTH
Topic archived. No new replies allowed.