Question

In: Computer Science

This program is supposed to identify whether I inputted a number or a letter through enumerated...

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];
   }

}

Solutions

Expert Solution

/*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


Related Solutions

I am trying to write a java program that determines if an inputted year is a...
I am trying to write a java program that determines if an inputted year is a leap year or not, but I am not able to use if else statements how would I do it. I don't need the code just advice.
I want to make a python program that encodes/decodes inputted messages. There are only three options...
I want to make a python program that encodes/decodes inputted messages. There are only three options in the initial menu: 1. encode, 2. decode, 3. quit, and if 1, 2, or 3 are not the input the program says that it is not valid, try again. If encode is chosen, the user inputs their message, the message should only be alphabetical and spaces. Then, the user can pick a number in the range [-27,27] with space being the 27th character...
PLEASE USE THE ECLIPSE. I want to make a program that shows whether the correct number...
PLEASE USE THE ECLIPSE. I want to make a program that shows whether the correct number is correct or incorrect. Example: how much is 1+1? option 1: 2 option 2: 3 option 3: 4 option 4: 5 option 5: 6 The answer is 1. because the answer is 2 and the option number is 1. Write a Java application that simulates a test. The test contains at least five questions about first three lectures of this course. Each question should...
I need to make this program which supposed to accept the date form the user in...
I need to make this program which supposed to accept the date form the user in format of YYYY-MM-DD and output the corresponding date in its expanded format. output should be something like: Enter the date in YYYY-MM-DD: 2020-10-03 October o3, 2020 (in C++) I have this program but there are some errors that I couldn't find. // Lab3_Project_jk.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include<iostream> #include<string> using namespace std; int main()...
1- Create a Java program to determine a certain number whether that number is odd or...
1- Create a Java program to determine a certain number whether that number is odd or even. Hint : use arithmetic operators and expressions to find the odd or even numbers. 2- Create a Java program to ask a user to enter a name and password as shown below: name is “Ahmed” and his password is 2321 or name is “Ali” and his password is 6776 . The program shows a greeting “Hi ..Welcome to my program” if the user...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
What am i doing wrong. I want for the program to run through then when it...
What am i doing wrong. I want for the program to run through then when it gets to "do you want to play again?enter yes or no" if they choose yes I want it to run again if they choose no then end. def play(): print("Welcome to the Game of Life!") print("A. Banker") print("B. Carpenter") print("C. Farmer") user_input = input("What is your occupation? ").upper() if user_input == "A": money = 100 elif user_input == "B": money = 70 elif user_input...
Let’s Measure: Quiz I: Identify what is being described. Write the letter of the best answer...
Let’s Measure: Quiz I: Identify what is being described. Write the letter of the best answer on the line before each number. _______1.) The economic system in which production decisions are made according to customs and traditions, a. Traditional economic system c. market system b. Command economy d. mixed economy ______ 2.) The system that works under the principle that the interest of society should prevail over that of the individual. a. Traditional economic system c. market system b. Command...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT