Ask the user to enter a number between 5 and 8 (inclusive). Use the bool data-ty
ID: 3628359 • Letter: A
Question
Ask the user to enter a number between 5 and 8 (inclusive). Use the bool data-type where appropriate. (DO ALL FOUR)1) Use a for loop to validate the choice. It should continue until the user puts the correct number
2) Use a while loop to validate the choice. It should continue until the user puts the correct number
3) Use a do-while loop to validate the choice. It should continue until the user puts the correct number
4) Use one of the above loops along with a switch statement to validate the user’s input. Do not use if statements.
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
int main()
{int n;
cout<<"for loop ";
for(n=0;n<5||n>8;)
{cout<<"Enter a number between 5 and 8 inclusive: ";
cin>>n;
}
cout<<"while loop ";
cout<<"Enter a number between 5 and 8 inclusive: ";
cin>>n;
while(n<5||n>8)
{cout<<"Enter a number between 5 and 8 inclusive: ";
cin>>n;
}
cout<<"do...while loop ";
do
{cout<<"Enter a number between 5 and 8 inclusive: ";
cin>>n;
}while(n<5||n>8);
cout<<"with switch ";
bool good=false;
do
{cout<<"Enter a number between 5 and 8 inclusive: ";
cin>>n;
switch(n)
{case 5:
case 6:
case 7:
case 8: cout<<"valid data ";
good=true;
break;
default: cout<<"invalid input ";
}
}while(!good);
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.