9_295_2.cpp

来自「我学习C++ Primer Plus过程中写下的课后作业的编程代码」· C++ 代码 · 共 35 行

CPP
35
字号
// 2.修改程序清单9.8:用string对象代替字符数组.这样,该程序将不再需要检查输入的字符串
// 是否过长,同时可以将输入字符串同字符串""进行比较,比判断是否为空行.

// static.cpp -- using a static local variable

#include <iostream>
#include <string>

void strcount (const std::string &str);

int main()
{
	using namespace std;
	string input;
	
	cout<<"Enter a line: \n";
	getline(cin, input);
	while ("" !=input)
	{
		strcount (input);
		cout<<"Enter next line (empty line to quit):\n";
		getline(cin, input);
	}
	cout << "Bye\n";
	return 0;
}

void strcount (const std::string &str)
{
	using std::cout;
	cout<<"\""<<str <<"\"contains";
	int count = str.length();
	cout << count << " characters\n";
}

⌨️ 快捷键说明

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