Need assistance with this exercise...

Hello everyone!

I'm doing an exercise where I have to write a program that will suggest a subscription plan based on the users answers to a series of yes-or-no questions.

the exercise is based on this chart: https://imgur.com/a/AEMw5Ln

my main question is: how can I get C++ to 'tally up' the results and produce a plan?

here's my code so you can see what I've done so far...

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
  #include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
	//Declare variable
	char answer;
	int storageCapacity;
	float years;
	float costPerMonth;
	float yearlyCost;
	bool freeTheme = false;
	bool customDesign = false;
	bool seoTools = false;
	bool liveSupport = false;
	
	
	// Firstly, Welcome the client!
	cout << " Welcome to the Wordpress subsciption sugesstion Tool!" << endl;
	cout << " In order to find the right plan for you, we'll need to ask you a series of questions!" << endl;
	cout << " Answer the following questions using 'y' or 'n' keys when prompted." << endl;
	
	// secondly, Present the series of questions and prompt the user for input.
	cout << "\t\n 1.) Would you like to utilize free themes? (y/n)" << endl;
	cin >> answer;
	
	if(answer = 'y')
	{
		freeTheme = true;
	}
	else
	{
		freeTheme = false;
	}
	cout << "\t\n 2.) Would you like to customize your themes? (y/n)" << endl;
	cin >> answer;
	
	if(answer = 'y')
	{
		customDesign = true;
	}
	else
	{
		customDesign = false;
	}
		
	cout << "\t\n 3.) Will you be needing Search Engine Optimization? (y/n)" << endl;
	cin >> answer;
	
	if(answer = 'y')
	{
		seoTools = true;
	}
	else
	{
		seoTools = false;
	}
		
	cout << "\t\n 4.) Is live support important for you? (y/n)" << endl;
	cin >> answer;
	
	if(answer = 'y')
	{
		liveSupport = true;
	}
	else
	{
		liveSupport = false;
	}

	cout << "\t\n 5.) How much data will you need to host? 3GB, 6GB, 13GB, or unlimited(999GB)?" << endl;
	cin >> storageCapacity;
	
	if(storageCapacity = 3)
	{
		costPerMonth = 0.00;
	}
	else if (storageCapacity = 6)
	{
		costPerMonth = 5.00;
	}
	else if (storageCapacity = 13)
	{
		costPerMonth = 8.00;
	}
	else if (storageCapacity = 999)
	{
		costPerMonth = 25.00;
	}
	
	cout << "\t\n 6.) How many years are you interest in hosting for?" << endl;
	cin >> years;
	


Which statements/ operators am I missing? should I try incorporating switch statements? Any help is much appriciated.
Turn up your compiler warning level.

Then fix all those = which should be == in all your if statements.

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
$ g++ -Wall baz.cpp
baz.cpp: In function ‘int main()’:
baz.cpp:29:17: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
  if(answer = 'y')
                 ^
baz.cpp:40:17: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
  if(answer = 'y')
                 ^
baz.cpp:52:17: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
  if(answer = 'y')
                 ^
baz.cpp:64:17: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
  if(answer = 'y')
                 ^
baz.cpp:76:24: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
  if(storageCapacity = 3)
                        ^
baz.cpp:80:30: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
  else if (storageCapacity = 6)
                              ^
baz.cpp:84:31: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
  else if (storageCapacity = 13)
                               ^
baz.cpp:88:32: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
  else if (storageCapacity = 999)
                                ^
baz.cpp:12:8: warning: variable ‘costPerMonth’ set but not used [-Wunused-but-set-variable]
  float costPerMonth;
        ^
baz.cpp:13:8: warning: unused variable ‘yearlyCost’ [-Wunused-variable]
  float yearlyCost;
        ^
baz.cpp:14:7: warning: variable ‘freeTheme’ set but not used [-Wunused-but-set-variable]
  bool freeTheme = false;
       ^
baz.cpp:15:7: warning: variable ‘customDesign’ set but not used [-Wunused-but-set-variable]
  bool customDesign = false;
       ^
baz.cpp:16:7: warning: variable ‘seoTools’ set but not used [-Wunused-but-set-variable]
  bool seoTools = false;
       ^
baz.cpp:17:7: warning: variable ‘liveSupport’ set but not used [-Wunused-but-set-variable]
  bool liveSupport = false;
       ^


Then resolve to write LESS code between compilations.
Writing 100 lines, getting a sack of errors and dumping it on a forum is not a long term strategy.

You should have figured out something was wrong with your first question/answer.

Hello BennysMaria,

Looking over the program at this point I see no use for "cmath". If you plan on using the functions "abs()", "pow()", "sqrt()", "sin()", tan()" or others then you would need "cmath". If you would be using just (+, -, *, / or %) then "cmath" is not needed as these are built into the IDE/compiler and always available.

It is always a good idea to initialize your variables when defined. If for no other reason than to know that they do not contain a garbage value.
1
2
3
4
5
6
7
8
9
10
//Declare variable
char answer{};
int storageCapacity{};
double years{};
double costPerMonth{};
double yearlyCost{};
bool freeTheme{ true };
bool customDesign{};    // <--- From C++11 on this is all you need.
bool seoTools = false;  // <--- This is extra work that does the same as above.
bool liveSupport = false;

The empty {}s, available from C++11 on, will set the variable to (0) zero based on the type of variable.

Since "freeTheam" is available to each plan you can set this one to true and then you do not need to ask any question about it.

"double" is the preferred choice for a floating point type, but even a "double" can have problems. From what I can see for now an "int" would work just fine as you are only using whole numbers.

For your if statements.
I used this as an example, but it is not needed because "freeTheam" is set to true and available to every plan.
1
2
3
4
5
6
7
8
9
10
11
cout << "\t\n 1.) Would you like to utilize free themes? (y/n)" << endl;
cin >> answer;

if (answer = 'y')  // <--- But what if the user typed 'Y'? You should have "if (answer =='y' || answer == 'Y')"
{
    freeTheme = true;
}
else
{
    freeTheme = false;
}

As the comment shows you should check for either case of the letter. Any given user will break your program without even trying. Best to try and anticipate any problems.

The "else" part is not needed. More so in the rest of the if statements as the variables are initialized to false this makes them redundant since the variables are already set to false.

I would consider either setting the bool variables to reflect the "free" plan or the plan that you want to push. Then adjust the plan type based on the answers to the questions.

I do see a potential problem with question 5. If the user enters "3, 5, 13 or 999" everything is OK, but if the user types "3GB, 6GB or 13GB" the formatted input will store the number in the variable stopping at the first character that is not a number leaving the "GB\n" in the input buffer for the next "cin" to use which would cause "cin" to fail when it expects a number based on "years" being defined as a numeric value and you are trying to give it something that is not a number. The same would happen if the user enter the letter 'u' or "unlimited".

This is one solution that would get a proper number to use instead of something that would cause a problem.

How much data will you need to host?
  1. 3GB
  2. 6GB
  3. 13GB
  4. unlimited
  Enter choice:_  // <--- The (_) denotes a space.


Your next set of if statements:
1
2
3
4
if (storageCapacity == 3)
{
    costPerMonth = 0.00;
}

First if you initialize your variables when defined this is redundant as "costPerMonth" is already set to zero.

These statements make it look like you are basing the cost of a plan solely on the amount of storage which would make the first 4 questions unnecessary. The answers to these questions would have not bearing on the plan type. I do not think that is what the program is intended to do.

One question I do have is after looking at your link the green check is easy to understand. But what does the red X mean and what is the difference between the red X and an empty cell? Knowing the difference may determine how you write your program.

Andy
Topic archived. No new replies allowed.