1.
#include <iostream>
#include <string>using namespace std;
bool accept()
{
cout<<"Do you want to proceed (y or n)?\n";//写出提问
char answer =0;
cin >> answer;//输入回答
if (answer=='y')return true;//若回答y,返回true
else
return false;///否则返回false
}
int main()
{
cout<<accept();//输出结果
std::cin.ignore(std::cin.rdbuf()->in_avail()+1);
return 0;
}
2.
#include <iostream>
#include <string>
using namespace std;
bool accept()
{
cout<<"Do you want to proceed (y or n)?\n";//写出提问
char answer =0;
cin >> answer;//输入回答
switch(answer){
case 'y':return true;break;
case 'n':return false;break;
default:cout<<"I'll take that for a no.\n";
return false;
}
}
int main()
{
cout<<accept();//输出结果
std::cin.ignore(std::cin.rdbuf()->in_avail()+1);
return 0;
}
3.
#include <iostream>
#include <string>
using namespace std;
bool accept()
{
int time =4;
cout<<"Do you want to proceed (y or n)?\n";//提出问题
while(time>=1){//输入次数为三次
char answer =0;
cin>>answer;//输入问题
switch(answer){
case 'y':return true;
case 'n':return false;
default:cout<<"Sorry,I don't understand that .\n";//不是y和n,可再次输入
time--;//输出次数+1
cout<<"you have only "<<time<<" chance.\n";
}
}
cout<<"you have only "<<time<<" chance.\n";
cout<<"I'll take that for a no.\n";//输入次数超过3次后,认为是false
return false;
}
int main()
{
cout<<accept();//输出结果
std::cin.ignore(std::cin.rdbuf()->in_avail()+1);
return 0;
}