Okay. Here is my work. I added a few more things to the program but that was because I was bored. I can still fix a few things but I got tired. I will edit and refine the code later on. Anyways, here it is:
Code: Select all
/*Assignment 5.3*********
**Code by: David Rivera**
**10.03.2007************/
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
/* Declare Functions */
void getInterestType(int p);
void getName(int p);
void getStartAmount(int p);
void getInterestPercentage(int p);
double compoundInterest(double percentage, double currentAmount);
double simpleInterest(double percentage, int startAmount, double currentAmount);
const int length = 30; //Names longer than 29 characters are not supported.
/* Create the template for each person's info */
struct personInfo
{
char name[length]; //Person name
int startAmount; //Users investment start amount
double interestPercent; //Users interest percent
int interestType; //Users interest type
double currentAmount; //users current balance
};
personInfo personOne; //Create first user info
personInfo personTwo; //Create second user info
int main()
{
getName(1); //Get first user name (In assignment "Daphne")
getStartAmount(1); //Get first users investment start amount (in assignment "$100")
getInterestPercentage(1); //First users interest percentage rate (in assignment "10%")
getInterestType(1); //Get first users investment type [simple or compound ( in assignment "simple")
getName(2); //Get second users name (in assignment "Cleo")
getStartAmount(2); //Get second users investment start amount (in assignment "$100")
getInterestPercentage(2); //Second users interest percentage rate (in assignment "5%")
do //interest type loop
{
getInterestType(2); //Get second users investment type [simple or compound ( in assignment "compound")
} while(personOne.interestType == personTwo.interestType); //Check if interest type is the same for user one & two, loop if true.
int years = 0; //Years it will take
char personAhead[length]; //Which person starts with simple interest
char personBehind[length]; //Which person starts with simple interest
personOne.currentAmount = personOne.startAmount; //Assign the start amount
personTwo.currentAmount = personTwo.startAmount; // to current amount
if(personOne.interestType == 1 && personTwo.interestType == 2) //Check which user starts with simple interest method
{
for(int i =0; i <= length; i++) //Loop for asigning character array tp another array
{
personAhead[i] = personOne.name[i]; //Assign the person with simple method to be ahead
personBehind[i] = personTwo.name[i]; //assign the person with compound method to be behind
}
do
{
personOne.currentAmount = simpleInterest(personOne.interestPercent, personOne.startAmount, personOne.currentAmount); //Simple interest calculation
personTwo.currentAmount = compoundInterest(personTwo.interestPercent, personTwo.currentAmount); //Compound interest calculation
years++; //Add a year
} while(personOne.currentAmount >= personTwo.currentAmount); //Do this loop while person ahead amount is greater or equal than person behind
} else { //If first check is not true then flip person ahead and person behind
for(int i =0; i <= length; i++) //Loop for assigning character array to another array
{
personAhead[i] = personTwo.name[i]; //Assign the person with simple method to be ahead
personBehind[i] = personOne.name[i]; //assign the person with compound method to be behind
}
do
{
personOne.currentAmount = compoundInterest(personOne.interestPercent, personOne.currentAmount); //Compound interest calculation
personTwo.currentAmount = simpleInterest(personTwo.interestPercent, personTwo.startAmount, personTwo.currentAmount); //Simple interest calculation
years++; //Add a year.
} while(personOne.currentAmount <= personTwo.currentAmount); //Do this loop while person ahead amount is greater or equal than person behind
}
cout << "It will take " << years << " years for " << personBehind << "'s investment to exceed " //Output how many years it will take for person with
<< personAhead << "'s investment." << endl; // compound interest to pass person with simple interest.
cout << personOne.name << "'s final balance: " << personOne.currentAmount << endl; //Output person one's final balance
cout << personTwo.name << "'s final balance: " << personTwo.currentAmount << endl; //output person two's final balance
system("pause"); //wait for user to press a key
return 0;
}
/* Function - get user names */
void getName(int p)
{
if(p == 2 ) //Check which user we are getting the information for
{
cout << "Please enter the name of person two." << endl << "Person Two Name: "; //prompt the user person name
cin >> personTwo.name; //Assign value to person 2 name
} else {
cout << "Please enter the name of person one." << endl << "Person One Name: "; //prompt the user
cin >> personOne.name; //Assign value to person 1 name
}
}
/* Function - get user start amounts */
void getStartAmount(int p)
{
cout << "Enter the starting investment amount." << endl << "Investment Amount: $"; //prompt the user
if(p == 2 ) //Check which user we are getting the information for
{
cin >> personTwo.startAmount; //Assign value to person 2 start amount
} else {
cin >> personOne.startAmount; //Assign value to person 1 start amount
}
}
/* Function - get user interest percentages */
void getInterestPercentage(int p)
{
cout << "Enter the interest percentage." << endl << "Interest Percentage: "; //prompt the user
if(p == 2) //Check which user we are getting the information for
{
cin >> personTwo.interestPercent; //Assign value to person 2 interest percent
personTwo.interestPercent /= 100; //Convert to decimal value
} else {
cin >> personOne.interestPercent; //Assign value to person 1 interest percent
personOne.interestPercent /= 100; //Convert to decimal value
}
}
/* Function - get user interest types */
void getInterestType(int p)
{
cout << "Enter the interest type." << endl << "( 1 = simple; 2 = compound )" << endl << "Interest Type: "; //prompt the user
if(p == 2) //Check which user we are getting the information for
{
cin >> personTwo.interestType; //Assign value to person 2 interest type
if(personTwo.interestType == personOne.interestType)
{
cout << personTwo.name << "'s interest type cannot match " //If person one & person two interest types match
<< personOne.name << "'s interest type." << endl; //prompt user to choose a different type
getInterestType(2); //loop the function
}
} else {
cin >> personOne.interestType; //Assign value to person 1 interest type
}
}
/* function - Calculate Comopund interest */
double compoundInterest(double percentage, double currentAmount)
{
currentAmount += currentAmount * percentage; //multiply current amount by percentage, then add it to current amount
return currentAmount; // return the value
}
/* function - Calculate simple interest */
double simpleInterest(double percentage, int startAmount, double currentAmount)
{
currentAmount += startAmount * percentage; //Multiply start amount by percentage the add it to current amount
return currentAmount; // return the value
}
/* PROGRAM END */
It's a bit long because of the extra work I did. Here is the screen shot of the output:

- Notice the values are not hard coded into the program, you have to input the values yourself.
- 5_3.jpg (44.6 KiB) Viewed 1338 times
If you guys need any help with this or don't understand the code just post & I'll do my best.
Update: I've fixed an error in the code where if you put the users in flipped, as in Cleo's information first then Daphne's, it give the incorrect balance & years it would take. Also added a check to make sure that both person's interest type differ from one another.
- Vinyard
- vinyard.