/*
4-10 if-else-if vs. switch
Set up something that uses a switch statement and an if-else-if to do exactly the same thing.
*/
#include <iostream>
#include <string>
usingnamespace std;
void main()
{
string cool;
string y;
string n;
cout << " If you type y, I will tell you you're cool. If you type n, I'll tell you you're not. <y/n> : ";
cin >> cool;
if (cool == "y")
{cout << "You're Cool.\n";}
elseif ( cool == "n")
{cout << "You're not cool.\n";}
else
{ cout << "You didnt put a y or an n.\n";}
cout << "I will do the same thing using a switch.\n";
switch (cool)
{
case y:
cout << "You're Cool.\n";
break;
case n:
cout << "You're not Cool.\n";
break;
default:
cout << "You didnt put a y or an n.\n";
break;
}
system ("pause");
}
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
char cool;
cout << " If you type y, I will tell you you're cool. If you type n, I'll tell you you're not. <y/n> : ";
cin >> cool;
int choice=static_cast<int>(cool);
switch (cool)
{
case 121:
cout << "You're Cool.\n";
break;
case 110:
cout << "You're not Cool.\n";
break;
default:
cout << "You didnt put a y or an n.\n";
break;
}
}