CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2015
    Posts
    3

    Savings Calculator Help!

    I am very new to C++ and im having a hard time with this program. Attached is what im suppose to do and what its suppose to look like below is my source code.

    assignment1.docxassignment1.docx
    #include <iostream>
    #include <iomanip>
    #include <cmath>

    using namespace std;

    int main()
    {
    string firstName;
    double principal;
    double annualRate;
    double year;
    double balance;
    double maxYear = 20;



    cout << "What is your name? ";
    cin >> firstName;

    cout << "How much do you want to initially put into savings? ";
    cin >> principal;

    cout << "What is the annual interest rate as a percentage? ";
    cin >> annualRate;

    cout << "How many years do you want to print out? ";
    cin >> year;




    do
    {
    cout << "Daily " << endl;
    {
    balance = principal *pow(1 + annualRate, year)/365;

    cout << fixed << "$" << setprecision(0);

    cout << setprecision(2) << balance << endl;
    }
    year += 1;
    }
    while (year <= maxYear);

    do
    {
    cout << " Monthly" << endl;
    {
    balance = principal *pow(1 + annualRate, year)/12;

    cout << fixed << "$" << setprecision(0);

    cout << setprecision(2) << balance << endl;
    }
    year += 1;
    }
    while (year <= maxYear);

    do
    {
    cout << "Annually " << endl;

    {
    balance = principal *pow(1 + annualRate, year);

    cout << fixed << "$" << setprecision(0);

    cout << setprecision(2) << balance << endl;
    }
    year += 1;
    }
    while (year <= maxYear);



    return 0;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Savings Calculator Help!

    When posting code, please use code tags. Go Advanced, select the formatted code and click '#.

    and your question regarding the code is??

    You have an issue concerning the use of variable year. If you trace through the program using the debugger (or on paper if you haven't yet been introduced to the debugger), you'll see.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jun 2015
    Posts
    3

    Re: Savings Calculator Help!

    My question now is; How do I get it to take the year that the user inputs and print that out instead of printing out the parameters I have set.


    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
    	string name;
    	double principal, year, rate, balance;
    	double maxYear = 20;
    	
    	
    	
    	cout << "What is your first name? " << fixed;
    	cin >> name;
    	
    	cout << "Well, " << name << ", I'm going to ask you some questions." << fixed << endl << endl;
    	
    	cout << "How much do you want to initially put into savings? ";
    	cin >> principal;
    	
    	cout << "What is the annual interest rate as a percentage? ";
    	cin >> rate;
    	
    	cout << "How many years do you want to print out? ";
    	cin >> year;
    	
    	cout << setprecision(year) << fixed; 
    	//print header for table
    	for (float year=1; year <2; year++ )
    	{
    		cout << "YEAR	" << "DAILY		" << "MONTHLY	        " << "ANNUALLY		" << endl;
    	}
    	//print some dashes
    	cout <<  "------------------------------------------------------------------" << endl;
    	 
    	 cout << setprecision(year) << fixed; 
    	 for (float i=1; i<21; i++)
    	 {
    	
    	
    		cout  << setprecision(0)  << i;
    	
    	cout << endl;
    	}
    }

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Savings Calculator Help!

    Your code doesn't compile for me, but issues I see,

    Variables used to control loops should be integral types. Don't use floating point in loops

    The variable "year" in your for loop is overriding the scope of the variable "year" defined in main. Use unique variable names.

    Why do you even have a for loop for year. It's only going to execute once. You don't need a loop.

    What are you trying to do with your final for loop?

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Savings Calculator Help!

    Code:
    cout << setprecision(year) << fixed;
    This doesn't seem right to set the precision to the number of years to print??
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Jun 2015
    Posts
    3

    Re: Savings Calculator Help!

    Okay so I have changed a bit. My next question is how do I get it to print the output from the formula so it goes year by year. anything helps thank you.


    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
    	string name;
    	double principal; 
    	double rate;
    	double balance;
    	double year;
    	double maxYear = 20;
    	double daily, monthly, annually;
    	
    	int count;
    	
    	
    	
    	cout << "What is your first name? " << fixed;  //Name of User
    	cin >> name;
    	
    	cout << "Well, " << name << ", I'm going to ask you some questions." << fixed << endl << endl;
    	
    	cout << "How much do you want to initially put into savings? ";  // initial amount put into savings
    	cin >> principal;
    	 if (principal < 1 || principal > 10000)
    	                do{
    	                    cout<<"Please enter a initial amount between $1 and $10,000";
                       cout<<"\n"<<endl;
                       cout<<"How much do you want to initially put into savings? ";
                       cin>>principal;
                       cout<<"\n";
                    }while (principal < 100 || principal > 10000);
    	
    	cout << "What is the annual interest rate as a percentage? ";  // interest rate
    	cin >> rate;
    	if (rate < 1 || rate > 100)
                    do{
    	                    cout<<"Please enter an Annual Interest Rate between 1 and 100";
    	                    cout<<"\n"<<endl;
    	                    cout<<"Enter the Annual Interest Rate";
    	                    cin>>rate;
    	                    cout<<"\n";
    	                }while (rate < 1 || rate > 100);
    	
    	cout << "How many years do you want to print out? "; // years to be printed
    	cin >> year;
    	if (year < 1 || year >= 20)
    		do {
    			cout << "please enter year between 1 and 20";
    			cout <<"\n"<< endl;
    			cout << "How many years do you want to print? ";
    			cin >> year;
    			cout << "\n";
    			}while (year <1 || year >= 20);
    	
    	cout << endl; // space between end of questions and table
    	
    	//print header for table
    		cout << "YEAR	" << "DAILY		"  << "MONTHLY	        "  << "ANNUALLY		"   << endl;
    	//print some dashes
    	cout <<  "------------------------------------------------------------------" << endl;
    	 //print numbers from users input for years shown 
    	 for (count = 1; count<=year; count++)
    	{
    		
    		cout << count;
    		
    		cout << endl;
    	}
    	if (year <1 || year >=20)
    	do{
    		cout << balance = principal *pow(rate, year);
    		cout << balance << fixed << endl;
    		} while (year < 1 || year >= 20);
    }

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Savings Calculator Help!

    You are missing a #include for the string class.

    Code:
    cout << balance = principal *pow(rate, year);
    This is not valid. As you are displaying the value of balance in the next statement, try
    Code:
    balance = principal *pow(rate, year);
    The calculation code needs to be within the for loop, not outside the for loop dependent upon an if statement whose condition should never be true
    Code:
    if (year <1 || year >=20)
    ???

    Your input code although checking for an invalid value doesn't consider that the input entered may not be numeric which would cause a 'fail' state to occur on the input stream. A possible way of coding this for the initial amount is
    Code:
    	while ((cout << "How much do you want to initially put into savings? ") && (!(cin >> principal) || cin && (principal < 1 || principal > 10000))) {
                 if (!cin) {
    			cin.clear();
    			cin.ignore(1000, '\n');
    		}
    
    		cout << "Please enter a initial amount between $1 and $10,000" << endl;
    	}
    Similarly for the other inputs.

    Also as GCDEF stated in post #4, variables used to control loops should be integral types (eg int).
    Last edited by 2kaud; June 30th, 2015 at 06:30 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Savings Calculator Help!

    You have two different tests for the principal amount. The first time you're testing < 1 and the second < 100. You should be consistent there.

    When testing for years, you're using >= 20 but prompting for a value between 1 and 20. Using >= instead of > will flag 20 as an error.

    Your for loop doesn't output anything but the count.

    As mentioned, you want your values output in the for statement.

    Even if it were logically correct, your final if statement could never execute as you've already ensured values between 1 and 20 for year.

    If you did get to your final loop, it would never terminate as you never change the value of year to make the condition false.

    Programming doesn't lend itself to guessing. I know it's hard at first, but you really need to know what each and every statement does. Write small chunks of code at a time. Use the debugger to watch exactly what's happening in your code and verify that it's behaving the way you want. Don't write any code until you have a really good idea in your head as to what it's supposed to do.
    Last edited by GCDEF; June 30th, 2015 at 07:31 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured