Solved

It's been solved, thank you!
Last edited on
> I just wanted to compare my code to yours!
Yeah, right - like no one has ever tried to pull that trick before.

If you want a serious appraisal of your work, post it.

Hello ThisBoi,

Do not take this the wrong way. I just copy and paste this in case you do not know.


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.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


This works better when you post your code and ask for suggestions. Any code that you may get to look at is likely to be beyond where you are at. And this forum works best when you post your code and ask for suggestions or help with a problem.

To start with I would not use the line using namespace std; in the program. After about two months or so I came to realize that this line can cause problems and should not be used.

Next variables like "X", "Y", "Z", "a", "b" and "N" would be changed. The capital letters make me think that these variables are defined as constants and not regular variables. As I have read here in the past a variable name should be a noun and I add to that a variable name should describe what the variable is or does.

I hope that you can see that posting your code and asking for input is better than asking someone to write the same program just to compare the differences.

I hope that I have not discouraged you because I would like to see your code and offer any suggestions I can as would others.

Hope that helps,

Andy
If you could make a code and post it here, that’d be really nice.

You first. Since, y'know, we're being really nice and sharing.
Hi! I can totally see where you guys are coming from. Earlier I said that the code was finished, but it seems like something is wrong? The answer for Z varies, it sometimes becomes 0. Any ideas as to why? As such here's my code:
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
99
100
101
102
103
104
105
106
#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

int validb(int b);
int validN(int N);
int solveX(int a, int b);
int solveY(int a, int b, int X, int N);
int solveZ(int i, int N, int X, int Y);
void display(int X, int Y, int Z);

int main ()
{
	int a,b,N,X,Y,Z,i;
	char choice;
	
	cout<<"Enter values for a, b, and N:";
	cout<<"\n\na:";
	cin>>a;
	
	cout<<"\nb:";
	cin>>b;
	b = validb(b);
	
	cout<<"\nN:";
	cin>>N;
	N = validN(N);
	
	X = solveX(a,b);
	Y = solveY(a,b,X,N);
	Z = solveZ(i,N,X,Y);
	
	display(X,Y,Z);
	
	cout<<"\n\nWould you like to go again? Press y to continue, n to stop.";
	cin>>choice;
	choice = toupper(choice);
	
	for (;cin>>choice;)
	{
		if (choice=='Y')
		{
			system ("CLS");
			return main();
		}
		else if (choice=='N')
		{
			cout<<"\n\nThank you! Goodbye";
			return 0;
		}
	}
}

int validb(int b)
{
	while (b<=0)
	{
		cout<<"Invalid input, b must be greater than 0.\nPlease reenter values.";
		cout<<"b:";
		cin>>b;
	}
	
}

int validN(int N)
{
	while (N<=0)
	{
		cout<<"Invalid input, N must be greater than 0.\nPlease reenter values.";
		cout<<"N:";
		cin>>N;
	}
}

int solveX(int a, int b)
{
	return (a*b*b)+(a/sqrt(b));
}

int solveY(int a, int X, int N, int b)
{
	return (a*X)+(a+N/b);
}

int solveZ(int i, int N, int X, int Y)
{	
	int f;
	unsigned factorial = 1;
	f = N-1;
	for(int i = 1; i<=f; ++i)
	{
		factorial *= i;
	}
	return (i/(X-Y));
}

void display(int X, int Y, int Z)
{
	cout<<fixed<<setprecision(3)<<endl;
	cout<<"\n\nThe values for X, Y, and Z are:";
	cout<<"\nX:"<<X;
	cout<<"\nY:"<<Y;
	cout<<"\nZ:"<<Z;
}
Last edited on
"Seems to be." What kind of error? Be specific.

For example, a compiler says:
 In function 'int solveZ(int, int, int)':
92:21: error: 'n' was not declared in this scope
94:3: error: 'factorial' was not declared in this scope
1
2
3
4
5
6
7
8
9
10
11
int solveZ(int N, int X, int Y)
{
	int i;
	
	i = N-1;
	for(int i = 1; i <=n; ++i) // error: what is 'n'?
	{
		factorial *= i; // error: what is 'factorial'?
	}
	return (i/(X-Y));
}

