C++ program to find greatest of two numbers using Switch case conditional statement.

C++ program to find greatest of two numbers using Switch case conditional statement.
I am finding error's & output is in correct. can any one help me.

#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
int main()
{
clrscr();
int First, Second;
cout<<"Enter two Integers."<<endl;
cout<<"First"<<setw(3)<<": ";
cin>>First;
cout<<"Second"<<setw(3)<<": ";
cin>>Second;
switch ((First > Second))
{
case 0:
cout<<"First is Greater than Second."<<endl;
break;
};
switch ((First < Second))
{
case 0:
cout<<"Second is Greater than First."<<endl;
break;
};
switch ((First == Second))
{
case 0:
cout<<"First and Second are Equal."<<endl;
break;
};
getch();
return 0;
}
The code makes no sense. What's the point of the switches?
Hey there. i went about this slightly different, but the result should be the same
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

#include <iostream>

int main()
{
	//Declared the variables.
	int first, second;
	//Asked for the numbers.
	std::cout << "What is the first number?\n";
	std::cin >> first;
	std::cout << "What is the second number?\n";
	std::cin >> second;

	//Set up the switches.
	switch(first>second)
	{
	case 1:
		std::cout <<"The first number is bigger.\n";
		break;
	};

	switch(first<second)
	{
	case 1:
		std::cout <<"The second number is bigger.\n";
		break;
	};

	switch(first==second)
	{
	case 1:
		std::cout <<"The numbers are equal.\n";
		break;
	};

	//Paused the compiler before it exited. i left out any text because it seemed pointless, and there are two 
	//because the "Enter" from the last "std::cin" was being read by the "std::cin.get()".
	std::cin.get();
	std::cin.get();
	return 0;
}


the only thing that i saw was that since you were asking for it to compare two things, in a Boolean manner, case 0: was meaning "if the statement is false: output this". i just changed it to case 1: and to reverse it. :-) hope this helped!
C++ program to find greatest of two numbers using Switch case conditional statement.

Is this an exercise??

It is a bit of a misuse of a switch statement: if statements are what you you should use here (when a condition evaluate to true or false).

Also note that the code posted is solving a bigger problem that required by the original statement. It just asks you to find the bigger value.
Topic archived. No new replies allowed.