Click here to Skip to main content
15,882,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I was trying to do the following problem but I keep getting the error of invalid operands of type double and double to binary operator. I've switched everything I can think of with my super-limited abilities and still can't fix the error. Can anyone help me? (I posted the question at the very bottom that I am doing the code for). Thank you very much to anyone who can help

*edit* I noticed when i switch the double with int the code works but i need to be able to use decimal points.the same error happens to me when i use float


C++
#include <cstdlib>
#include <iostream>
using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    double principal1;
    double rate;
    double T;
    double amount;
    cout << "Enter principal1: ";
    cin >> principal1;
    cout << "Enter rate: ";
    cin >> rate;
    cout << "Enter times compounded annually: ";
    cin >> T;
    amount=(principal1)*((1+(rate/T))^T);
    cout << "The total amount is : " << amount << endl;
    
    return 0;
}



Assuming there are no deposits other than the original investment, the balance in a savings account after one year may be calculated as
Amount = Principal*(1 + Rate/T)^ T
Principal is the balance in the savings account, Rate is the interest rate, and T is the number of times the interest is compounded during a year (T is 4 if the interest is compounded quarterly).
Write a program that asks for the principal, the interest rate, and the number of times the interest is compounded. It should display a report similar to
Programming Challenges 145
Interest Rate:4.25
Times Compounded: 4
Principal:1000
Interest:43.34
Amount in Savings:1043.34
Posted
Updated 13-Sep-15 9:34am
v5
Comments
Alan N 13-Sep-15 15:17pm    
HINT: 1) In the question what does the ^ operator do? 2) In C++ what does the ^ operator do?
Sergey Alexandrovich Kryukov 13-Sep-15 15:29pm    
Reading a reference could way faster than asking a question...
—SA

You did not show the line where the compilation error is. There is an error in the line amount=(principal1)*((1+(rate/T))^T); you cannot use XOR ^ operator here. If you meant to use power function, please use: http://www.cplusplus.com/reference/cmath/pow[^].

See also:
https://msdn.microsoft.com/en-us/library/3akey979.aspx[^].

It's very counter-productive to write code without reading on the basics of the language you use. Isn't it so easy to consult some reference Web page (MSDN, in this case) or a book? My advice is to get to programming seriously.

—SA
 
Share this answer
 
v4
Comments
CPallini 13-Sep-15 15:35pm    
Indeed. 5.
Please note, there's no need of System.Math.Pow, standard C++ provides the pow function.
Sergey Alexandrovich Kryukov 13-Sep-15 22:28pm    
Thank you, Carlo.
Sure, Afzaal already fixed my two links.
—SA
Afzaal Ahmad Zeeshan 13-Sep-15 16:43pm    
5ed; good catch.

Plus, I made the change as to what Carlo has said above and the program is also C++ so I changed the function call, resource links. Hope, I didn't mess up with the quality of your answer. :-)
Sergey Alexandrovich Kryukov 13-Sep-15 22:28pm    
Thank you very much, Afzaal, especially for fixing my references; I only streamlined the phrasing a bit,
—SA
Wendelius 13-Sep-15 23:22pm    
Good eyes :)
Thank you , i switched my code to the following and made it work. I was unfamiliar with exponents and now I'm good with them


#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;

/*
*
*/
int main() {
float principal1; //starting principal
float rate; //interest rate
float T; //times compounded annually
float amount; //final amount in account
float helper;
cout << "Enter principal1: ";
cin >> principal1;
cout << "Enter rate: ";
cin >> rate;
cout << "Enter times compounded annually: ";
cin >> T;
helper=(1+(rate/T)); /*I didn't know how to properly use exponents with this so i made a helper variable thing */
amount=(principal1)* pow(helper,T);
cout << "The balance after a year is : " << amount << endl;

return 0;
}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 13-Sep-15 22:31pm    
How could you consider it as "answer"? and even self-accepted it? Better remove it, or move to some comment to the question, to avoid abuse reports and down-votes...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900