Returning a value in a void function

Oct 26, 2010 at 11:54pm
How would one go about returning a value in a type void function? I would assume that it is possible in some way because my professor has assigned my class an assignment in which we have to do just that. We have to make 5 functions, all type void to integrate with our previous project. Here is the prompt for the first function:

getBalance is a function that will open a file called balance.txt, and retrieve the credit value. If there is nothing in the balance.txt file, or it doesn’t exist, assume that the user wants to begin a new account, with 50 credits. getBalance is a void function that returns either the balance from the file or 50 credits.

I understand the fileio part, but I don't know how to return balance in a void function. Would anybody be willing to point me in the right direction? I'm not asking for any code, just advice. Thanks.
Oct 27, 2010 at 12:24am
couldn't you use a pointer?
http://www.cplusplus.com/doc/tutorial/pointers/

1
2
3
4
5
6
7
8
9
10
int& credit;
ifstream file("");

getBalence(credit, file);

//...

void getBalence(int* credit,ifstream file){
// your code
}


i am just typing off the top pf my head, just giving you a direction
Oct 27, 2010 at 3:56am
You've got a value and some associated functions. Use a class. Here's an example of what a class would look like for your issue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class  Bank_account {
public:	
	int balance;
	Bank_account() {balance =0;}
        Bank_account(int amount){balance = amount;}

void get_balance(){
	if (condition)
		dosomethingelse;
	else (condition)
		dosomethingelse;
	}
}

int main(){
	Bank_account myaccount;
        vector<Bank_account> thebank;
        thebank.push_back(myaccount);
	myaccount.get_balance();
	cout << thebank.at(0).balance;
}


Loop input into an object and toss the object into the vector. Then loop through checking balance and writing stuff to the new file.
Last edited on Oct 27, 2010 at 3:59am
Oct 27, 2010 at 5:05am
The simplest solution:
1
2
3
4
void sum( int& result, int a, int b )
{
  result = a + b;
}
Topic archived. No new replies allowed.