1. Write a program that prompts the user to enter two numbers then an operator, The program should display the output using switch statements.
2. (a) Write a program to return the ASCII character of any key on the keyboard.
(b)Demonstrate how you would validate for:
(i) Numeric entry only
(ii) Alphanumeric entry only....
1)
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double a, b;
char sign;
double result;
cout << "Enter an expression: ";
cin >> a >> sign >> b;
switch (sign)
{
case '+':
result = a + b;
break;
case '*':
result = a * b;
break;
case '-':
result = a - b;
break;
case '/':
result = a / b;
break;
default:
cout << "Invalid input." << endl;
return -1;
}
cout << result << endl;
return 0;
}
2)
#include <iostream>
#include <cstdlib>
#include <string>
#include <conio.h>
using namespace std;
bool validate_numeric(string input)
{
for (int c = 0; c < input.size(); c++)
& if (!isdigit(input[c]))
& return false;
return true;
}
bool validate_alpha_numeric(string input)
{
for (int c = 0; c < input.size(); c++)
& if (!isdigit(input[c]) && !isalpha(input[c]))
& return false;
return true;
}
int main()
{
char c;
string value;
cout << "Press any key: ";
c = _getch();
cout << c << endl;
cout << "The corresponding ASCII character code is: " << (int)c;
cout << endl << endl;
cout << "Enter a numeric value: ";
getline(cin, value);
if (!validate_numeric(value))
& cout << "Wrong input" << endl << endl;
cout << "Enter an alphanumeric value: ";
getline(cin, value);
if (!validate_alpha_numeric(value))
& cout << "Wrong input" << endl;
return 0;
}