Need help with C++ program code

Converting my C++ procedural code to Object Oriented

Page 1 of 1

7 Replies - 1190 Views - Last Post: 07 September 2008 - 02:46 AM Rate Topic: -----

#1 gixxerman03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 08-March 08

Need help with C++ program code

Post icon  Posted 06 September 2008 - 08:46 AM

Can some one help me with converting my C++ procedural code to Object Oriented? I am at a stand still with my code. I have to include classes and methods in my code. I created a class already. Any help will be very appreciated.

Here is my code:

<code>

// Standard Library Type
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>

// Declaration for namespace standard (std)
using namespace std;

// Variable for Selection option
char selection = ' ';

// Boolean statement for loop
bool finish = false;

// Declare the calculator class
class calculator{
public:
int principle;
int yrs;
double ir;
double monthlyinterestrate;

};
int main(){
calculator loanpayment; // Create a claculator object
double monthlypayment;

//Assign values to fields in loanpayment
loanpayment.principle = 0;
loanpayment.yrs = 0;
loanpayment.ir = 0;
loanpayment.monthlyinterestrate = loanpayment.ir*100;

// Calcualation for loan monthly payment
monthlypayment = loanpayment.principle * (loanpayment.ir/1200)/(1-pow(1+(loanpayment.ir/1200),-1*(loanpayment.yrs*12)));
do{

// output the results (followed by a new line)
cout << " Mortgage Calculator" << endl;
cout << " Version 1.0.0\n" << endl;
cout << " ////////////////////////////////////////////////////////////////////////// " << endl;
cout << " // // " << endl;
cout << " // Calculator will Calculate and display the mortgage payment amount // " << endl;
cout << " // using the amount of the mortgage, the term of the mortgage, and the // " << endl;
cout << " // interest rate of the mortgage as input by the user. Allow user to // " << endl;
cout << " // loop back and enter new data or quit. // " << endl;
cout << " ////////////////////////////////////////////////////////////////////////// " << endl;
cout << "\n";
cout << "\n";
cout << "Enter Principle of the loan: $"; // prompt user for loan amount
cin >> loanpayment.principle;
cout << "\n";

cout << "Enter interestrate for your loan: "; // prompt user for interestrate for loan
cin >> loanpayment.ir;
cout << "\n";

cout << "Enter the term in yrs: "; // prompt term for loan
cin>> loanpayment.yrs;
cout << "\n";

//perform calculation to find mortgage payment
monthlypayment = loanpayment.principle * ((loanpayment.ir/100)/12) / ( 1.0 - 1.0/pow((1.0 + ((loanpayment.ir/100)/12)), (loanpayment.yrs*12)));
cout << "Your monthly payment is: $" << monthlypayment << endl; // print result
cout <<"\n";
// prompt user to calculate another loan
cout << "Would you like to calculate another loan principle? Y/N ";
cin >> selection;
// This loops prompts user to select to enter new data for loan mortgage or end progam
if (selection == 'Y' || selection == 'y')
finish = true;
else if (selection == 'N' || selection == 'n') {
cout << endl;
continue;
}
else {
cout << "You entered " << selection << " which is not a valid command.\n";
cout << "Please enter either 'Y' or 'N'\n\n";
}
cout << "\n";
cout<<"Thank you for using my mortgage calculator"; // Thanks you message to users
cout<<"\n";

cout <<"Please come again"; // message to user to come again
cout <<"\n";
cout <<"\n";
cout <<"\n";


}while (finish);
// Allow system to pause for user
system ("PAUSE");
return 0;
// End of program
}


</code>

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with C++ program code

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,437
  • Joined: 23-August 08

Re: Need help with C++ program code

Posted 06 September 2008 - 09:13 AM

Why, after already making 18 posts prior to this one, did you ignore the required use of [CODE] tags?

After you've rectified that, please tell us what is the problem you are experiencing so that we can help.
Was This Post Helpful? 0
  • +
  • -

#3 gixxerman03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 08-March 08

Re: Need help with C++ program code

Posted 06 September 2008 - 09:25 AM

[quote name='JackOfAllTrades' post='413775' date='6 Sep, 2008 - 09:13 AM']
Why, after already making 18 posts prior to this one, did you ignore the required use of
 tags?

After you've rectified that, please tell us what is the problem you are experiencing so that we can help.
[/quote]