Those are syntax errors that prevent the compilation of the program. You can't run a program that does not exist.

There were more:
 In function 'int main()':
46:16: warning: ISO C++ forbids taking address of function '::main' [-Wpedantic]
16:18: warning: unused variable 'i' [-Wunused-variable]
 In function 'int validb(int)':
65:1: warning: no return statement in function returning non-void [-Wreturn-type]
 In function 'int validN(int)':
75:1: warning: no return statement in function returning non-void [-Wreturn-type]

Never call the main() in your program. Forbidden. Use a loop.

What value does this function return?
1
2
3
double answer()
{
} // warning: no return statement in function returning non-void 

If you promise to return a value, then you must return a value.

Do you really need two validation functions? Consider parametritized function:
1
2
3
4
5
6
7
int valid( char name )
{
  int input {};
  cout << name << ':' ;
  cin >> input;
  return input;
}


If I type 0, will it be good or bad?
1
2
3
while ( b < 0 )
{
  cout << "Invalid input, b must be greater than 0.";
Last edited on
Why did you discard your append? Your append reappeared.
[...]

Only two remarks:
Integer is nice for computing with integers, add, sutract, multiply, modulo, euclidian division. In contrast for square root and real division the effect of integers is not always useful. In that case use float or similar. That will cure your Z.

Then, please compare int validb(int b) and int validN(int N) -- then read again your task: "1. Valid() - validates either b or N. Input validation b>0,N>0 (using . a while loop)"
Not edit/copy-edit/paste is the big advantage of computers, it's the reuse of general solutions. So one of the two functions is enough for b and N.

Edit: see strike through
Last edited on
Hi! Thank you for all your inputs. I've changed the validation, and the int to float. Also, I figured out why the value for Z was always 0. I forgot to add a "return b;" after its validation! Took a lot longer to find it but thanks again! Here's the working code now:
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
#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

float valid(float b);
float solveX(float a, float b);
float solveY(float a, float X, float N, float b);
float solveZ(float N, float i, float X, float Y);
void display(float X, float Y, float Z);

int main ()
{
	float a,b,N,X,Y,Z,i;
	char choice;
	
	i=1;
	cout<<"Enter values for a, b, and N:";
	cout<<"\n\na:";
	cin>>a;
	
	cout<<"\nb:";
	cin>>b;
	b = valid(b);
	
	cout<<"\nN:";
	cin>>N;
	
	X = solveX(a,b);
	Y = solveY(a,X,N,b);
	Z = solveZ(N,i,X,Y);
	
	display(X,Y,Z);
	
	cout<<"\n\nWould you like to go again? Press y to continue, n to stop.";
	cin>>choice;
	choice = toupper(choice);
	
	for (;choice;)
	{
		if (choice=='Y')
		{
			system ("CLS");
			return main();
		}
		else if (choice=='N')
		{
			cout<<"\n\nThank you! Goodbye";
			return 0;
		}
	}
}

float valid(float b)
{
	while (b<=0)
	{
		cout<<"Invalid input, b must be greater than 0.\nPlease reenter values.";
		cout<<"b:";
		cin>>b;
	}
	return b;
	
}

float solveX(float a, float b)
{
	return (a*b*b)+(a/sqrt(b));
}

float solveY(float a, float X, float N, float b)
{
	return (a*X)+(a+N/b);
}

float solveZ(float N, float i, float X, float Y)
{	
	float f;
	float factorial = 1;
	f = N-1;
	for(; i<=f; ++i)
	{
		factorial *= i;
	}
	return (factorial/(X-Y));
}

void display(float X, float Y, float Z)
{
	cout<<fixed<<setprecision(3)<<endl;
	cout<<"\n\nThe values for X, Y, and Z are:"<<endl;
	cout<<"\nX:"<<X<<endl;
	cout<<"\nY:"<<Y<<endl;
	cout<<"\nZ:"<<Z<<endl;
}
please don't edit your original question with "it's been solved, thanks" this is an abuse of this forum,

this question may help other people who are in the same situation as you in the future, but now they will not know the question as you have edited it.
Topic archived. No new replies allowed.