In: Computer Science
(Language: c++)Write a program that displays a question and 4 possible
answers numbered 1 through 4. . The program should
ask the user to answer 1, 2, 3, or 4 and tell them if they
are correct or not. If the user enters anything besides 1, 2,
3, or 4 the program should return an error message
example outout: whats 2+5?
1. 4
2. 7
3. 1
4. 0
// if user inputs anything other then option 2 the screen should display "incorrect"
// if the user inputs anything other then 1,2,3,or 4 the screen should display "error invalid answer given"
//, when the correct answer is given computer, should display "correct".
Source code:
#include <iostream>
using namespace std;
int main()
{
int ans=0;
cout<<"What is 2+5 ?"<<endl;
cout<<"1.4"<<endl;
cout<<"2.7"<<endl;
cout<<"3.1"<<endl;
cout<<"4.0"<<endl;
cin>>ans;
if(ans!=1 && ans!=2 && ans!=3
&& ans!=4)
{
cout<<"error invalid answer
given"<<endl;
}
else
{
if(ans==2)
{
cout<<"correct"<<endl;
}
else
{
cout<<"incorrect"<<endl;
}
}
}
Sample input and output: