C++编程练习与实现详解
在C++编程中,有许多实用的练习和实现方法,下面将详细介绍一些常见的编程练习及其实现。
1. 姓名输入与问候程序
以下代码实现了一个简单的姓名输入与问候程序,它会根据用户输入的姓名长度给出不同的提示:
const int nm_size = 128;
char user_name[nm_size];
cout << "Please enter your name: ";
cin >> setw(nm_size) >> user_name;
switch (strlen(user_name))
{
// same case labels for 0, 1
case 127:
// maybe string was truncated by setw()
cout << "That is a very big name, indeed -- "
<< "we may have needed to shorten it!\n"
<< "In any case,\n";
// no break -- we fall through ...
default:
// the 127 case drops through to here -- no break
cout << "Hello, " <<