Advertisement
skb50bd

StuStruc

May 20th, 2015
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <iomanip>
  5. using namespace std;
  6.  
  7.  
  8. struct date
  9. {
  10.     int day, mon, yr;
  11. };
  12.  
  13. struct student
  14. {
  15.     string id;
  16.     string name;
  17.     double totcre, grapo, cgpa;
  18.     date dob;
  19. };
  20.  
  21.  
  22.  
  23. double calcgpa (double gp, double cd);
  24. struct date getdate ();
  25. struct student getstu ();
  26. void printstu (student a);
  27.  
  28.  
  29.  
  30. int main()
  31. {
  32.     int n, choice = 3, i;
  33.     string temp;
  34.  
  35.     cout << "Number of Students: ";
  36.     getline (cin, temp);
  37.     stringstream (temp) >> n;
  38.  
  39.     struct student stu[n];
  40.  
  41.  
  42.     while (choice != 0)
  43.     {
  44.         cout << "Enter 1 to Write information" << endl << "Enter 2 to Read information" << endl << "Enter 0 to Exit" << endl;
  45.         getline (cin, temp);
  46.         stringstream (temp) >> choice;
  47.  
  48.         if (choice == 1)
  49.         {
  50.             cout << "Enter the entry number to write: ";
  51.             getline (cin, temp);
  52.             stringstream (temp) >> i;
  53.  
  54.             if (i > 0 && i <= n)
  55.                 stu[i-1] = getstu();
  56.             //for (i = 0; i < n; i++)
  57.                 //stu[i] = getstu();
  58.  
  59.             else
  60.                 cout << "Error! Invalid Entry Number";
  61.         }
  62.  
  63.         else if (choice == 2)
  64.         {
  65.             cout << "Enter the entry number to read: ";
  66.             getline (cin, temp);
  67.             stringstream (temp) >> i;
  68.  
  69.             if (i > 0 && i <= n)
  70.                 printstu (stu[i-1]);
  71.             //for (i = 0; i < n; i++)
  72.                 //printstu(stu[i]);
  73.             else
  74.                 cout << "Error! Invalid Entry Number";
  75.         }
  76.  
  77.         else if (choice == 0)
  78.             break;
  79.     }
  80.  
  81.     return 0;
  82. }
  83.  
  84.  
  85.  
  86. double calcgpa (double gp, double cd)
  87. {
  88.     return (gp/cd);
  89. }
  90.  
  91.  
  92.  
  93. date getdate ()
  94. {
  95.     date d;
  96.     string temp;
  97.  
  98.     cout << "Enter the Birthday (DD MM YYYY): ";
  99.  
  100.     getline (cin, temp);
  101.     stringstream (temp) >> d.day;
  102.     getline (cin, temp);
  103.     stringstream (temp) >> d.mon;
  104.     getline (cin, temp);
  105.     stringstream (temp) >> d.yr;
  106.  
  107.     return d;
  108. }
  109.  
  110.  
  111. student getstu ()
  112. {
  113.     student a;
  114.     string temp;
  115.  
  116.     cout << "Enter Student ID: ";
  117.     getline (cin, a.id);
  118.  
  119.     cout << "Enter Student's Name: ";
  120.     getline (cin, a.name);
  121.  
  122.     a.dob = getdate ();
  123.  
  124.     cout << "Enter Total Credits Completed: ";
  125.     getline (cin, temp);
  126.     stringstream (temp) >> a.totcre;
  127.  
  128.     cout << "Enter Grade Point: ";
  129.     getline (cin, temp);
  130.     stringstream (temp) >> a.grapo;
  131.  
  132.     a.cgpa = calcgpa (a.grapo, a.totcre);
  133.  
  134.     cout << "Information of " << a.name << " has been stored successfully" << endl << endl;
  135.  
  136.     return a;
  137. }
  138.  
  139.  
  140.  
  141. void printstu (student a)
  142. {
  143.     cout << left << setw(30) << "ID: " << a.id << endl;
  144.     cout << left << setw(30) << "Name: " << a.name << endl;
  145.     cout << left << setw(30) << "Date of Birth: " << a.dob.day << " - " << a.dob.mon << " - " << a.dob.yr << endl;
  146.     cout << left << setw(30) << "Total Credits Completed: " << a.totcre << endl;
  147.     cout << left << setw(30) << "Grade Point: " << a.grapo << endl;
  148.     cout << left << setw(30) << "CGPA: " << left << setw(2) << a.cgpa << endl << endl;
  149.  
  150.     return;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement