ex4_08.cpp

来自「Beginning Visual C++ 6源码。Wrox。」· C++ 代码 · 共 36 行

CPP
36
字号
// EX4_08.CPP
// Flexible array management using sizeof
#include <iostream>
using namespace std;

int main()
{
   const char* pstr[] = { "Robert Redford",      // Initializing a pointer array
                    "Hopalong Cassidy",
                    "Lassie",
                    "Slim Pickens",
                    "Boris Karloff",
                    "Oliver Hardy"
                  };  

   char* pstart = "Your lucky star is ";
   int count = (sizeof pstr)/(sizeof pstr[0]); // Number of array elements

   int dice = 0;

   cout << endl
        << " Pick a lucky star!"
        << " Enter a number between 1 and " << count << ": ";
   cin >> dice;

   cout << endl;
   if(dice >= 1 && dice <= count)                     // Check input validity
      cout << pstart << pstr[dice-1];                  // Output star name

   else
      cout << "Sorry, you haven't got a lucky star."; // Invalid input

   cout << endl;
   return 0;
}

⌨️ 快捷键说明

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