HELP

Program is about to compare any two numbers input. Then, it will display whether the number is minimum or maximum. Use switch statement.

#include <iostream>
using namespace std;
void main()
{
int num1, num2;

cout << "Input 2 integers separated by a space. " << endl;
cin >> num1 >> num2;

switch (num1 && num2)
{ case 2 :
cout << num1 << " " << "is the minimum number while" << " " << num2 << " " << "is the maximum number." << endl;
break;
case 1 :
cout << num1 << " " << "is the maximum number while" << " " << num2 << " " << "is the minimum number." << endl;
break;
default : cout << "The input is invalid." << endl;
}
system("pause");
}




WHAT AM I DOING WRONG HERE????????? PLEASE
WHAT AM I DOING WRONG HERE


pretty much everything. I'm not sure why you'd even need to use a switch statement for this??

what are you trying to do here:
switch (num1 && num2)
?


you could just do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int main() {

	// some test numbers
	int num1 = 10;
	int num2 = 12;

	if (num1 > num2)
	{
		std::cout << num2 << " is minimum. " << num1 << " is maximum";
	}
	else if (num2 > num1)
	{
		std::cout << num1 << " is minimum. " << num2 << " is maximum";
	}
	else
	{
		std::cout << "Numbers are equal";
	}

	return 0;
}


or just use std::min and max:
http://en.cppreference.com/w/cpp/algorithm/min
Last edited on
Guys I need help. The question says to use switch statement
I don't know why everyone says this is impossible. You just have to be smart about how you do 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
#include <iostream>

using namespace std;

int main()
{
	int num1, num2;

	cout << "Input 2 integers separated by a space. " << endl;
	cin >> num1 >> num2;

	switch (num1 < num2)
	{
	case true:
		cout << num1 << " is the minimum number while " << num2 << " is the maximum number." << endl;
		break;
	case false:
		cout << num1 << " is the maximum number while " << num2 << " is the minimum number." << endl;
		break;
	default:
		cout << "The input is invalid." << endl;
	}

	cin.get();

        // Tested and works
}


A switch statement is used to compare a set value to a list of constant values. This is why the conditionals you had would not work. The values of the numbers could change and evaluate differently. So, you need to evaluate the logical equation within the beginning of the switch and then compare the resulting value with constants true and false.

I'm assuming this is a school assignment. However, this is indeed a horrible way to do it. You are much better off doing this the way that mutexe said: with if statements.
Last edited on
@ShazzyFeey (11)
already http://www.cplusplus.com/forum/general/154193/ thread solve your question.
Topic archived. No new replies allowed.