Function help

I need to write a function for the following code that passes two values into it from the following code. These are my instructions for the function:
Use an IF – ELSE – IF – ELSE statement to compare the two numbers and then display one of three messages:
The two numbers are equal.
The first number _ was larger than the second number _.
The second number _ was larger than the first number _.




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

using namespace std;

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;

	//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;
}
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
// prototype on top of main (prototype always have a semicolon, and prototypes declare the function (step 1)
void largeandsmall( int x, int y);

// call is 
int main()
{
largeandsmall(firstNum, secondNum);
}


//definition under main (definition always have { }(step 2)
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;
			}
}
//call this after your user input (function call in main() ( step 3))


// firstNum is assigned to x in the function
//secondNum is assigned to y in the function

//you use firstNum and secondNum instead of x and y is because 
//the function uses x and y
//your main uses firstNum and secondNum

// your user inputs and a number you put the numbers into the function by calling it. 


remove the = from the last 2 else

you only need
if (equal numbers)
else if then (first is larger)
else to the remaining ont (second is larger)
Last edited on
you teacher needs to teach you how to code concisely and precisely

a good programmer tries to use few lines as possible for their programs
using nested ternary operator may squeeze out a few more lines from your program:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main()
{
   int firstNum{}, secondNum{};
   std::cout << "Enter numbers \n";
   std::cin >> firstNum >> secondNum;
  (firstNum == secondNum) ? std::cout << "Equal numbers \n" :
       (firstNum > secondNum) ? std::cout << "First is greater \n" :
            std::cout << "Second is greater \n";
}

and some posts on the relative efficiency of the ternary operator vs if ... else
http://stackoverflow.com/questions/3565368/ternary-operator-vs-if-else

It's just a basic programming and logic class, not a exact c plus plus class, that's just the language we're using as a base. The first response helped the most
small unreadable code that does the same thing is not improved.
big, bloated code that does too much is not improved.
You want readable code that resolves the issue without wasted work. Modern program sacrifices wasted work for re-use and readability more often than not.

The second block is fine.
one std::cout << can run through all the ternary operators:
1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
   int firstNum{}, secondNum{};
   std::cout << "Enter numbers \n";
   std::cin >> firstNum >> secondNum;
   std::cout << ( (firstNum == secondNum) ? "Equal numbers \n" :
       (firstNum > secondNum) ? "First is greater \n" : "Second is greater \n" );
}
Topic archived. No new replies allowed.