Thread: Advantages in struct?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    Advantages in struct?

    This was an exercise comparing to methods of holding month/days of month data and displaying them. Which would you use and why?

    The exercise is from C++ Programming Language 3rd edition 5.9 #7, pg 105.

    Code:
    // year.cc - to compare an array of structs to an array of cstrings
    //           holding  month/day of month data
    
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    
    int main(int argc, char *argv[])
    {
      const char *months[12] = {"January","February","March","April",
    			    "May","June","July","August",
    			    "September","October","November","December"};
    
      int daysofmonths[12]={31,28,31,30,31,30,31,30,30,31,30,31};
    
      struct monthdays{
        char month[10];
        int days;
      };
    
      monthdays year[12]={"January",31,"February",28,"March",31,"April",30,
    		      "May",31,"June",30,"July",31,"August",30,
    		      "September",30,"October",31,"November",30,"December",31};
    
      for (int index=0; index < 12; ++index)
        {
          cout << months[index] << "\t\t\t" << daysofmonths[index] << endl;
        }
      
      for (int index=0; index < 12; ++index)
        {
          cout << year[index].month << "\t\t\t" << year[index].days << endl;
        }
    
    return 0;
    }
    forgot to declare everything as const oh well
    Last edited by curlious; 12-14-2003 at 01:50 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 07:13 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  4. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM