comparing two numbers

I'm trying to compare two numbers entered by a user and use a main function to read and display the largest and smallest number. What step am I missing?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  int compareNums();

int main()
{
	int x = 0;
	int y = 0;
	
	cout <<"Enter a number: ";
	cin >>x;
	cout <<"Enter another number: ";
	cin >>y;
	
	if(x > y)
	{
		cout<<"Largest number: " << x << endl;
		cout<<"Smallest number: " << y << endl;
	}
	

	system("pause");
	return 0;
}
Hm.. D'you want to use ccmpareNums() function ? Since you've declare the function prototype
I am required to use the compareNums() function but I am not sure how to. All input and output statements much be in main.
In your code, the text will only display if x is bigger than y. So you could add another statement similar to your '(x > y)' one, just opposite.

Also as LendraDwi mentions, your compareNums() function isn't doing anything atm. If you want to use that you could let it accept 2 numbers as parameters and call it after your input.
Last edited on
Would that be something like:

compareNums(int, int);

Yes, how ever you will want to name the types as well. Like you would with a regular int.
Callum5042 When you prototype you don't have to give it names. Though I probably would so you know what they are going to be and you can just copy/paste for the definition of the function.
This is what I have now but i can't figure out how to proceed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void compareNums(int, int);

int main()
{
	int x = 0;
	int y = 0;
	
	cout <<"Enter a number: ";
	cin >>x;
	cout <<"Enter another number: ";
	cin >>y;
	
	compareNums(x , y);
	
	if(x > y)
	{
		cout<<"Largest number: " << x << endl;
		cout<<"Smallest number: " << y << endl;
	}
	

	system("pause");
	return 0;
}
You have compareNums declared but not defined. Here a small example.

1
2
3
4
5
6
7
8
9
10
11
void compareNums(int a, int b)
{
	// code here
}

int main()
{
	compareNums(5, 10);

	return 0;
}


Then in this case, your 'a' becomes 5 and your 'b' becomes 10.

giblit I'm aware you don't have to but in this came it makes it easier to explain.
Last edited on
I thnk if you want to call the function from if statement use bool instead of void, or if you're not using if statement inside main use int
But for now write the function first
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
int main()
{
	int x = 0;
	int y = 0;
	
	cout <<"Enter a number: ";
	cin >>x;
	cout <<"Enter another number: ";
	cin >>y;
	
	if(x > y)
	{
		cout<<"Largest number: " << x << endl;
		cout<<"Smallest number: " << y << endl;
	}
	if(x < y)
	{
		cout<<"Largest number: " << y << endl;
		cout<<"Smallest number: " << x << endl;
	}
	if(x == y)
	{
		cout<<"They are same" << endl;
        }
	

	system("pause");
	return 0;
}
1
2
3
4
5
6
7
8
void compareNums(int& large, int& small)
{
    if (large < small) {
        int tmp = large;
        large = small;
        small = tmp;
    }
}

or
1
2
3
4
void compareNums(int& large, int& small)
{
    if (large < small) std::swap(large, small);
}

Topic archived. No new replies allowed.