Business problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • audiokarate
    New Member
    • Sep 2007
    • 16

    #1

    Business problem

    Well here is my program. I'm new to C++ and new to this forum. Hopefully someone can tell me how to get over this hurdle. I need to use a for loop to change the years to year 1, year 2 etc. My problem is that I need to output the estimated and adjusted salary also for each year. I am stuck! Whenever I run it like this, the years change however it only outputs the estimated and adjusted for year 1. Help??


    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    int main()
    {
    
    	const double INFLATION_RATE = .0285;
    
    	double annual_salary = 0;
    	double growth_rate = 0;
    	double yr_1 = 0,
    		   yr_2 = 0,
    		   yr_3 = 0;
    	double adjusted_yr1 = 0,
    		   adjusted_yr2 = 0,
    		   adjusted_yr3 = 0;
    	
    	double annual_growth = 0;
    	double annual_inflation = 0;
    	double total_estimated = 0;
    	double total_adjusted = 0;
    	double growth_percent = 0;
    	double annual_growthYR1 = 0;
    	double annual_growthYR2 = 0;
    	double annual_growthYR3 = 0;
    	double annual_inflationYR2 = 0;
    	double annual_inflationYR3 = 0;
    	double count = 0;
    
    	
    	
    	cout << "Enter your annual salary: ";
    	cin >> annual_salary;
    	cout << endl;
    
    	cout << "Enter the estimated yearly rate of growth for your salary in percent: ";
    	cin >> growth_percent;
    	cout << endl;
    
    	
    	
    
    	//ESTIMATED INCOME CALCULATIONS
    	
    	growth_rate = growth_percent/100; 
    	annual_growthYR1 = annual_salary * growth_rate;
    	yr_1 = annual_salary + annual_growthYR1;
    	annual_growthYR2 = yr_1 * growth_rate;
    	yr_2 = yr_1 + annual_growthYR2;
    	annual_growthYR3 = yr_2 * growth_rate;
    	yr_3 = yr_2 + annual_growthYR3;
    	total_estimated = yr_1 + yr_2 + yr_3;
    	
    	
    	// ADJUSTED INCOME CALCULATIONS
    
    	annual_inflation = annual_salary * INFLATION_RATE;
    	adjusted_yr1 = yr_1 - annual_inflation;
    	annual_inflationYR2 = yr_1 * INFLATION_RATE;
    	adjusted_yr2 = yr_2 - annual_inflationYR2;
    	annual_inflationYR3 = yr_2 * INFLATION_RATE;
    	adjusted_yr3 = yr_3 - annual_inflationYR3;
    	total_adjusted = adjusted_yr1 + adjusted_yr2 + adjusted_yr3;
    
    
    
    
    	for (count = 1; count < 4; ++count)
    	{
    	//OUTPUT
    	cout << " The estimated salary for year " << count << " is " << yr_1 << endl;
    	cout << " The adjusted salary for year " << count << " is " << adjusted_yr1 << endl << endl;
    
    	
    	
    	}// END FOR LOOP
    	
    	
    	
    	
    	cout << " The total estimated salary for all 3 years is " << total_estimated << endl 
    		 << " and the total adjusted salary for all 3 years is " << total_adjusted;
    	cout << endl << endl;
    	cout << "***************************************************************************" << endl << endl;
    	
    	
    	
    	
    	
    
    	return(0);
    
    }//END MAIN
  • hdanw
    New Member
    • Feb 2008
    • 61

    #2
    Originally posted by audiokarate
    Well here is my program. I'm new to C++
    You need to study some more. Open your book and read to chapter 6.

    What you want is an array.

    change this :
    Code:
    yr_1 = annual_salary + annual_growthYR1;
    	annual_growthYR2 = yr_1 * growth_rate;
    	yr_2 = yr_1 + annual_growthYR2;
    	annual_growthYR3 = yr_2 * growth_rate;
    	yr_3 = yr_2 + annual_growthYR3;
    	total_estimated = yr_1 + yr_2 + yr_3;
    to this :
    Code:
    yr[1] = annual_salary + annual_growthYR1;
    	annual_growthYR2 = yr[1] * growth_rate;
    	yr[2] = yr_1 + annual_growthYR2;
    	annual_growthYR3 = yr[2] * growth_rate;
    	yr[3] = yr_2 + annual_growthYR3;
    	total_estimated = yr[1] + yr[2] + yr[3];
    and do the same in your loop.

    Code:
    double yr[4];
    double adjusted_yr[4]
    Of course if you dont want to use arrays you can always write your loop like this :
    Code:
    for (count = 1; count < 4; ++count)
    	{
    	//OUTPUT
    		 switch(count )
    		 {
    				 case 1:
    					 cout << " The estimated salary for year " << count << " is " << yr_1 << endl;
    					 cout << " The adjusted salary for year " << count << " is " << adjusted_yr1 << endl << endl;
    				 break;
    				 case 2:
    					 cout << " The estimated salary for year " << count << " is " << yr_2 << endl;
    					 cout << " The adjusted salary for year " << count << " is " << adjusted_yr2 << endl << endl;
    				 break;
    				 case 3:
    					 cout << " The estimated salary for year " << count << " is " << yr_3 << endl;
    					 cout << " The adjusted salary for year " << count << " is " << adjusted_yr3 << endl << endl;
    				 break;
     
     
     
    	}// END FOR LOOP
    which is easier, that or this?
    Code:
    	for (count = 1; count < 4; ++count)
    	{
    	//OUTPUT
    	cout << " The estimated salary for year " << count << " is " << yr[count] << endl;
    	cout << " The adjusted salary for year " << count << " is " << adjusted_yr[count] << endl << endl;
     
    	}// END FOR LOOP
    note that the indexing actually starts at 0 (zero) and goes to one less than the specified size
    Code:
    type my_variable_name[size];
    Good luck,

    Dan -

    Comment

    Working...