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
#include <iostream>
#include <string>
usingnamespace 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 ";
}
elseif (firstNum >= secondNum)
{
cout << "The first number " << firstNum << "was larger than the second number " << secondNum << endl;
}
elseif (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 ";
}
elseif (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;
}
}
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.
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.
@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.
#include <iostream>
#include <string>
usingnamespace std;
//Declaration Block
void largeandsmall(constint, constint);
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(constint x, constint y) ///<=======
{
if (x == y)
{
cout << "The two numbers are equal ";
}
elseif (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;
}
}
#include <iostream>
#include <string>
usingnamespace std;
void largeandsmall(constint, constint);
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 ";
}
elseif (firstNum >= secondNum)
{
cout << "The first number " << firstNum << "was larger than the second number " << secondNum << endl;
}
elseif (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 ";
}
elseif (x > y)
{
cout << "The first number " << x << "was larger than the second number " << y;
}
elseif (y > x)
{
cout << "The second number " << x << "was larger than the first number " << y;
}
}
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.
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)