topical media & game development

talk show tell print

basic-program-code-04-Ex4-08.c

? / basic-program-code-04-Ex4-08.c


  // EX4_08.CPP
  // Flexible array management using sizeof
  include <iostream>
  using std::cin;
  using std::cout;
  using std::endl;
  
  int main()
  {
     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;
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.