tuition calculator

this is the excercise

Suppose that the tuition for a university is $10,000 this year (year 1) and increases
5% every year. Write a program that computes the tuition in x years from now, with
x being a number given by the user. In addition, the program should compute the
total cost of y years’ worth of tuition starting from now, with y being a number given
by the user. Finally, the program should display the tuition for x years along with the total tuition cost at the end of each year. Your program will terminate when the user
enters 0.

the output should look like that >>

Welcome to the tuition calculator!
1 Compute the yearly tuition in x years from today
2 Compute the total tuition cost for y years from today
3 Compute yearly tuition and total tuition cost at the end of each year, for x years
0 Quit program
Enter choice: 1
You want to know the yearly tuition in how many years from today? 2
The yearly tuition after 2 years from today will be 11025
Enter choice: 2
You want to know the total tuition cost after how many years from today? 2
The total tuition cost after 2 years from today will be 20500
Enter choice: 3
How many years you want to consider starting from today? 3
Year Tuition Total Tuition Cost at the End of Year
1 10000 10000
2 10500 20500
3 11025 31525
Enter choice: 0
Goodbye!

it shouldnt ask "enter choice" all the time. Its just to show what it wants with each number chosen.

Here is my code. I dont know how do i count the second choice and have no idea what to do for the third choice either.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;


int main()
{
	double tuition = 10000;
	double total;
	int a;
	cout << "Welcome to the tuition calculator!" << endl;
	cout << "1 Compute the yearly tuition in x years from today " << endl;
	cout << "2 Compute the total tuition cost for y years from today" << endl;
	cout << "3 Compute yearly tuition and total tuition cost at the end of each year, for x years " << endl;
	cout << "0 Quit program " << endl << endl;
	cout << "Enter your choice: ";
	cin >> a;

	if (a == 1) {
		cout << "You want to know the yearly tuition in how many 
 years from today? ";
		int b;
		cin >> b;
		for (int i = 1; i <= b; i++) {
			tuition = tuition * .05 + tuition;
		}

		cout << "The yearly tuition after " << b << " years from today will be " << tuition;
	}
	else if (a == 2) {
		cout << "You want to know the total tuition cost after how many years from today? ";
		int c;
		cin >> c;
		for (int z = 1; z <= c; z++) {
			
			tuition = tuition + (tuition * .05 + tuition);
		}
		cout << "The total tuition cost after " << c << " years from today will be " << tuition;
	}
	
 
}


I know only loops,else if and thats pretty much it. So please keep it the same simple style so i can understand your code. Thank you very much
Last edited on
Hello akira5555,

While I go over your code.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Andy
done
Hello akira5555,

In regards to the {}s. Pick a style and be consistent in its use.

Lines 19 and 20 will not work that way. Either make it 1 line, better choice here, or put each bit of the string in a set double quotes.

Choice 1 and 2 are very close to being exactly the same. I have started with choice 1 first and have come up with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>

using namespace std;

int main()
{
    constexpr double TUITION{ 10000.0 }, YEARINCREASE{ 0.05 };  // <--- Could aslo use "1.05" here.
    double total{}, tuition{};
    int MenuChoice{};

    cout <<
        "Welcome to the TUITION calculator!\n"
        "1 Compute the yearly TUITION in x years from today\n"
        "2 Compute the total TUITION cost for y years from today\n"
        "3 Compute yearly TUITION and total TUITION cost at the end of each year, for x years\n"
        "0 Quit program\n"
        "Enter your choice: ";
    cin >> MenuChoice;

    if (MenuChoice == 1)
    {
        int years{};

        tuition = TUITION;

        cout << "\nYou want to know the yearly TUITION in how many years from today ? ";
        cin >> years;

        std::cout << '\n';

        for (int i = 1; i <= years; i++)
        {
            total += tuition;

            std::cout << "The yearly tuition in year " << i << " is: " << tuition << '\n';

            tuition *= 1 + YEARINCREASE;
        }

        cout << "\nThe yearly tuition after " << years << " years from today will be " << total;
    }

Line 41 would be optional.

I think this for loop is what you are wanting.

Choice 2 could make use of the same for loop.

Choice 3 looks like it is a combination of choice 1 and choice 2.

In the above for loop you may want to do some rounding as 4 years ends up with a decimal value that you may or may not want.

Andy
You'll need a loop. Specifically, you'll want to make sure everything you want to repeat is inside the loop, while everything you don't want to repeat is outside the loop. You'll need the loop to keep iterating until the user enters '0'.

Oh, and as for:

it shouldnt ask "enter choice" all the time.


it should ask "enter choice" every time it waits for the user to enter the choice. Otherwise, the program will just be sitting there waiting, with no way for the user to know what it's waiting for.
Hello akira5555,

Sorry I had to step away from the computer before I could add this:

Welcome to the TUITION calculator!
1 Compute the yearly TUITION in x years from today
2 Compute the total TUITION cost for y years from today
3 Compute yearly TUITION and total TUITION cost at the end of each year, for x years
0 Quit program
Enter your choice: 1

You want to know the yearly TUITION in how many years from today ? 3

The yearly tuition in year 1 is: 10000
The yearly tuition in year 2 is: 10500
The yearly tuition in year 3 is: 11025

The yearly tuition after 3 years from today will be 31525



Andy
MikeyBoy, you did not undertsand what i mean. Ofc it should ask the user after you press run, i am not that stupid. What i mean is after it asks you to enter a choice it wont ask you to enter it again after counting whatever you chose there
Handy Andy, thank you for your idea, could definetly use your code and continue onwards. You are indeed handy ;)
Hello akira5555,

As you program is designed right now it will only work once. For now this is fine until you get everything working correctly.

Eventually you will have to wrap most of "main" in a do/while or while loop to keep it running until the user enters zero.

A loop is not that important. What is important is getting each menu choice to work the way that you want. Sometimes it is easiet to add the loop later and concentrate on 1 thing at a time.

Andy
i got this so far, its basically finished i just dont know how do i get tuition right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
using namespace std;


int main()
{
	constexpr double TUITION{ 10000.0 };
	double tuition = 10000;
	double total{};
	double yearincrease = 0.05;
	int menuchoice;
	cout << "Welcome to the tuition calculator!" << endl;
	cout << "1 Compute the yearly tuition in x years from today " << endl;
	cout << "2 Compute the total tuition cost for y years from today" << endl;
	cout << "3 Compute yearly tuition and total tuition cost at the end of each year, for x years " << endl;
	cout << "0 Quit program " << endl << endl;
	cout << "Enter your choice: ";
	cin >> menuchoice;

	if (menuchoice == 1) {
		cout << "You want to know the yearly tuition in how many years from today? ";
		int b;
		cin >> b;
		for (int i = 1; i <= b; i++) {
			
			tuition *= 1 + yearincrease;
		}

		cout << "The yearly tuition after " << b << " years from today will be " << tuition;
	}
	else if (menuchoice == 2) {
		cout << "You want to know the total tuition cost after how many years from today? ";
		int x;
		cin >> x;
		for (int i = 1; i <= x; i++) {
			total += tuition;
			tuition *= 1 + yearincrease;
			
		}
		
		cout << "The total tuition cost after " << x << " years from today will be " << total;
	}

	else if (menuchoice == 3) {
		cout << "How many years you want to consider starting from today? ";
		int k;
		cin >> k;
	        cout << endl << "Year" << "    " << "Tuition" << "    " << "Total Tuition Cost 
                    at the End of Year";
		for (int i = 1; i <= k; i++) {
			total += tuition;
			tuition *= 1 + yearincrease;
			
			cout << endl << i << "       " << tuition << "          " << total;
		}
	}

}
i always get the wrong tuition result in choice 3. Instead of 10000 first year i get 10500 and so I get the tuition for the second year as the first year
If you want the tuition increase to not occur until after the payment itself, then you need to print the cost, then do the price increase logic.

Also, 'b' 'x' 'k' are not meaningful variable names. You could make them all be int years; instead since you're asking the user to input the number of years. You also never use your constexpr int TUITION.
Last edited on
you really think this will help dude? i dont know how do i change my code for it to work like that. That is why i am asking for help.
Hello akira5555,

Your latest code is a step in the right direction, but not quite there yet.

This is what I have come up with based on what you started with, but it could be better.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <iomanip>  // <--- Added.
#include <string>   // <--- Added.

using namespace std;

