C++ Value Lost

Discussion in 'Programming General' started by ghey, Jul 10, 2011.

C++ Value Lost
  1. Unread #1 - Jul 10, 2011 at 3:53 PM
  2. ghey
    Joined:
    Nov 6, 2010
    Posts:
    24
    Referrals:
    0
    Sythe Gold:
    0

    ghey Newcomer
    Banned

    C++ Value Lost

    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;
    	}
    
     
  3. Unread #2 - Jul 10, 2011 at 9:47 PM
  4. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    C++ Value Lost

    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.
     
< buying] need somone to make me a website. | * SwiftScreen BETA Released! * Screen Shot / Auto-Crop / Image Conversion >

Users viewing this thread
1 guest


 
 
Adblock breaks this site