Answer to Question #256823 in C++ for Urooj

Question #256823

You are tasked to make an (almost) useless contraption that tells the user when they have entered either the number 1 or 2 to console.

When the user enters 1 the message “you have entered the number 1” should print to console, and when the user enters 2 the message “you have entered the number 2” should print to console.

Hint: This can be solved by having two conditions in the while loop. You will also need an if statement within the loop.


1
Expert's answer
2021-10-26T05:09:27-0400
#include<iostream>
using namespace std;
int main()
{       
  cout<<"Please, enter something with numbers 1 or 2 and others\n";
  char c;
  while(c=getchar()) 
  {                
    if(c=='1') 
       cout<<"you have entered the number 1\n"; 
    else if(c=='2')     
       cout<<"you have entered the number 2\n";
   }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS