9 Replies - 2180 Views - Last Post: 07 February 2011 - 11:25 AM Rate Topic: -----

#1 J-e-L-L-o  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 204
  • Joined: 23-January 11

Help with interest accrued program

Posted 03 February 2011 - 07:05 PM

I am getting an error at line 36. dont know why.

/* 3_16 calculating interest earned 
Write a program that asks for principal, interest rate, # of times compounded,
and calculate interest earned. 
*/

#include<iostream>
#include<cmath> 			// Needed for pow function
#include<iomanip>			// Need for manipulating text
using namespace std;

int main()
{
	float amount, principal, rate, interest, calc_rate;	// Declare variables
	double time; 
	
	// Ask for inputs. 
	cout << "This program calculates interest earned.\n";
	cout << "What is the principal (beginning balance)? \n";
	cin  >> principal;
	cout << "What is the interest rate in percent? \n";
	cin  >> rate;
	cout << "How many times a year will the principal be compounded? \n";
	cin  >> time;
	
	// Limit deciamals to 4.
	cout << setprecision(4) << fixed;
	
	// Convert rate to decimal for calculations. 
	rate *= 1 / 100.0;
	
	// Make interest rate constant throughout program.
	const float RATE = rate;
	
	// Calculate parenthesis first 
	calc_rate = 1 + (RATE / time);		// Inside
	calc_rate =  double pow(calc_rate, time);	// Inside raised to Time
	
	// Calculate amount
	amount = principal * calc_rate;
	
	// Calculate interest
	interest = amount - principal;
	
	// Output the results. 
	cout << "Here are the results:\n";
	cout << endl;
	cout << setprecision(0) << fixed;
	
	// Interest rate.
	cout << left << "Interest rate:" << setw(30) << right 
	     << RATE << endl;
	
	// Time. 
	cout << left << "Number of times compounded in a year:" << setw(30) << right
		 << time << endl;
	
	// Output following to 2 decimals. 
	cout << setprecision(2) << fixed;
	
	// Output principal
	cout << left << "Principal:" << setw(30) << right
	 	 << principal << endl;
  	
  	// Interest.
  	cout << left << "Interest earned:" << setw(30) << right
	 	 << interest << endl;
	
	// Amount in savings. 
	cout << left << "Amount in savings:" << setw(30) << right
	     << amount << endl;
	     
    return 0;
}


	
	


error says: expected primary expression before double
and : expected ; before double.


what am I doing wrong?
Thanx

Is This A Good Question/Topic? 0
  • +

Replies To: Help with interest accrued program

#2 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Help with interest accrued program

Posted 03 February 2011 - 07:13 PM

Take out the 'double' on line 36.

When you call functions you don't use the return type.

answer = pow(number, exponent);
Was This Post Helpful? 2
  • +
  • -

#3 J-e-L-L-o  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 204
  • Joined: 23-January 11

Re: Help with interest accrued program

Posted 03 February 2011 - 09:17 PM

thanx. mixing floats and double was creating some havoc when i started doing operations.
Was This Post Helpful? 1
  • +
  • -

#4 J-e-L-L-o  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 204
  • Joined: 23-January 11

Re: Help with interest accrued program

Posted 06 February 2011 - 05:13 PM

View PostJ-e-L-L-o, on 03 February 2011 - 08:17 PM, said:

thanx. mixing floats and double was creating some havoc when i started doing operations.


Here is the fixed code if anyone is interested:

/* 3_16 calculating interest earned 
Write a program that asks for principal, interest rate, # of times compounded,
and calculate interest earned. 
*/

#include<iostream>
#include<cmath> 			// Needed for pow function. 
#include<iomanip>			// Need for manipulating text. 
using namespace std;

int main()
{
	double amount, principal, rate, interest, calc_rate;	// Declare variables
	double time; 
	
	// Ask for inputs. 
	cout << "This program calculates interest earned.\n";
	cout << "What is the principal (beginning balance)? \n";
	cin  >> principal;
	cout << "What is the interest rate in percent? \n";
	cin  >> rate;
	cout << "How many times a year will the principal be compounded? \n";
	cin  >> time;
	
	// Limit deciamals to 4.
	cout << setprecision(4) << fixed;
	
	// Convert rate to decimal for calculations. 
	rate *= 1.0 / 100;
	
	// Make interest rate constant throughout program.
	const double RATE = rate;
	
	// Calculate parenthesis first 
	calc_rate = 1 + (RATE / time);		// Inside
	calc_rate =  pow(calc_rate, time);	// Inside raised to Time
	
	// Calculate amount
	amount = principal * calc_rate;
	
	// Calculate interest
	interest = amount - principal;
	
	// Output the results. 
	cout << "Here are the results:\n";
	cout << endl;
	
	
	// Interest rate.
	cout << setprecision(2) << fixed << left << setw(40) << "Interest rate:" << right << setw(10)
	     << RATE * 100 << "%\n";		// Converts decimal to whole number
	
	// Time in integer number only. 
	cout << setprecision(0) << fixed << left << setw(40) << "Number of times compounded in a year:"
	     << right << setw(10) << time << endl;
	
	// Output following to 2 decimals. 
	cout << setprecision(2) << fixed;
	
	// Output principal
	cout << left << setw(40) << "Principal:" << right 
		 << setw(10) << principal << endl;
  	
  	// Interest.
  	cout << left << setw(40) <<  "Interest earned:" << right 
  		 << setw(10) << interest << endl;
	
	// Amount in savings. 
	cout << left << setw(40) << "Amount in savings:" << right
		 << setw(10) << amount << endl;
	     
    return 0;
}

This post has been edited by JackOfAllTrades: 06 February 2011 - 08:21 PM
Reason for edit:: Fiixed code tags

Was This Post Helpful? 0
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Help with interest accrued program

Posted 06 February 2011 - 09:04 PM

For a next version, you might like to look at validating your input and handling bad input so your program doesn't crash.
You may also like to consider using an int variable to hold input that will be integers, like the number of compounding periods.
You may like to use some functions if you do data validation at input.
You may eventually want to look at a Money class to handle rounding and then exact calculations and reporting that always agree to the exact cent.
(You don't really need a class to do this for amounts that are small enough to be handled exactly as integer cents.)
There may be links in the snippets you would like to see. I'll see if I can find some ...

An example of a simple approach to a start to validating data input ...
http://www.dreaminco...tion-some-more/

An example that gives a start to a money class
http://www.dreaminco...-class-account/

For future ref.
http://developers-he...index.php/topic,106.msg706.html#msg706
http://developers-heaven.net/forum/index.php/topic,106.msg706.html#msg706

This post has been edited by David W: 06 February 2011 - 09:48 PM

Was This Post Helpful? 1
  • +
  • -

#6 J-e-L-L-o  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 204
  • Joined: 23-January 11

Re: Help with interest accrued program

Posted 06 February 2011 - 09:42 PM

View PostDavid W, on 06 February 2011 - 08:04 PM, said:

For a next version, you might like to look at validating your input and handling bad input so your program doesn't crash.
You may also like to consider using an int variable to hold input that will be integers, like the number of compounding periods.
You may like to use some functions if you do data validation at input.
You may eventually want to look at a Money class to handle rounding and then exact calculations and reporting that always agree to the exact cent.
(You don't really need a class to do this for amounts that are small enough to be handled exactly as integer cents.)
There may be links in the snippets you would like to see. I'll see if I can find some ...

An example of a simple approach to a start to validating data input ...
http://www.dreaminco...tion-some-more/

An example that gives a start to a money class
http://www.dreaminco...-class-account/


thank you for the help but I am just beginning. I have not dealt with classes. I haven't encountered a money class or currency variable. I did validate input to see if it would run and it gave me the correct amounts. i used 1000 for the principle, 12 for the number of months, and 4.25 for the interest rate. This is a simple program that I wrote that is based on the skills that I have learned in class only.

I chose to use double for the number of months for future use; lets say I didn't specify whole numbers only. That way a user can input 3.5 for example. Not likely but I would like the option to be there if someone had an off case of amortization. I also chose double to not have to worry about static casting in the calculations. (although double has order of precedence over int). I reasoned it would give the program more flexibility without limiting the user to whole number of months.

Thanx for the criticism and I am willing to accept any faults. I'm down to learn the good stuff before I know better lol.
Was This Post Helpful? 0
  • +
  • -

#7 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Help with interest accrued program

Posted 06 February 2011 - 10:04 PM

I thought you might have anticipated decimal value periods. But in your output, do you want to strip off any decimals then?

I appreciate you are keen to learn, as you asked for comments. So the suggestions were for next steps.
Your final program, as above, will crash if non-numeric data is entered accidentally before any numeric data is entered ...
And it leaves any non-numeric data that accidentally might have been entered after numeric characters, in the cin input stream, so then, a next call for numbers by cin will crash the program.
Take a look at the first link above to see a simple way to handle both these real data entry situations that beginners may appreciate seeing early to avoid much pain in their programs that crash on some input unawares ...
Was This Post Helpful? 0
  • +
  • -

#8 J-e-L-L-o  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 204
  • Joined: 23-January 11

Re: Help with interest accrued program

Posted 06 February 2011 - 10:14 PM

you are right about the output. i truncated back to no decimals. I forgot to change that.
i understand the validating numeric input. I will have to brush up on that. I have limited knowledge of cin.get and cin.getline. and none of cin.sync.

Thanx. I havent incorporated arrays to limit program input yet in my simple programing examples to prevent buffer and keyboard overruns :donatello:
Was This Post Helpful? 0
  • +
  • -

#9 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Help with interest accrued program

Posted 07 February 2011 - 11:11 AM

View PostJ-e-L-L-o, on 07 February 2011 - 12:14 AM, said:

you are right about the output. i truncated back to no decimals. I forgot to change that.
i understand the validating numeric input. I will have to brush up on that. I have limited knowledge of cin.get and cin.getline. and none of cin.sync.

Thanx. I havent incorporated arrays to limit program input yet in my simple programing examples to prevent buffer and keyboard overruns :donatello:


int n;
cout << "Enter an int: " << flush;
cin >> n;


There is really a lot going on when the above is executed in a running program.
If the cin input stream buffer is 'empty' or contains only 'whitespace' char's ...
The running program will get char's from the input device into the input buffer until it is signaled to stop (for example, the enter key was pressed)
Then it will process those char's, skipping over any leading 'whitespace' char's
It will build up your integer, char by char, as long as valid char's ... but then stops and leaves in the input stream what remains, which is most commonly the whitespace newline char(s)
There is more, but this may give you a 'start'
Google cin.good() and cin.sync() ... and write and test some code that use them.
Was This Post Helpful? 0
  • +
  • -

#10 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Help with interest accrued program

Posted 07 February 2011 - 11:25 AM

Also ... as early as you can, look up and use
C++ string
And the very useful function calls ...
getline( cin, inputStr );
getline( cin, inputStrToken, delimitChar );

Seeing and using these early, will give you a big boost in ease of programming in C++
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1