The user is supposed to enter either a ‘yes’ or ‘no’ in response to a prompt. Th
ID: 3703012 • Letter: T
Question
The user is supposed to enter either a ‘yes’ or ‘no’ in response to a prompt.
The script will print “OK, continuing” if the user enters either a ‘y’, ‘Y’,'Yes', or 'yes' or
it will print “OK, halting” if the user enters a ‘n’, ‘N’, 'No', or 'no'
or will print “Error” if the user enters anything else.
Code this twice once with if and once with switch:
Lets recode the example above to keep prompting until an an acceptable yes or no is entered:
What loop should we use?
what should we intialize?
Explanation / Answer
// code using if
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s; // string to store users input
cin>>s;
if(s=="y" || s=="Y" || s=="yes" || s=="Yes") // yes condition
cout<< "OK, continuing "; // yes message
else if(s=="n" || s=="N" || s=="no" || s=="No") // no codition
cout<< "OK, halting "; // no message
else // rest
cout<<"Error "; // error message
}
------------------------------------------------------------------------------------------------------------------------
// code using switch
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
int main()
{
string s; // string to store users input
cin>>s;
switch(s[0]) // first letter of the input is enough to determine the yes or no conditions
{
case('y'):
case('Y'):
{
cout<< "OK, continuing "; // yes message
break; // required for the switch to exit if any of the yes conditions is satisfied
}
case('n'):
case('N'):
{
cout<< "OK, halting "; // no message
break; // required for the switch to exit if any of the no conditions is satisfied
}
default:
cout<<"Error "; // error message
}
}
------------------------------------------------------------------------------------------------------------------------------------
// recode using if to keep prompting
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s; // string to store users input
bool acceptable=0; // initially acceptable is 0 so that loop goes on forever until we change it
while(acceptable != 1) // this will ensure prompting as long ass acceptable is not set to 1
{
cin>>s;
if(s=="y" || s=="Y" || s=="yes" || s=="Yes") // yes condition
{
cout<< "OK, continuing "; // yes message
acceptable=1; // valid input
}
else if(s=="n" || s=="N" || s=="no" || s=="No") // no codition
{
cout<< "OK, halting "; // no message
acceptable=1; // valid input
}
else // rest
cout<<"Error "; // error message
}
}
---------------------------------------------------------------------------------------------------------------------------
// recode using switch to keep prompting
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
int main()
{
string s; // string to store users input
bool acceptable=0; // initially acceptable is 0 so that loop goes on forever until we change it
while(acceptable != 1) // this will ensure prompting as long ass acceptable is not set to 1
{
cin>>s;
switch(s[0]) // first letter of the input is enough to determine the yes or no conditions
{
case('y'):
case('Y'):
{
cout<< "OK, continuing "; // yes message
acceptable=1; // need to change acceptable to 1 as the input is acceptable
break; // required for the switch to exit if any of the yes conditions is satisfied
}
case('n'):
case('N'):
{
cout<< "OK, halting "; // no message
acceptable=1; // need to change acceptable to 1 as the input is acceptable
break; // required for the switch to exit if any of the no conditions is satisfied
}
default:
cout<<"Error "; // error message
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.