• Log in or sign up for an account here: https://www.sythe.org/login

C++ Value Lost

ghey

Newcomer
Joined
Nov 6, 2010
Sythe Gold
0
Messages
24
The function void info uses variables from float question why is the values from these variables not shown in void info it appears as 0.[/php]

Code:
#include<iostream>
using namespace std;

void Greeting ();
float Question ();
void info (float, float, float);

	float balance, total, i;

int main()
{

	Greeting();
	Question();
	info(balance, total, i);

system("pause");
return 0;
}

void Greeting()
{
	cout<< "Welcome to the Bank of Kwantlen!"<< endl << endl; 
	return;
}

float Question()
{
	char answer;
	float amount, rate, goal, newbalance, total, balance, i;
	

		int num;
		num = 5;

	cout <<"Do you want to invest (i) with us or borrow (b) from us? ";
	cin >> answer;
	cout << endl;

	
		cout << "Enter the amount to invest annually:	";
		cin >> amount;
		cout << "Enter the yearly percentage rate of return:	";
		cin >> rate;
		cout << "Enter the investment goal:		";
		cin >> goal;
		cout << endl;

		cout << "Year		Balance			Balance\n";
		cout << "		(Jan. 1)		(Dec. 31)\n";

	total = 0;
    i=1;
    newbalance =0;
    balance =0;

    while (total < goal)
    {
        balance = total + amount;
        total = balance *(1+(rate/100));

		cout << i << "		"	<<	balance << "			" << total << endl;	
		i++;
	}
	
	return balance; 
	return total;
	return i;
    }

	void info (float balance, float total, float i)
	{
		
		cout << total;
		cout << balance;
		cout << i;
	}
 
Le Monkey
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
Because you have local variables named total, balance and i in Question().

Those are what get assigned values within Question() and the info() function is passed the -global- variables with the same name (which remain unassigned).

Read about variable scope and this will make a lot more sense to you.

Call info() within Question() or correct the scope issue; learning how to take the latter option will help you more in the long run.
 
Runescape Gamblingrunescape gambling
Back
Top