Working with strings in an array - C++ Data Type

C++ examples for Data Type:Array

Description

Working with strings in an array

Demo Code

#include  <iostream>

int main()/*from   w  ww. ja v a2  s.  c o  m*/
{
  const int max_str {80};      // Maximum string length including \0
  char stars[][max_str] {
                          "AAAAAA",  "EEEEEE",
                          "BBBBBB",  "FFFFFF",
                          "CCCCCC",  "GGGGGG",
                          "DDDDDD",  "HHHHHH"
                        };
  int choice {};

  std::cout << "Pick a lucky star! Enter a number between 1 and "
            << sizeof(stars)/sizeof(stars[0]) << ": ";
  std::cin  >> choice;

  if(choice >= 1 && choice <= sizeof stars/sizeof stars[0])
  {
    std::cout << "Your lucky star is " << stars[choice - 1] << std::endl;
  }
  else
  {
    std::cout << "Sorry, you haven't got a lucky star." << std::endl;
  }
}

Result


Related Tutorials