[quote name='JackOfAllTrades' date='6 Sep, 2008 - 09:13 AM' post='413775']
Why, after already making 18 posts prior to this one, did you ignore the required use of [code] tags?

After you've rectified that, please tell us what is the problem you are experiencing so that we can help.
[/quote]


Sorry about that! I thought I added the code tags. Can some one help me with converting my C++ procedural code to Object Oriented? I am at a stand still with my code. I have to include classes and methods in my code. I created a class already. Any help will be very appreciated.



[code]
// Standard Library Type
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>

// Declaration for namespace standard (std)
using namespace std;

// Variable for Selection option
char selection = ' ';

// Boolean statement for loop
bool finish = false; 

// Declare the calculator class
class calculator{
public:
int principle;
int yrs;
double ir;
double monthlyinterestrate;

};
int main(){
calculator loanpayment; // Create a claculator object
double monthlypayment;

//Assign values to fields in loanpayment
loanpayment.principle = 0;
loanpayment.yrs = 0;
loanpayment.ir = 0;
loanpayment.monthlyinterestrate = loanpayment.ir*100;

// Calcualation for loan monthly payment 
monthlypayment = loanpayment.principle * (loanpayment.ir/1200)/(1-pow(1+(loanpayment.ir/1200),-1*(loanpayment.yrs*12)));
do{ 

// output the results (followed by a new line)
cout << " Mortgage Calculator" << endl;
cout << " Version 1.0.0\n" << endl;
cout << " ////////////////////////////////////////////////////////////////////////// " << endl;
cout << " // // " << endl;
cout << " // Calculator will Calculate and display the mortgage payment amount // " << endl;
cout << " // using the amount of the mortgage, the term of the mortgage, and the // " << endl;
cout << " // interest rate of the mortgage as input by the user. Allow user to // " << endl;
cout << " // loop back and enter new data or quit. // " << endl;
cout << " ////////////////////////////////////////////////////////////////////////// " << endl;
cout << "\n";
cout << "\n";
cout << "Enter Principle of the loan: $"; // prompt user for loan amount
cin >> loanpayment.principle;
cout << "\n";

cout << "Enter interestrate for your loan: "; // prompt user for interestrate for loan
cin >> loanpayment.ir;
cout << "\n";

cout << "Enter the term in yrs: "; // prompt term for loan
cin>> loanpayment.yrs; 
cout << "\n";

//perform calculation to find mortgage payment
monthlypayment = loanpayment.principle * ((loanpayment.ir/100)/12) / ( 1.0 - 1.0/pow((1.0 + ((loanpayment.ir/100)/12)), (loanpayment.yrs*12)));
cout << "Your monthly payment is: $" << monthlypayment << endl; // print result
cout <<"\n"; 
// prompt user to calculate another loan
cout << "Would you like to calculate another loan principle? Y/N ";
cin >> selection;
// This loops prompts user to select to enter new data for loan mortgage or end progam
if (selection == 'Y' || selection == 'y')
finish = true;
else if (selection == 'N' || selection == 'n') {
cout << endl;
continue;
}
else {
cout << "You entered " << selection << " which is not a valid command.\n";
cout << "Please enter either 'Y' or 'N'\n\n";
}
cout << "\n";
cout<<"Thank you for using my mortgage calculator"; // Thanks you message to users
cout<<"\n";

cout <<"Please come again"; // message to user to come again 
cout <<"\n";
cout <<"\n";
cout <<"\n";


}while (finish);
// Allow system to pause for user 
system ("PAUSE");
return 0;
// End of program
}



Was This Post Helpful? 0
  • +
  • -

#4 gixxerman03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 08-March 08

Re: Need help with C++ program code

Posted 06 September 2008 - 09:34 AM

Sorry about that! I thought I added the code tags. Can some one help me with converting my C++ procedural code to Object Oriented? I am at a stand still with my code. I have to include classes and methods in my code. I created a class already. Any help will be very appreciated.



// Standard Library Type
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>

// Declaration for namespace standard (std)
using namespace std;

// Variable for Selection option
char selection = ' ';

// Boolean statement for loop
bool finish = false; 

