Creating Libraries with void functions

For my beginner's C++ class my assignment is to create a library with following functions (prototypes are given along with a description):

1. void increasing (double& A, double& B, double& C); //Arranges A, B, and C in increasing order.

2. void decreasing (double& A, double& B, double& C); //Arranges A, B, and C in decreasing order.

3. void mmm (double& A, double& B, double& C); //Replaces A with the minimum of A, B, and C; replaces B with the average of
A, B, and C; replaces C with the maximum of A, B, and C.

Use the library in a program where the user is asked to enter three numbers. The program calls all three functions above and
outputs the new values of A, B, and C for each case. Do not just output numbers. Display a message with each result.*/


Jdog is my library name.

My header is
 
void increasing(double&A, double&B, double&C); /*Displays 3 numbers in increasing order*/


My .cpp is
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
  void increasing(double&A, double&B, double&C)
{
	double small, double medium, double large;
	if ((A <= B) && (A <= C) && (B <= C))
	{
		small = A;
		medium = B;
		large = C;
	}
	if ((A <= B) && (A <= C) && (B >= C))
	{
		small = A;
		medium = C;
		large = B;
	}
	if ((A <= B) && (A >= C) && (B <= C))
	{
		small = B;
		medium = A;
		large = C;
	}
	if ((A >= B) && (A <= C) && (B <= C))
	{
		small = B;
		medium = A;
		large = C;
	}
	if ((A >= B) && (A >= C) && (B <= C))
	{
		small = B;
		medium = C;
		large = A;
	}
	if ((A >= B) && (A <= C) && (B >= C))
	{
		small = C;
		medium = A;
		large = B;
	}
	if ((A >= B) && (A >= C) && (B >= C))
	{
		small = C;
		medium = B;
		large = A;
	}
	if ((A <= B) && (A >= C) && (B >= C))
	{
		small = A;
		medium = B;
		large = C;
	}
	if ((A <= B) && (A >= C) && (B >= C))
	{
		small = C;
		medium = A;
		large = B;
	}

	A = small;
	B = medium;
	C = large;
	return;
}


My coding is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()

#include<iostream> 
#include<cmath>
#include"Jdog.h"
{
	using namespace std;

	void increasing(double& A, double& B, double& C);
	double A, double B, double C;
	int small, int medium, int large;

	cout << "Enter three numbers" << endl;
	cin >> A >> B >> C;
	A = small, B = medium, C = large;

	cout << "Numbers in increasing order:" << void increasing(A, B, C) << small, meduim, large << endl;
	return 0;
}


My issues:
-The "void increasing" in my cout is coming up as an error.
-I'm not understanding how to rearrange A,B and C in increasing order and subsequently decreasing order.
-Frankly I don't know how to get the last one.
Hello jahymanson,

Welcome to the forum and thank you for using code tags.

In line 10 you define A, B, C as doubles and in line 11 small, medium and large are defined as ints. This will be a problem if you try to put a double into an int. Some information about the number, like the decimal part will be lost, and any resulting calculation will be wrong.

Next none of your variables have been initialized, so at line 15 after entering a number into "A" you set it to garbage using "small". The same for "B" and "C".

Then in lint 17 when you call the function you are sending a number like "-858993460" which will not work well.

With what you have it would be better to docin >> small >> medium >> large;.

That is what I see right now. Give it a try and see if it changes anything. I also suggest initializing your variables and change the int to a double.

Hope that helps,

Andy
Andy,

How do I initialize the variables?
Hello jahymanson,

1
2
double A{}, double B{}, double C{};
int small{}, int medium{}, int large{}; // <--- Better to make these double. 


The {} or uniform initialization will set these variables to zero.

Hope that helps,

Andy
Hello jahymanson,

After I loaded the program I found some other problems.

First in main the preferred way of writing the file is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream> 
#include<cmath>
#include"Jdog.h"

using namespace std;  // <--- Not the best idea to use this.

//  I believe this is in the header file "Jdog.h" and is not needed here.
void increasing(double& A, double& B, double& C);

int main()
{

	double A, double B, double C;
	int small, int medium, int large;

	cout << "Enter three numbers" << endl;
	cin >> A >> B >> C;
	A = small, B = medium, C = large;

	cout << "Numbers in increasing order:" << void increasing(A, B, C) << small, meduim, large << endl;
	return 0;
}


Now the lineint small, int medium, int large; if you replace the commas with semicolons it would work otherwise you do not need the "int" before each name.And "small" tuned out to be a problem .An example of "using namespace std;" When the program is compiles, or in my case Visual Studio IDE flagged "small" even before I could compile it, "std::" is put in front of "small" believing that "small" should be in the "std" namespace. It is also expecting "size" to have a parameter which you do not provide, so it believes it to be an error. Before I figured out the problem with "small" I changed it ot "least" as a work around.

In the cout line void increasing(A, B, C) is not a function call with the return type of void. "void" does not need to be there for a function call. Also because the function does not return anything it has no use here. The function call should be done before the cout and in the cout "A", "B" and "C" should be in the place of the function call. Then small, meduim, large will not work. You need "<<" in place of the commas.

It is a good chance that you will need something before the "return 0" to pause the program before the program ends so you have a chance to read what was printed.

See http://www.cplusplus.com/forum/beginner/1988/

I still have to test the "increase" function to see how it is working.

Hope that helps,

Andy
Edit:
Last edited on
Hello jahymanson,

After testing the "increasing" function I found the same problem with the definition of the variables in the function. This is all you need for the definition: double least{}, medium{}, large{};. After that the code worked for everything I tested.

I also made a change to the output in main to show the original numbers and the new order.

Hope that helps,

Andy
Topic archived. No new replies allowed.