Tuesday, November 1, 2011

Struct Sample Code

#include <string>
#include <iostream>
using namespace std;

  struct Course
  {
    string name;
    int session;
    float grade;
    string prereq;
    string teacher;
  }; // Student

int main()
{
/* how to demarc the structs and arrays
 * struc s[5] = { {s1, s2, s3, s4, s5},
 *                {s1, s2, s3, s4, s5},
 *                 ....}};
 */
  Course clist[5] = {
   {"COMSC110", 3131, 100.00, "COMSC100","Robert"},
   {"COMSC100", 1111, 100.00, "COMSC90","Tony"},
   {"COMSC110", 2131, 100.00, "COMSC100","Larry"},
   {"COMSC265", 3331, 100.00, "COMSC110","Joe"},
   {"COMSC110", 3431, 100.00, "COMSC100","Tom"}};
//  clist[0].session = 3131;
//  clist[0].teacher = "Tony";
//  clist[0].name = "COMSC110";

  string term;
  cout << "What Course are you looking for? ";
  getline(cin, term);

  // a way to querry the course, and print out only COMSC110
  for(int i =0; i <5; i++)
    if(clist[i].name == term)
    cout << clist[i].name << ": "
       << clist[i].session << " by "
       << clist[i].teacher << endl;

  return 0;
}

No comments:

Post a Comment