int main()
{
    constexpr double TUITION{ 10000.0 }, YEARINCREASE{ 0.05 };  // <--- Stores the number as "0.05000000000000000278".

    int MenuChoice{};
    double total{}, tuition{};
    
    std::cout << std::fixed << std::setprecision(2);

    //do
    //{
        cout <<
            "\n\n"
            << std::string(84, '-') << '\n' << std::string((84 / 2) - 18, ' ')
            << "Welcome to the tuition calculator!\n" << std::string(84, '-') << '\n' <<

            "1 Compute the yearly tuition in x years from today\n"
            "2 Compute the total tuition cost for y years from today\n"
            "3 Compute yearly tuition and total tuition cost at the end of each year, for x years\n"
            "0 Quit program\n"
            "Enter your choice: ";
        cin >> MenuChoice;

        if (MenuChoice == 1)
        {
            int years;          // <--- Local variable to the if statement. Destroyed when the if ends.

            tuition = TUITION;  // <--- You do not make use of the constant in your code.
            total = 0.0;        // <--- Needs to be zeroed in each if statement.

            cout << "\nYou want to know the yearly tuition in how many years from today ? ";
            cin >> years;

            std::cout << '\n';

            for (int i = 1; i <= years; i++)
            {
                tuition *= 1 + YEARINCREASE;
            }

            cout << "\nThe yearly tuition after " << years << " years from today will be " << tuition << "\n\n";
        }

The number stored in the variable "YEARINCREASE" may cause a slight difference in your calculations, but should not be a problem.

The beginning of the do/while loop is for later. You do not have to worry about it right now.

The 1st "cout" is a suggestion, but the format makes it much easier to work with. Each line, 23 - 27, may look like individual strings, but the IDE and compiler treat it as 1 big string.

Also replacing the "endl"s with the new line (\n) works just as well. The "endl" is a function that takes time. The more you have the more time it takes and you could see the program run a bit slower because of it.

The for loop is a basic start.

In choice 2 what you have is correct. And in choice 3 this will be just using the for loop in choice 2 a little differently.

Just to give you an idea:



------------------------------------------------------------------------------------
                        Welcome to the tuition calculator!
------------------------------------------------------------------------------------
1 Compute the yearly tuition in x years from today
2 Compute the total tuition cost for y years from today
3 Compute yearly tuition and total tuition cost at the end of each year, for x years
0 Quit program
Enter your choice: 1

You want to know the yearly tuition in how many years from today ? 3


The yearly tuition after 3 years from today will be 11576.25



------------------------------------------------------------------------------------
                        Welcome to the tuition calculator!
------------------------------------------------------------------------------------
1 Compute the yearly tuition in x years from today
2 Compute the total tuition cost for y years from today
3 Compute yearly tuition and total tuition cost at the end of each year, for x years
0 Quit program
Enter your choice: 2

You want to know the total tuition cost after how many years from today? 3

The total tuition cost after 3 years from today will be 31525.00



------------------------------------------------------------------------------------
                        Welcome to the tuition calculator!
------------------------------------------------------------------------------------
1 Compute the yearly tuition in x years from today
2 Compute the total tuition cost for y years from today
3 Compute yearly tuition and total tuition cost at the end of each year, for x years
0 Quit program
Enter your choice: 3

You want to know the yearly tuition in how many years from today ? 3

The yearly tuition in year 1 is: 10000.00
The yearly tuition in year 2 is: 20500.00
The yearly tuition in year 3 is: 31525.00


------------------------------------------------------------------------------------
                        Welcome to the tuition calculator!
------------------------------------------------------------------------------------
1 Compute the yearly tuition in x years from today
2 Compute the total tuition cost for y years from today
3 Compute yearly tuition and total tuition cost at the end of each year, for x years
0 Quit program
Enter your choice: 0

     Goodbye!


If I understand correctly this is what you are after.

I will give your code a test and see if I find anything else.

Andy
Hello akira5555,

Sorry for the delay.

After testing your new code and with Ganado's suggestion plus some additions it works.

Sorry if I misunderstood what you output should look like.

After testing and some adjustments you need to add what I did with lines 34 and 35 of the last code I posted. If you do not reset these values you will get wrong answers when you put most of "main" in a loop because you will just be continually adding to these variables.

The rest is a few well placed "\n"s to make the output easier to read.

Andy
Topic archived. No new replies allowed.