Help with typecasting

This is a discussion on Help with typecasting within the C++ Programming forums, part of the General Programming Boards category; The assignment asks to input these values: D = Investment Amount (5000) P = Interest (5.25) Y = Number of ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    11

    Exclamation Help with typecasting

    The assignment asks to input these values:

    D = Investment Amount (5000)
    P = Interest (5.25)
    Y = Number of Years (2.25)
    M = Conversions/Year (4)

    It tells me to use float for interest, and int for the rest.
    I have to use this formula to calculate the Maturity value (S)
    S = D ( 1 + P/M) to the power of YM

    Using the data above S is suppose to equal 5622.60
    When I debug I get 5065.62.

    I think I have made an error in typecasting int to double or float to double

    Code:
    // MaturityCalc.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int m, y, d;
    	double s;
    	float p;
    
    	//keyboard input
    	cout << "Please input the following information";
    	cout << endl;
    	cout << "Investment Amount: ";
    	cin >> d;
    	cout << endl;
    	cout << "Interest Rate: ";
    	cin >> p;
    	cout << endl;
    	cout << "Conversions/Year: ";
    	cin >> m;
    	cout << endl;
    	cout << "Number of Years: ";
    	cin >> y;
    	cout << endl;
    	//end keyboard entry
    	
    	y = y * m;
    	p = 1 + (p/100)/m;
    	pow ((double) p , (double)y);
    	s = d * p;
    	cout << "This is the result: " << (double)s;
    
    	system ("pause");
    
    	return 0;
    }

    Note: This is my second assignment, first week of C++, I have very little knowledge of this.

    Any help is appreciated.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    If you have 2.25 years, then years needs to be a float or double.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    Thanks for that, it fixed a glitch that was skipping y, but I'm still getting the same answer.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Are you getting (and ignoring) any compiler warnings?

    Did you fix the data type or just cast around it?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    No compiler errors. I'm not sure what your second comment means, sorry.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Post your code please.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    Code:
    // MaturityCalc.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int m, d;
    	double s;
    	float p, y;
    
    	//keyboard input
    	cout << "Please input the following information";
    	cout << endl;
    	cout << "Investment Amount: ";
    	cin >> d;
    	cout << endl;
    	cout << "Interest Rate: ";
    	cin >> p;
    	cout << endl;
    	cout << "Conversions/Year: ";
    	cin >> m;
    	cout << endl;
    	cout << "Number of Years: ";
    	cin >> y;
    	cout << endl;
    	//end keyboard entry
    	
    	y = y * m;
    	p = 1 + (p/100)/m;
    	pow ((double) p , (double)y);
    	s = d * p;
    	cout << "This is the result: " << (double)s;
    
    	system ("pause");
    
    	return 0;
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Were you planning to store the result of the pow anywhere handy?

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    Haha, that was it! Thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recv() typecasting error
    By jmd15 in forum Networking/Device Communication
    Replies: 7
    Last Post: 05-19-2006, 03:32 PM
  2. c and c++ style typecasting confusion
    By vaibhav in forum C++ Programming
    Replies: 14
    Last Post: 08-30-2005, 07:29 AM
  3. typecasting
    By sreetvert83 in forum C++ Programming
    Replies: 7
    Last Post: 07-22-2005, 01:55 PM
  4. typecasting
    By JaWiB in forum C++ Programming
    Replies: 14
    Last Post: 06-01-2003, 10:42 PM
  5. typecasting?
    By tammo21 in forum C Programming
    Replies: 6
    Last Post: 08-17-2002, 04:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21