Function help with a program

Mar 18, 2017 at 1:59pm
I'm having trouble with a function, I have the following function written but it's popping up with errors. The two errors are that largeandsmall is popping up as undefined even though I have it defined in the program, and the first curly brace is popping up with a declaration error. this is all the code I have written including the function

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
#include <iostream>
#include <string>

using namespace std;

int main()
{
	//Declaration Block
	void largeandsmall(int x, int y);
	int firstNum;
	int secondNum;

	//User Input Block
	cout << "Please enter a number: ";
	cin >> firstNum;

	cout << "Please enter a second number: ";
	cin >> secondNum;

		//Processing Block
		if (firstNum == secondNum)
		{
			cout << "The two numbers are equal ";
		}
		else 
			if (firstNum >= secondNum)
			{ 
				cout << "The first number " << firstNum << "was larger than the second number " << secondNum << endl;
			}
			else 
				if (secondNum >= firstNum)
				{
					cout << "The second number " << secondNum << "was larger than the first number " << firstNum << endl;
				}

		system("pause");
		return 0;
}

void largeandsmall(int x, int y);
{
	if (firstNum == secondNum)
	{
		cout << "The two numbers are equal ";
	}
	else if (firstNum > secondNum)
	{
		cout << "The first number " << firstNum << "was larger than the second number " << secondNum << endl;
	}
	else
	{
		cout << "The second number " << secondNum << "was larger than the first number " << firstNum << endl;
	}
}
Mar 18, 2017 at 2:05pm
Line 9: void largeandsmall(int x, int y);

The function prototype (aka forward declaration) should be placed outside of main(). Functions may not be declared within another function.
Last edited on Mar 18, 2017 at 2:05pm
Mar 18, 2017 at 2:13pm
I fixed it but I'm still getting the same errors that I listed above
Mar 18, 2017 at 2:22pm
closed account (48T7M4Gy)
Make these changes:
1
2
3
4
void largeandsmall(int x, int y) ///<=======
{
    int firstNum = x; ///<=======
    int secondNum = y; ///<======= 
Mar 18, 2017 at 2:23pm
Tried compiling and got an error from Line 40. Try removing the semicolon at the end of the line.

1
2
3
4
void largeandsmall(int x, int y)
{ 
...
}

Once you've done that, you'll probably get new errors saying that firstNum and secondNum are not defined. You'll want to update any use of firstNum and secondNum in the largeandsmall() function to x and y respectively. This will ensure that variables used in largeandsmall() match the arguments you passed.

Also, since largeandsmall() is basically the same as the "Processing Block" in main(), you can delete the Processing Block and simply call your function instead.

1
2
3
4
largeandsmall(firstNum, secondNum);

system("pause");
return 0;
Last edited on Mar 18, 2017 at 2:29pm
Mar 18, 2017 at 2:30pm
Line 40:
 
void largeandsmall(int x, int y);

remove the semicolon from the end of the line.

Lines 42 to 52. The variables firstNum and secondNum should have the same names as the function parameters x and y. The actual name doesn't matter, but they must be consistent throughout the function.

Lines 20 through 34 - this is duplicating the processing done by the function. Delete this block of code and replace it with a function call:
 
    largeandsmall(firstNum, secondNum);


edit: sorry - my answer duplicates what has already been said.

Last edited on Mar 18, 2017 at 2:32pm
Mar 18, 2017 at 2:32pm
closed account (48T7M4Gy)
@OP Better still get rid of a bit of the unnecessary repetition and the semiolon blooper and it should be clearer how a function works. (const just means read-only, ie the function doesn't change the values passed to it.

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
#include <iostream>
#include <string>

using namespace std;

//Declaration Block
void largeandsmall(const int, const int);

int main()
{
    int firstNum;
    int secondNum;
    
    //User Input Block
    cout << "Please enter a number: ";
    cin >> firstNum;
    
    cout << "Please enter a second number: ";
    cin >> secondNum;
    
    largeandsmall(firstNum, secondNum);
    
    return 0;
}

void largeandsmall(const int x, const int y) ///<=======
{
    if (x == y)
    {
        cout << "The two numbers are equal ";
    }
    else if (x > y)
    {
        cout << "The first number " << x << "was larger than the second number " << y << endl;
    }
    else
    {
        cout << "The second number " << x << "was larger than the first number " << y << endl;
    }
}
Last edited on Mar 18, 2017 at 3:02pm
Mar 18, 2017 at 2:37pm
@Kemort I have to include the else if statements in the main of the program it's a requirement for the assignment I'm working on
Mar 18, 2017 at 2:55pm
closed account (48T7M4Gy)
@OP OK go for it Cheers :)
Mar 18, 2017 at 3:04pm
i'm also now having a problem with my program when it compiles it displays everything twice I have the following 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
#include <iostream>
#include <string>

using namespace std;

void largeandsmall(const int, const int);

int main()
{
	//Declaration Block
	int firstNum;
	int secondNum;

	//User Input Block
	cout << "Please enter a number: ";
	cin >> firstNum;

	cout << "Please enter a second number: ";
	cin >> secondNum;

	largeandsmall(firstNum, secondNum);

	//Processing Block
	if (firstNum == secondNum)
	{
		cout << "The two numbers are equal ";
	}
	else 
		if (firstNum >= secondNum)
		{
			cout << "The first number " << firstNum << "was larger than the second number " << secondNum << endl;
		}
		else 
			if (secondNum >= firstNum)
			{
				cout << "The second number " << secondNum << " was larger than the first number " << firstNum << endl;
			}

	system("pause");

	return 0;
}

void largeandsmall(int x, int y)
{
	if (x == y)
	{
		cout << "The two numbers are equal ";
	}
	else 
		if (x > y)
	{
		cout << "The first number " << x << "was larger than the second number " << y;
	}
	else
		if (y > x)
	{
		cout << "The second number " << x << "was larger than the first number " << y;
	}
}
Mar 18, 2017 at 3:33pm
closed account (48T7M4Gy)
It displays everything twice because you have two sets of if statements, one in main and the other in the function.

Comment out your line 21 and one set of output should be eliminated.
Mar 18, 2017 at 3:35pm
closed account (48T7M4Gy)
BTW I changed my line 26 by adding the two keywords 'const'.
Mar 18, 2017 at 3:37pm
it displays everything twice

Shouldn’t it?
Let’s assume the user gives the following two numbers: 1 and 2. What do you expect your code to do?
On line 21 it will pass the two numbers to the function largeandsmall(), which will display which one is larger and which is smaller; later, in the rows 24-37, the code again evaluates if the numbers are equal or one is larger and prints out which one is larger and which is smaller.

I think you can easily work out a solution for this ‘issue’ by yourself, can’t you?

- - -
Edit: sorry, hadn't seen kemort already answered.
Last edited on Mar 18, 2017 at 3:38pm
Mar 18, 2017 at 3:46pm
@Kemort sorry to sound dumb but what do you mean by comment out line 21?

@Enoizat I'm still learning most everything on this subject so I do have a little troube with some things
Mar 18, 2017 at 3:52pm
closed account (48T7M4Gy)
Just delete the line and see what happens :)
Last edited on Mar 18, 2017 at 3:55pm
Mar 18, 2017 at 3:55pm
closed account (48T7M4Gy)
To comment out a line you put // at the start of the line. That way the compiler will ignore the line. (Deleting the line means you have to type it back in. By commenting the line with // all you have to do is delete the // to get the line recognised by the compiler again)
Mar 18, 2017 at 4:02pm
thing is I also need to call the function to the program as well so deleting that would get rid of calling the function altogether
Mar 18, 2017 at 4:13pm
closed account (48T7M4Gy)
So delete the other part which is what I showed you in my earlier post. But then you said you had to keep both.

You'll have to decide what you want. You can't have both.
Topic archived. No new replies allowed.