C++ programming

Discussion in 'C++' started by cbbest, Jul 10, 2012.

  1. cbbest

    cbbest New Member

    Joined:
    Jul 10, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Here is my problem:
    Effective January 1st of each year Gabriela receives a 5% raise on her previous year's salary. She wants a program that calculates and displays the amount of her annual raises for the next 3 years. The program also should calculate and display her total salary for the 3 years.

    Here is what I've got...of course not right...any help would be great appreciated!

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main ()
    {
    	//declare variables
    	double salary = 0.0;
    	int years = 1;
    	const double raise = .05;
    	double total = 0.0;
    
    	//get first annual salary
    	cout << "Enter annual salary: ";
    	cin >> salary;
    
    	while ( years <= 3)
    	{
    		cout << "raise = salary * .05: ";
    		cout << "Enter raise: " << raise << endl;
    		cout << "Enter number of years: " << years << endl;
    		cout << "new salary: " << salary * raise << endl;
    		cin >> salary;	
    	 }	//end while		
    
    	//display the total salary for the next 3 years
    	cout << "Total Salary: " << total << endl;
    
    	system("pause");
    	return 0;
    }	//end of main function
     
  2. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    while ( years <= 3)
    {
    	cout << "raise = salary * .05: ";
    	cout << "Enter raise: " << raise << endl;
    	cout << "Enter number of years: " << years << endl;
    	cout << "new salary: " << salary * raise << endl;
    	cin >> salary;	
     }	//end while
    this loop has a few issues.

    you wouldn't want to ask the user for the salary with each iteration of years because you're giving the user a chance to change the salary while calculating the new yearly wage.

    you seem to be missing your increment of years. to make the loop quit, you need ++years somewhere inside of the loop.



    Depending on what you need to display, and by my math :D

    Code:
    while(...) {
       total = salary + salary * raise;
       ... // output stuff
       salary = total;
       ... // other bits of code
    }
    if you need to keep salary as it was originally, create a temp variable, assign it the value of salary prior to entering the loop, and then within the loop, manipulate the temp variable instead of salary. That way, you can output something like you've earned $X more after N years by using (total - salary).

    Hope that helps
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice