Im back with another assignment.

Some help would be appreciated

Not sure how to add images to this post so Ill try to explain to the bes tof my abilites. this assignment is meant to text loops and while i got the entire code to work doing that, its the actual inner working that give issue. The program builds but when it's meant to display the costs it shows the same cost for each line within the loop.

for example:

year 1 1212
year 2 1212
year 3 1212

instead of

year 1 1212
year 2 1224
year 3 1236

Im not sure how to get the each new line to add 12 each time

case 2 is also giving an issue where it displays case 3's end results instead the intended results. case 2 is supposed to go by up by 24, not by 48.


and last but not least when the program loops to the beginning. it refuses to run again. so after one succesful run, it loops back to the beginning and if you try to make another selection it wont display the result, it'll just loop back to the beginning


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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Ronda's Strikeforce Gym Project
// 2-26-2018
// Calculates the membership fees over the next 10 years based on the membership

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	//A) Declare necessary variables for user input and calculations
	int userInput;
	const int GOLD_CHOICE = 1, SILVER_CHOICE = 2, BRONZE_CHOICE = 3, QUIT_CHOICE = 4;
	const double GOLD = 0.01, SILVER = 0.02, BRONZE = 0.04, INITAL_FEE = 1200.00;

	//Variables
	int years = 1;
	double fees, cost;

		//Start do {(HERE)
		do
	{
		//B) Display menu
		cout << "          Welcome to Ronda's Strikeforce Gym!!\n";
		cout << "\n";
		cout << "x-----------------------------------------------------x\n";
		cout << "             Membership Fee Calculator\n";
		cout << "\n";
		cout << "\t1. Gold\n";
		cout << "\t2. Silver\n";
		cout << "\t3. Bronze\n";
		cout << "\t4. Quit\n\n";
		cout << "\n";
		cout << "Please enter your membership level(1 - 3 Enter 4 to Quit) \n";
		//C) Read in the input from the user
		cin >> userInput;
		
		//D) Validate user input(must input 1 through 4), loop if value is invalid
		while (userInput < 1 || userInput > 4)
		{
			cout << "Invalid entry!\n";
			cout << "Please enter a selection from 1 through 4:\n";
			cin >> userInput;
			
		}

		//E) If values 1 - 3 were entered perform calculations else if 4 was entered exit program with thank you message.
		// E.1) Perform calculation for specified membership for 10 years and output
		// (this will require a loop)

		if (userInput != QUIT_CHOICE)
		{
			//Start selection loop and calculation
			switch (userInput)
			{
			case 1:
				fees = INITAL_FEE * GOLD;
				cost = INITAL_FEE + fees;
				break;
			case 2:
				fees = INITAL_FEE * SILVER;
				cost = INITAL_FEE + fees;
			case 3: 
				fees = INITAL_FEE * BRONZE;
				cost = INITAL_FEE + fees;

			}

			const int max_number = 10;

			//Display Yearly Cost
			while (years <= 10)
			{
				cout << setprecision(2) << fixed << "Year     " <<  years << "    "<< cost << endl;
				years++; // increment the counter
			}
		}
		} while (userInput != QUIT_CHOICE);

	return 0;

}
What is the value of 'years' the first time you reach line 23?
How about the second time on line 23? Third?
Line 23? I valued years to 1 since having simply as int years wouldnt allow the program to build. Years is supposed to hold a value instead but like I said I couldnt get it to work without assigning 1 to it. and the first instance where I actually use it is in line 74, where it displays what I want without issue.
Last edited on
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	//A) Declare necessary variables for user input and calculations
	int userInput = 0 ;

	const int GOLD_CHOICE = 1, SILVER_CHOICE = 2, BRONZE_CHOICE = 3, QUIT_CHOICE = 4;
	const double GOLD = 0.01, SILVER = 0.02, BRONZE = 0.04, INITAL_FEE = 1200.00;

	//Variables
	int years = 1;
	double fees = 0 , cost = INITAL_FEE ; // change initialize value for cost

	//Start do {(HERE)
	do
	{
		//B) Display menu
		cout << "          Welcome to Ronda's Strikeforce Gym!!\n";
		cout << "\n";
		cout << "x-----------------------------------------------------x\n";
		cout << "             Membership Fee Calculator\n";
		cout << "\n";
		cout << "\t1. Gold\n";
		cout << "\t2. Silver\n";
		cout << "\t3. Bronze\n";
		cout << "\t4. Quit\n\n";
		cout << "\n";
		cout << "Please enter your membership level(1 - 3 Enter 4 to Quit) \n";
		//C) Read in the input from the user
		cin >> userInput;

		//D) Validate user input(must input 1 through 4), loop if value is invalid
		while (userInput < 1 || userInput > 4)
		{
			cout << "Invalid entry!\n";
			cout << "Please enter a selection from 1 through 4:\n";
			cin >> userInput;

		}

		//E) If values 1 - 3 were entered perform calculations else if 4 was entered exit program with thank you message.
		// E.1) Perform calculation for specified membership for 10 years and output
		// (this will require a loop)

		if ( userInput == 4 )
		{
			break ;
		}

		//if  ( userInput != QUIT_CHOICE )
		{
			//Start selection loop and calculation
			switch ( userInput )
			{
				case 1:
					fees = INITAL_FEE * GOLD;
					//cost = INITAL_FEE + fees;
					break;
				case 2:
					fees = INITAL_FEE * SILVER;
					//cost = INITAL_FEE + fees;
					break ; //insert
				case 3:
					fees = INITAL_FEE * BRONZE;
					//cost = INITAL_FEE + fees;
				default:
					break ;
			}

			// const int max_number = 10; // ????

			//Display Yearly Cost
			while (years <= 10)
			{
				cout << setprecision(2) << fixed << "Year     " <<  years << "    "<< (cost += fees) << endl;
				years++; // increment the counter
			}
			years = 1 ; // this solve next loop
		}

	} while (userInput != QUIT_CHOICE);

	return 0;

}


