Question

In: Computer Science

C++ function to a string into enum function to a enum into a string check valid...

C++

function to a string into enum

function to a enum into a string

check valid / loop ( asking a couple of times until answer invaild)

For example

Enum Fruit ( APPLE, STRAWBERRY, BLUEBERRY, BLACKBERRY)

output should be

what is your favorit fruit? : strawberry

you will have STRAWBERRY.

what is your favorite fruite : peach

invaild

TIA

Solutions

Expert Solution

code:

#include <iostream>
#include <string>
#include<stdio.h>
#include <cstdlib>
using namespace std;
enum Fruit {APPLE,STRAWBERRY, BLUEBERRY, BLACKBERRY}; //declaration of enum

Fruit val(const std::string& s) //function to convert string into ennum
{
if(s=="APPLE"){
       return APPLE;
   }
else if(s=="STRAWBERRY"){ //checking string and returning enum value
       return STRAWBERRY;
   }
else if(s=="BLUEBERRY"){
       return BLUEBERRY;
   }
   else if(s=="BLACKBERRY"){
       return BLACKBERRY;
   }
   else{
       cout<<"Invalid";
   }
}
Fruit con(Fruit v){                //function to convert enum into string
   switch(v){
       case APPLE:
           cout<<"Apple";
       break;
       case STRAWBERRY:
           cout<<"STRAWBERRY";   
       break;
       case BLUEBERRY:               //checking enum to send string value
           cout<<"BLUEBERRY";
       break;
       case BLACKBERRY:
           cout<<"BLACKBERRY";
       break;
       default:
           cout<<"No such fruit";
       break;                       //break statement to stop condition
   }
}

int main()
{
string mystring;    //string declaration
Fruit t;         //creating object of fruit
t=APPLE;
con(t);        //passing enum value to con function
cout<<"\n";
    do{   
               cout<<"What is your favourite fruit:";
           cin >> mystring;
           Fruit f = val(mystring); //passing string value to convert to enum
   switch (f) {
  
   case APPLE:
   cout<<"You will have APPLE"<<"\n";
   break;
   case STRAWBERRY:
               cout<<"You will have STRAWBERRY"<<"\n";
   break;
   case BLUEBERRY:
               cout<<"You will have BLUEBERRY"<<"\n";
   break;
   case BLACKBERRY:
               cout<<"You will have BLACKBERRY"<<"\n";
   break;
   default:
               exit(0); //to exit out of console
           break;
   }
   }   while(true);

return 0;
}

output:

code screenshot:


Related Solutions

C# Palindrome Permutation: Given a string, write a function to check if it is a permutation...
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words. Input: Tact Coa Output: True (permutations: "taco cat", "atco cta", etc.) Comment your code to explain it.
In C++ Valid Palindrome In this assignment, you need to implement a bool isPalindrome(string s) function....
In C++ Valid Palindrome In this assignment, you need to implement a bool isPalindrome(string s) function. Given a string s, isPalindrome(s) can determine if s is a palindrome, considering only alphanumeric characters and ignoring cases. Note: for the purpose of this problem, we define empty string as valid palindrome. Example 1: Input: ”A man, a plan, a canal: Panama” Output: true Example 2: Input: ”race a car” Output: false Requirement: There are many methods to check if a string is...
Write a Java method to check whether a string is a valid password. Password rules: A...
Write a Java method to check whether a string is a valid password. Password rules: A password must have at least ten characters. A password consists of only letters and digits. A password must contain at least two digits. There are at least SIX functions/methods as the following: 1. menu() – to print the password’s rules. 2. getString() – to get the password from the user. 3. isPassword() – to check either the password is valid based on the given...
Write a program that can check whether or not some string is a valid VUnet ID....
Write a program that can check whether or not some string is a valid VUnet ID. A valid VUnet ID consists of 3 letters followed by 3 digits. The letters in the VUnet ID can be written as lower case letters as well as upper case letters. Examples of valid VUnet IDs are 'ABC123', 'def456', and 'Ghi789'. An example of an invalid VUnet ID is 'ab123c'. Define functions to structure your program. That is what this assignment is all about!...
(10 marks) Write a function to check whether string s1 is a substring of string s2....
Write a function to check whether string s1 is a substring of string s2. The function returns the first index in s2 if there is a match. Otherwise, return -1. For example, the function should return 2 if s1 is "to" and s2 is "October". If s1 is "andy" and s2 is "candy", then the function should return 1. The function prototype is as follows: int indexOf(const char *s1, const char *s2).
There is a C function decodeMorse(const String & string, char message[]). This function examines the binary...
There is a C function decodeMorse(const String & string, char message[]). This function examines the binary string and iteratively constructs a decimal value (val) and width of each binary pattern (separated by spaces), until a space or a null character ('\0') is encountered in the string. Once a space or a null character is found, this function should call the assembly code (decode_morse()) to obtain the corresponding ASCII value, for the current val and width, and place the ASCII value...
Write a recursive function to check if a string whose each character is stored in a...
Write a recursive function to check if a string whose each character is stored in a separate node in a doubly linked list, is a palindrome. Use the code available from DoublyLinkedList.hpp on our Github. // // Doubly-linked list with 2 dummy nodes // #pragma once #include <stdexcept> template<typename T> struct Node { T data; Node<T>* next; Node<T>* prev; Node() = delete; // Intentionally no default constructor Node( const T & element ) : data( element ), next( nullptr ),...
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Create program which verifies if input string is a valid variable declaration or not. Use C...
Create program which verifies if input string is a valid variable declaration or not. Use C programming language. - This program can only use the following variable types: char, float, and int - Remove any newline \n from input string - The input prompt should say ">>> " - If input declaration is valid, it should print "Valid dec\n" - If input declaration is invalid, it should print "Invalid dec\n" - Make sure the identifier entered matches the rules of...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT