Open Source Institute | CyberArmy Intelligence & Security | CyberArmy Services & Projects

[Library Index]

[View category: C++] [Discuss Article]

Example for C++

Article is yet to be rated
Author:      pjd
Submitted:      28-Apr-2007 19:41:03
Imported From:      The CyberArmy University (original author: pjd)


Example for C++: A program that calculates compounded interest on a yearly rate.
// Code Example 1.1 

  // PJD 

  /* This program calculates compounded interest on a yearly rate 

  It includes almost all concepts of C++ when your first learning 

  If you dont understand what something does try to figure it out 

  If not compile this program and do some research */ 

  */ 

  

  #include <iostream.h>; //Allows cin/cout 

  #include <iomanip.h>; //Allows formatting of numbers 

  #include <stdlib.h>; //Allows DOS Commands (I only use "CLS" to clear the screen) 

  // Function Prototypes 

  //They do not return a value 

  void print_settings(); 

  void print_savings(); 

  void print_summary(); 

  void print_calculations(); 

  void print_millionare(); 

  // Global Variables 

  /*Im going to use these to prevent the need to pass by reference/value 

  it would only make this program more confusing */ 

  

  //Integers 

  //Int short allows for large numbers 

  int short HoursWorked; 

  int short Hours; 

  int short Hour; 

  int short userc; 

  int short Y; 

  int short Years; 

  

  //Float-Point Variables 

  //Float data type allows for numbers with decimals 

  float MoneySpent; 

  float MoneyMade; 

  float MoneyLeftOver; 

  float MoneyLeft; 

  float MoneySpentI; 

  float PricePerHour; 

  float startbalance; 

  float Total; 

  float WeekMoneyMade; 

  float CI; 

  

  // Bool Variables 

  bool millionare; //Allows for True / False 

  int main () 

  { 

  // Local Variables 

  int menu; 

  bool done; 

  bool settings_complete; 

  while(done != true) //While Loops 

  { 

  

  //Main Menu 

  cout << "\\t Menu \\t" << endl; 

  cout << "1 - Settings" << endl; 

  cout << "2 - Account Summary" << endl; 

  cout << "3 - Quit" << endl; 

  cin >> menu; //Cins the users choice 

  switch(menu) 

  { 

  case 1: //If the user enters 1 this is executed 

  print_settings(); //Calls the Function 

  settings_complete = true; 

  done=false; //Keeps the loop going 

  break; //Use these between cases 

  

  case 2: //If the user enters 2 this is executed 

  // An If/Else Structure 

  if(settings_complete == true) 

  { 

  system("CLS");//Clears the Screen (stdlib.h) 

  print_calculations(); //Calls the Function 

  print_summary(); //Calls the Function 

  } 

  else //If the first statement is untrue this is executed 

  { 

  system("CLS");//Clears the Screen (stdlib.h) 

  cout << "Please Edit The Settings Before Choosing this options \\n"; 

  cout << endl; 

  } 

  done=false; //Keeps the loop goin 

  break; 

  case 3: //If the user enters 3 this is executed 

  done =true; //Ends the main loop, therefore ending the whole program 

  break; 

  default: //If the user enters any number besides 1,2,3 this is displayed 

  cout << "You Have Enter an Invalid Choice. " << endl; 

  cout << endl; 

  done=false; 

  break; 

  } 

  } 

  

  return 0; //When the compiler reads this statement the program will end 

  } 

  

  //Void Function 

  void print_settings() 

  { 

  // Local Variables 

  bool done; 

  int menu; 

  do 

  { 

  system("CLS"); //Clears the Screen (stdlib.h) 

  cout << "\\t Menu - Settings" << endl; 

  cout << "1 - Edit - Money Per Hour" << endl; 

  cout << "2 - Edit - Hours Worked" << endl; 

  cout << "3 - Edit - Expenditures" << endl; 

  cout << "4 - Edit - Current Interest" << endl; 

  cout << "5 - Edit - Savings Menu" << endl; 

  cout << "6 - Quit" << endl; 

  cin >>menu; 

  

  switch(menu) //Another switch structure 

  { 

  case 1: 

  cout << "How much do you make an hour: "; 

  cin >> PricePerHour; 

  system("CLS");//Clears the Screen (stdlib.h) 

  done=false; 

  break; 

  case 2: 

  cout << "How many hours do you work a week? (1-168): "; 

  cin >> HoursWorked; 

  system("CLS");//Clears the Screen (stdlib.h) 

  

  // Check For Invalid Entrys// || stands for "OR" 

  //So if Hours worked is <= 0 or Hours worked is >= 168 then the statement 

  //Is executed 

  if ((HoursWorked <= 0) || (HoursWorked >= 168)) 

  { 

  cout << "Your Number was Invalid. Please enter a Number between (1-168):"; 

  cin >> HoursWorked; 

  } 

  done=false; 

  break; 

  case 3: 

  cout << "How much money do you plan on spending Weekly? "; 

  cin >> MoneySpent; 

  cout << endl; 

  

  // Check For Invalid Entrys 

  if (0 > MoneySpent) 

  { 

  cout << "Your Number was Invalid. A Positive Number Please: " << "\\n"; 

  cin >> MoneySpent; 

  } 

  done=false; 

  break; 

  case 4: 

  cout << "What is the Current Interest Rate? "; 

  cin >> CI; 

  cout << endl; 

  

  // Check For Invalid Entrys 

  if (0 > CI) 

  { 

  cout << "Your Number was Invalid. A Positive Number Please: " << "\\n"; 

  cin >> CI; //Cins a positive number for Interest rate 

  } 

  done=false; 

  break; 

  case 5: 

  print_savings(); //Calls the Function 

  done=false; 

  cout << endl; 

  break; 

  case 6: 

  done=true; 

  system("CLS");//Clears the Screen (stdlib.h) 

  break; 

  default: 

  cout << "You Have Enter an Invalid Choice. " << endl; 

  done=false; 

  cout << endl; 

  break; 

  } 

  

  }while (done!=true); 

  } 

  void print_savings() 

  { 

  // Local Variables 

  bool done; 

  int menu; 

  int million; 

  do 

  { 

  system("CLS"); 

  cout << "\\t Menu - Savings" << endl; 

  cout << "1 - Years" << endl; 

  cout << "2 - Edit Account Balance" << endl; 

  cout << "3 - Millionare" << endl; 

  cout << "4 - Quit" << endl; 

  cin >> menu; 

  switch(menu) 

  { 

  case 1: 

  cout << "How Many Years would you like to have the total savings calculated for" << endl; 

  cout << "(Any Number Greater then 1, 0 if You dont Wish To use this option): \\n"; 

  cin >> userc; 

  done=false; 

  break; 

  case 2: 

  cout << "What is the Current Balance of Your Account? \\n"; 

  cout << "(Any Number Greater then 1, 0 if You dont Wish To use this option): \\n"; 

  cin >> startbalance; 

  done=false; 

  break; 

  case 3: 

  cout << "Type 1, if you would like to calculate the time needed to become a millionare." << endl; 

  cout << "0 for No." << endl; 

  cin >> million; 

  if (million==1) 

  {millionare=true;} 

  else 

  {millionare=false;} 

  done = false; 

  case 4: 

  done=true; 

  break; 

  default: 

  cout << "You Have Enter an Invalid Choice. " << endl; 

  done=false; 

  break; 

  } 

  } while(done!=true); 

  } 

  void print_calculations() 

  { 

  if (millionare==true) 

  { 

  print_millionare(); 

  } 

  else 

  { 

  // Local Variables 

  int short Y; 

  int short Z; 

  float X; 

  float Xvariable; 

  float Yvariable; 

  //Calculations 

  // Calculates Total Hours Worked During the Year 

  Hours = HoursWorked * 56; 

  Hour = HoursWorked; 

  

  // This then Calculate How Much Money Was made (Weekly &amp; Yearly) 

  MoneyMade = PricePerHour * Hours; 

  WeekMoneyMade = PricePerHour * Hour; 

  

  // Simply calculation the money spent to a yearly value 

  MoneySpentI = MoneySpent * 56; 

  

  // Calculates the Net Profit (Weekly &amp; Yearly) 

  MoneyLeftOver = MoneyMade - MoneySpentI; 

  MoneyLeft= WeekMoneyMade -MoneySpent; 

  

  // Initializes Required Variables For the Following Calculations 

  X = MoneyLeftOver; 

  Z = 0; 

  Y = 0; //This is set to zero because you dont have a salary before the year begins 

  //But because of the loop the value after one loop is increased by 1(Y++) 

  Years = 0; 

  Yvariable = MoneyLeftOver; 

  Xvariable = startbalance; 

  

  // Annual Interest (Salary) 

  do 

  { 

  Yvariable = ((Y * MoneyLeftOver) * CI) + X; 

  Y++; 

  Years++; 

  } 

  while (userc != Y + 1); 

  // Annual Interest (Initial Bank Balance) 

  do 

  { 

  Xvariable = ((Z * startbalance) * CI); 

  Z++; 

  } 

  while (userc != Z + 1); 

  

  // Account Balance plus the sum of the salary balance; 

  Total = Yvariable + Xvariable; 

  } 

  } 

  

  void print_summary() 

  { 

  bool done; 

  int choice; 

  int menu; 

  

  do 

  { 

  cout << "\\t Menu - Account Summary" << endl; 

  cout << "1 - Current Summary" << endl; 

  cout << "2 - Quit" << endl; 

  cin >> menu; 

  system("CLS"); 

  switch(menu) 

  { 

  case 1: 

  cout.setf(ios::fixed); 

  cout << "\\t Summary" << endl;; 

  cout << "Weekly" << endl; 

  cout << "Hours Worked: " << HoursWorked << endl; 

  cout << "Profit: $" << setprecision(2) << MoneyLeft << endl; 

  cout << endl; 

  cout << "Yearly" << endl; 

  cout << "Hours Worked: " << Hours << endl; 

  cout << "Profit: $" << setprecision(2) << MoneyLeftOver << endl; 

  cout << endl; 

  cout << "Savings" << endl; 

  cout << "After " << setprecision(2) << userc << " year(s) of saving, your current balance is: $" << Total << endl; 

  cout << endl; 

  if(millionare==true) 

  { 

  cout << "It Will Take: " << Years << " Years to Save a Million Dollars." << endl; 

  } 

  cout.unsetf(ios::fixed); 

  

  cout << "Return to the Main Menu (1 for Yes, 0 for No): "; 

  cin >> choice; 

  if(choice == 1) 

  { 

  done = true; //Ends the Loop 

  system("CLS");//Clears the Screen (stdlib.h) 

  } 

  else 

  { 

  done = false; //Keeps Loop Going 

  system("CLS");//Clears the Screen (stdlib.h) 

  } 

  

  break; 

  case 2: 

  done=true; 

  system("CLS");//Clears the Screen (stdlib.h) 

  break; 

  default: 

  cout << "You Have Enter an Invalid Choice. " << endl; 

  done=false; 

  break; 

  } 

  } while(done != true); 

  } 

  void print_millionare() 

  { 

  // Local Variables 

  int short Y; 

  int short Z; 

  float X; 

  float Xvariable; 

  float Yvariable; 

  //Calculations 

  // Calculates Total Hours Worked During the Year 

  Hours = HoursWorked * 56; 

  Hour = HoursWorked; 

  

  // This then Calculate How Much Money Was made (Weekly &amp; Yearly) 

  MoneyMade = PricePerHour * Hours; 

  WeekMoneyMade = PricePerHour * Hour; 

  

  // Simply calculation the money spent to a yearly value 

  MoneySpentI = MoneySpent * 56; 

  

  // Calculates the Net Profit (Weekly &amp; Yearly) 

  MoneyLeftOver = MoneyMade - MoneySpentI; 

  MoneyLeft= WeekMoneyMade -MoneySpent; 

  

  // Initializes Required Variables For the Following Calculations 

  X = MoneyLeftOver; 

  Z = 0; //Control Variable for the second statement 

  Y = 0; //Control Variable for the first statement 

  Years = 0; //You dont make money before you begin the year, but after the loop goes around once 

  //The value is increased to 1 

  Yvariable = MoneyLeftOver; //Transfers the value to another variable to prevent a change to the main variable 

  Xvariable = startbalance; //See Above 

  

  // Annual Interest (Salary) 

  do //A do/while loop 

  { 

  Yvariable = ((Y * MoneyLeftOver) * CI) + X; 

  Y++; 

  Years++; 

  } 

  while (userc != Y + 1); 

  // Annual Interest (Initial Bank Balance) 

  do 

  { 

  Xvariable = ((Z * startbalance) * CI); 

  Z++; 

  } 

  while (userc != Z + 1); 

  

  // Account Balance plus the sum of the salary balance; 

  Total = Yvariable + Xvariable; 

  } 

  

  /* Feel free to edit this program as you wish, I wrote it when I began learning C++ 

  if you can improve upon it PM me a new copy, Im planning on creating a headerfile for most of the functions */
Written by Tr. pjd (April 7 2004)

This article was originally published by CyberArmy.net in the CyberArmy Library.

You must be logged in to vote on an article

About Us | Privacy Policy | Mission Statement | Help