// Declare the calculator class
class calculator{
public:
int principle;
int yrs;
double ir;
double monthlyinterestrate;

};
int main(){
calculator loanpayment; // Create a claculator object
double monthlypayment;

//Assign values to fields in loanpayment
loanpayment.principle = 0;
loanpayment.yrs = 0;
loanpayment.ir = 0;
loanpayment.monthlyinterestrate = loanpayment.ir*100;

// Calcualation for loan monthly payment 
monthlypayment = loanpayment.principle * (loanpayment.ir/1200)/(1-pow(1+(loanpayment.ir/1200),-1*(loanpayment.yrs*12)));
do{ 

// output the results (followed by a new line)
cout << " Mortgage Calculator" << endl;
cout << " Version 1.0.0\n" << endl;
cout << " ////////////////////////////////////////////////////////////////////////// " << endl;
cout << " // // " << endl;
cout << " // Calculator will Calculate and display the mortgage payment amount // " << endl;
cout << " // using the amount of the mortgage, the term of the mortgage, and the // " << endl;
cout << " // interest rate of the mortgage as input by the user. Allow user to // " << endl;
cout << " // loop back and enter new data or quit. // " << endl;
cout << " ////////////////////////////////////////////////////////////////////////// " << endl;
cout << "\n";
cout << "\n";
cout << "Enter Principle of the loan: $"; // prompt user for loan amount
cin >> loanpayment.principle;
cout << "\n";

cout << "Enter interestrate for your loan: "; // prompt user for interestrate for loan
cin >> loanpayment.ir;
cout << "\n";

cout << "Enter the term in yrs: "; // prompt term for loan
cin>> loanpayment.yrs; 
cout << "\n";

//perform calculation to find mortgage payment
monthlypayment = loanpayment.principle * ((loanpayment.ir/100)/12) / ( 1.0 - 1.0/pow((1.0 + ((loanpayment.ir/100)/12)), (loanpayment.yrs*12)));
cout << "Your monthly payment is: $" << monthlypayment << endl; // print result
cout <<"\n"; 
// prompt user to calculate another loan
cout << "Would you like to calculate another loan principle? Y/N ";
cin >> selection;
// This loops prompts user to select to enter new data for loan mortgage or end progam
if (selection == 'Y' || selection == 'y')
finish = true;
else if (selection == 'N' || selection == 'n') {
cout << endl;
continue;
}
else {
cout << "You entered " << selection << " which is not a valid command.\n";
cout << "Please enter either 'Y' or 'N'\n\n";
}
cout << "\n";
cout<<"Thank you for using my mortgage calculator"; // Thanks you message to users
cout<<"\n";

cout <<"Please come again"; // message to user to come again 
cout <<"\n";
cout <<"\n";
cout <<"\n";


}while (finish);
// Allow system to pause for user 
system ("PAUSE");
return 0;
// End of program
}



Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,437
  • Joined: 23-August 08

Re: Need help with C++ program code

Posted 06 September 2008 - 10:17 AM

1. It's principal, not principle.

2. Make all member variables private, not public.

3. Create a constructor in the class to initialize all member variables to 0.

4. Add methods to set and get member variables.

5. Add a method to calculate the mortgage payment from values in member variables.
Was This Post Helpful? 0
  • +
  • -

#6 gixxerman03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 08-March 08

Re: Need help with C++ program code

Posted 06 September 2008 - 11:02 AM

View PostJackOfAllTrades, on 6 Sep, 2008 - 10:17 AM, said:

1. It's principal, not principle.

2. Make all member variables private, not public.

3. Create a constructor in the class to initialize all member variables to 0.

4. Add methods to set and get member variables.

5. Add a method to calculate the mortgage payment from values in member variables.



Thanks for your Help! Can you provide me some examples to go by?
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,437
  • Joined: 23-August 08

Re: Need help with C++ program code

Posted 06 September 2008 - 05:22 PM

There's a link at the top of the page called Tutorials. Click on it, choose C++ tutorials, and start reading. That, or there's always Google.
Was This Post Helpful? 0
  • +
  • -

#8 RedSonja  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 172
  • Joined: 04-September 08

Re: Need help with C++ program code

Posted 07 September 2008 - 02:46 AM

The change to the object-oriented paradigm is in your head, not in your code. It is not something you achieve by inserting a few lines, you have to start again. You need to do a course, even an online one will do.

Hint: if you already keep the teapot in the same cupboard as the tea you are on the right track.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1