the following cycles did not print in the output because 'year' remained 10 after the first cycle.
Last edited on
the following cycles did not print in the output because 'year' remained 10 after the first cycle.

Exactly.

You don't even need the years outside the do loop.
You could remove line 14 (int years=1;) and rewrite lines 75-81 as:
1
2
3
4
5
6
//Display Yearly Cost
for ( int years=1; years <= 10; ++years )
{
  cout << setprecision(2) << fixed << "Year     " <<  years
       << "    "<< (cost += fees) << endl;
}
Well I see how I missed putting in the break statement for case 2 and 3. So that did help fix that. In fact this did fix everything. I followed your code and saw my mistakes. I did add that
1
2
3
4

const int max_number = 10
int min_number = 1 
 


Something i picked up looking back at my book. before seeing your post. I've at this for the past 7 hours, so slowly but surely was inching towards it.

Now the only thing I got to fix is the cost+= fees doesnt display what i need for all 3 cases. I think I have to get a little more in depth.
The current code, the one above makes so the results of case one start at 1224 and go up by 12, so while the increment is correct its also supposed to start at 1212. case 2 results starts at 1248 and its supposed to start at 1224. case 3 results start at 1296 instead of 1248.
You reset 'cost' only before the do loop. Perhaps within loop, before the "show loop"?

Note:
1
2
3
4
5
6
7
8
9
cout << (cost += fees);

// is same as
cost += fees;
cout << cost;

// and different from
cout << cost;
cost += fees;



PS. What are the constants GOLD_CHOICE, SILVER_CHOICE, BRONZE_CHOICE? They are not used anywhere.
Hint: what are the 1, 2, 3 in the switch?
Last edited on
the problem is that the variable 'cost' must also be initialized at each cycle. alternatively you can declare const within the do-while cycle
alright, let me see what I can do in one hour. Literally the last thing i need to fix to get this done properly. I wanna feel that sense of accomplishment once I figure it out.
The GOLD_CHOICE, and the other constant, I'm pretty sure I used incorrectly. They were meant to define each case within the switch but I did notice I didnt actually use since I named the case 1, 2, 3. I was assuming that they correlated to the constants.
So this is what I have so far, I kinda did a cop out with by using a new constant const cost_1 = 1880 but it made the job work to display the proper numbers. The loop isnt restarting properly though. Each time it runs, the results just continue on using the last series of numbers instead of starting from the very beginning. Looking into it to see how I can make it stop doing that.

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

// Ronda's Strikeforce Gym Project
// 2-26-2018
// Calculates the membership fees over the next 10 years based on the membership

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	//A) Declare necessary variables for user input and calculations
	int userInput = 0;

	const int GOLD_CHOICE = 1, SILVER_CHOICE = 2, BRONZE_CHOICE = 3, QUIT_CHOICE = 4;
	const double GOLD = 0.01, SILVER = 0.02, BRONZE = 0.04, INITAL_FEE = 1200.00;

	//Variables
	double fees = 0, cost = INITAL_FEE, cost_1 = 1200;

		//Start do {(HERE)
		do
	{
		//B) Display menu
		cout << "          Welcome to Ronda's Strikeforce Gym!!\n";
		cout << "\n";
		cout << "x-----------------------------------------------------x\n";
		cout << "             Membership Fee Calculator\n";
		cout << "\n";
		cout << "\t1. Gold\n";
		cout << "\t2. Silver\n";
		cout << "\t3. Bronze\n";
		cout << "\t4. Quit\n\n";
		cout << "\n";
		cout << "Please enter your membership level(1 - 3 Enter 4 to Quit) \n";
		//C) Read in the input from the user
		cin >> userInput;
		
		//D) Validate user input(must input 1 through 4), loop if value is invalid
		while (userInput < 1 || userInput > 4)
		{
			cout << "Invalid entry!\n";
			cout << "Please enter a selection from 1 through 4:\n";
			cin >> userInput;
			
		}

		//E) If values 1 - 3 were entered perform calculations else if 4 was entered exit program with thank you message.
		// E.1) Perform calculation for specified membership for 10 years and output
		// (this will require a loop)

		if (userInput == 4)
		{
			break;

		}

		if (userInput != QUIT_CHOICE)
		{
			//Start selection loop and calculation
			switch (userInput)
			{
			case 1:
				fees = INITAL_FEE * GOLD;
				cost = INITAL_FEE + fees;
				break;
			case 2:
				fees = INITAL_FEE * SILVER;
				cost = INITAL_FEE + fees;
				break;
			case 3: 
				fees = INITAL_FEE * BRONZE;
				cost = INITAL_FEE + fees;
				break;
			}

			
			const int	min_number = 1,
						max_number = 10;

			//Display Yearly Cost
			for (int years = 1; years <= 10; ++years)

			{
				cout << setprecision(2) << fixed << "\tYear\t" << years
					<< "\t" << (cost_1 += fees) << endl;
				
			}
			
		}
		
		} while (userInput != QUIT_CHOICE);
		


	return 0;

}
I got it, not completely but im like 95% done with it. added a few more math formulas and that did the trick.
Topic archived. No new replies allowed.