In: Computer Science
This program is supposed to identify whether I inputted a number or a letter through enumerated types and an array of messages. I'm aware that you can't compare strings and enums but I'm out of ideas. What method do you suggest.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
enum letters { A, B, C, D, E, F, G, H, I, J, K, L, M,
N, O, P, Q, R, S, T, U, V, W, X, Y, Z} ;
enum numbers {zero, one, two, three, four, five, six,
seven, eight, nine} ;
const string labeling[] = {"letter", "number"};
//array of messages;
int x;
numbers Numbers; //declare a variable of this
enumerated type
letters Letters;
cout << "insert capital letters A-Z or
zero-nine";
cin >> x;
if (Numbers == x){
cout << labeling[1];
}
if( Letters == x){
cout << labeling[0];
}
}
/*C++ test program to check if user entered is number or letter without using enumerations*/
//main.cpp
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
/*without using enumeration ,one can check if user
input is letter or character*/
/*enum letters { A, B, C, D, E, F, G, H, I, J, K, L,
M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z} ;
enum numbers {zero, one, two, three, four, five, six,
seven, eight, nine} ;
numbers Numbers; //declare a variable of this
enumerated type
letters Letters;*/
const string labeling[] = {"letter", "number"};
//array of messages;
int x;
//read value of integer from user
//In this case, you can enter letter or
character.
//By using cin.fail() that results true if user input
is integer
//otherwise results false for otherthan integer
value
cout << "insert capital letters A-Z or zero-nine
: ";
cin >> x;
/*Using fail method on cin operator
we can check if user input value is integer or
not
If cin.fail is true then the user input is
letter
otherwise it is number*/
if(cin.fail())
cout <<
labeling[0]<<endl;
else
cout <<
labeling[1]<<endl;
system("pause");
return 0;
}
------------------------------------------------------------------------------------------------------------
Sample Output:
Run1:
insert capital letters A-Z or zero-nine : 1
number
Run2:
insert capital letters A-Z or zero-nine : A
letter