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.
Write a program that translates a letter grade into a number grade. Letter grades are A,...
Write a program that translates a letter grade into a number grade. Letter grades are A, B, C, D, and F, possibly followed by + or -. Their numeric values are 4, 3, 2, 1 and 0. There is no F+ or F-. A “+” increases the numeric value by 0.3, a “–“ decreases it by 0.3. However, an A+ has value 4.0.4 pts Enter a Letter grade: B- The numeric value is 2.7 Your code with comments A screenshot...
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...
how can I get my program to read one of 4 text files (chosen/ inputted by...
how can I get my program to read one of 4 text files (chosen/ inputted by user, game1.txt, game2, game3, or game4.txt). and place its information into variables, one of which is an array. I sort of understand this, but I don't know how to make my program know which parts of the textfile go into which variables. my 4 textfiles are in the format: the list of e4-bc need to be entered into my array. B 35 e4 e5...
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...
This is for java For my assignment, I was supposed to make a number guessing game....
This is for java For my assignment, I was supposed to make a number guessing game. The class, HiLo, should pick a random number between 1 and 100 (inclusive). Then, prompt the user to guess the number. On each guess, print if the user is correct or if the guess is too high or too low. Allow only 5 guesses. In the end, print out the correct answer and how many attempts were made. Then give the user the option...
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...
C++ Create a program that checks whether a number is a prime number and displays its...
C++ Create a program that checks whether a number is a prime number and displays its factors if it is not a prime number. Console Prime Number Checker Please enter an integer between 1 and 5000: 5 5 is a prime number. Try again? (y/n): y Please enter an integer between 1 and 5000: 6 6 is NOT a prime number. It has 4 factors: 1 2 3 6 Try again? (y/n): y Please enter an integer between 1 and...
Write a Java program that calculates a random number 1 through 100. The program then asks...
Write a Java program that calculates a random number 1 through 100. The program then asks the user to guess the number.If the user guesses too high or too low then the program should output "too high" or "too low" accordingly.The program must let the user continue to guess until the user correctly guesses the number. ★Modify the program to output how many guesses it took the user to correctly guess the right number
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT