Question

[C++] Declare three Boolean flags and initialize it as with false value. Ask the user to...

[C++] Declare three Boolean flags and initialize it as with false value. Ask the user to enter a number, if the number is even make the first flag true, if the number is odd make the second flag true, and if the value entered by the user was not a number make the third flag true. Print out the values of the three flags along with a proper message telling the user about what he/she entered.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below:

#include<iostream>                                                                               // including the header file  
using namespace std;

int main()
{
   bool flag1=false,flag2=false,flag3=false;                                                   // declaring and initializing 3 flags to flase
   int number;
  
   cout<<"Please enter a number:";                                                               // Asking user to enter a number and storing the number in number variable
   cin>>number;
  
   if(!cin)                                                                                   // if cin did not received the numeric input
   {
       cin.clear();                                                                           // clear cin
       flag3=true;                                                                               // assign true to flag3
   }
   else
   {
       if(number%2==0)                                                                           // else if the number % 2 is equals to 0
           flag1=true;                                                                           // put true in flag1 because it is an even number
       else
           flag2=true;                                                                           // else put true in flag2 because it is an odd number
   }
  
   cout<<endl<<endl<<"The data in the flags are:";                                               // print the data of the flags
   cout<<endl<<"Flag1 : "<<(flag1?"true":"false");                                               // if the data in flags is 1 print true otherwise false
   cout<<endl<<"Flag2 : "<<(flag2?"true":"false");
   cout<<endl<<"Flag3 : "<<(flag3?"true":"false");
  
   if(flag1==true)                                                                               // print whether the data entered by the user is even, odd or not a number
       cout<<endl<<endl<<"You have entered an even number";
   else if(flag2==true)
       cout<<endl<<"You have entered an odd number";
   else if(flag3==true)
       cout<<endl<<"The data entered by you is not a number";
  
  
}

===========================================================================================

Output 1:

========================================================================================

Output 2:

==========================================================================================

Output 3:

===========================================================================================

Ternary operator:

Please check the following code that we have used in this program:

cout<<endl<<"Flag1 : "<<(flag1?"true":"false");

here (flag1?"true":"false") is the ternary operator

that means check the value in the variable flag1

if the value is true then print true ==> ? re presents true

if the value is false then print false ==> : represents false

we may also write the statement simply as:

cout<<endl<<"Flag1 : "<<flag1;

but c++ represents true as 1 and false as 0, so wherever the value of flag is true it will print 1 and wherever the value of flag is false it will print 0. that is why we have used the ternary operator here.

Add a comment
Know the answer?
Add Answer to:
[C++] Declare three Boolean flags and initialize it as with false value. Ask the user to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 8) Write the Java code to Declare an Integer Number and Initialize it to value 10....

    8) Write the Java code to Declare an Integer Number and Initialize it to value 10. Then use Ternary Operator to Check if the Integer Number is Odd or Even and print out the Result Odd or Even.

  • Write code to repeatedly ask the user for a number. If the number is positive and...

    Write code to repeatedly ask the user for a number. If the number is positive and even, print out a happy message. If the number is positive and odd, print out a sad message. If the number is negative, print out an angry message If they enter zero, stop asking If they enter something that isn't an integer, print out a message telling them they are an idiot, but keep looping

  • Evaluate Exponent: Declare a double variable called result and initialize it to 1.0; Ask the user...

    Evaluate Exponent: Declare a double variable called result and initialize it to 1.0; Ask the user for two values - one for the base, and the other for the exponent. If the exponent parameter is 0, give the user an error. You do not need to handle a negative exponent value. Otherwise, declare an integer variable called loop and initialize it to 0 Write a do while loop as follows: Inside the loop: Multiply the results times the number Increment...

  • Java code Guessing Game Refinement. (The user thinks of a number and the program guesses it)...

    Java code Guessing Game Refinement. (The user thinks of a number and the program guesses it) Program should be able to guess correctly in 7 tries or lower. Initialize a variable that represents the lowest possible number to 0 (what type should this be?) Initialize a variable that represent the highest possible number to 100 (what type should this be?) Initialize a Boolean variable that represents if we’ve achieved the correct guess to false Initialize a variable in which to...

  • Please code in c 1. Write a program which does the following: Declare and initialize three...

    Please code in c 1. Write a program which does the following: Declare and initialize three pointers. One points to an integer, one points to a float and one points to a character variable Ask the user to enter appropriate values for these variables. Output the values to the screen by accessing the variables directly and then by using pointer references. Print the address of each variable. Modify the variables by performing any arithmetic operation only using pointer refer- ences...

  • c++ please (1) Write a program that prompts the user to enter an integer value N...

    c++ please (1) Write a program that prompts the user to enter an integer value N which will rpresent the parameter to be sent to a function. Use the format below. Then write a function named CubicRoot The function will receive the parameter N and return'its cubic value. In the main program print the message: (30 Points) Enter an integer N: [. ..1 The cubic root of I.. ] is 2 update the code om part and write another function...

  • How to write this code in C++???? using fstream Write a program which: Asks the user...

    How to write this code in C++???? using fstream Write a program which: Asks the user to enter a positive integer greater than 0 Validates that the entry is a positive integer Outputs the digits in reverse order with a space separating the digits Outputs the even digits not in reverse order with a space separating the digits (consider zero to be even) If there are no even digits, the an appropriate message should be displayed: There are no even...

  • C program (Not C++, or C#) Viginere Cipher 1)Ask the user if we are encrypting or...

    C program (Not C++, or C#) Viginere Cipher 1)Ask the user if we are encrypting or decrypting. 2) Ask the user to enter a sentence to be transformed. 3) Ask the user to enter a sentence that will be used as the encryption or decryption key. 4) The sentences (array of characters) should end with a NULL terminator '\0'. 5) The range of values will be all printable characters on the ASCII chart starting with a SPACE - Value 32,...

  • Write C code to repeatedly ask the user for a number, then once the user enters...

    Write C code to repeatedly ask the user for a number, then once the user enters 0, the code displays the min, max and average of all values entered. To get proper credit, name the variables as listed below and follow the directions carefully. Do not use any break or similar type of statements. To implement the above code, you will need to count the entries - name the counter variable num_count. You will also need to use a variable,...

  • Display a welcome message and then ask the user to enter his/her name Display a personal...

    Display a welcome message and then ask the user to enter his/her name Display a personal welcome message by addressing the user by his/her name. Declare a constant and assign a number to it as the Conference Room capacity. Ask the user how many people are attending the meeting. Display a meaningful message for each of the three different scenarios. The message informs the user whether he/she can have the requested room and also displays the number of people that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT