whileio.cpp

来自「c语言教程源码」· C++ 代码 · 共 37 行

CPP
37
字号
//这个程序在本书所带软盘中,文件名为WHILEIO.CPP
//这个程序以人-机对话的方式要求用户输入一个回答,
//以便确定程序是否继续执行。这个程序可以作为一个大程序的一部分。

#include <iostream.h>
#include <ctype.h>		//支持将小写字母转换成大写字母的库子程序toupper()

void main(void)
{
	char ans;

	cout << "Do you want to continue?" << endl;
	cout << "Type y to continue, press any other key to stop...";
	cin  >>  ans;		//等待用户的输入

	while (toupper(ans) == 'Y')
	{
		cout << endl << "Do you want to continue?" << endl;//再次请求输入
		cout << "Type y to continue, press any other key to stop...";
		cin  >>  ans;
	}
	cout << endl << "Thank you for use of this program!" << endl;

}

/*下面是这个程序运行后的一个典型输出结果:
Do you want to continue?
Type y to continue, press any other key to stop...y

Do you want to continue?
Type y to continue, press any other key to stop...y

Do you want to continue?
Type y to continue, press any other key to stop...b

Thank you for use of this program!
*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?